Windows命名管道写入access violation报错含义及MQL修复方案
问题描述
- 正在开发交易机器人,主体代码使用MQL编写,需要与C++程序实现双向通信:
- 接收C++端下发的买入、卖出交易指令
- 向C++端回传最新资产价格数据
- 两端基于Windows 10系统的命名管道实现通信
- 现存故障:MQL侧执行管道写入操作时触发错误,报错截图如下:

- 咨询诉求:明确该错误的具体含义,获取可落地的修复方法
关联代码
原有写入函数实现
int CNamedPipe::WriteUnicode(string message) { int ushortsToWrite, bytesWritten; ushort UNICODEarray[]; ushortsToWrite = StringToShortArray(message, UNICODEarray); WriteFile(hPipe,ushortsToWrite,sizeof(int),bytesWritten,0); WriteFile(hPipe,UNICODEarray,ushortsToWrite*sizeof(ushort),bytesWritten,0); return bytesWritten; } //+-----------
故障复现精简测试代码
原始项目代码体量过大无法完整贴出,编写的精简版MQL测试代码运行后可复现完全相同的报错,完整代码如下:
//+------------------------------------------------------------------+ //| test3.mq5 | //| Copyright 2021, MetaQuotes Ltd. | //+------------------------------------------------------------------+ #property copyright "Copyright 2021, MetaQuotes Ltd." #property version "1.00" //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ enum ENUM_PIPE_ACCESS { PIPE_ACCESS_INBOUND=1, PIPE_ACCESS_OUTBOUND=2, PIPE_ACCESS_DUPLEX=3, }; //+------------------------------------------------------------------+ enum ENUM_PIPE_MODE { PIPE_TYPE_RW_BYTE=0, PIPE_TYPE_READ_MESSAGE=2, PIPE_TYPE_WRITE_MESSAGE=4, }; #define PIPE_WAIT 0 #define PIPE_NOWAIT 1 #define ERROR_PIPE_CONNECTED 535 #define ERROR_BROKEN_PIPE 109 #define INVALID_HANDLE_VALUE -1 #define GENERIC_READ 0x80000000 #define GENERIC_WRITE 0x40000000 #define OPEN_EXISTING 3 #define PIPE_UNLIMITED_INSTANCES 255 #define MQLTICK_SIZE 40 #define PIPE_BUFFER_SIZE 4096 #define STR_SIZE 255 //+------------------------------------------------------------------+ //| DLL imports | //+------------------------------------------------------------------+ #import "kernel32.dll" int CreateNamedPipeW(string pipeName,int openMode,int pipeMode,int maxInstances,int outBufferSize,int inBufferSize,int defaultTimeOut,int security); int WaitNamedPipeW(string lpNamedPipeName,int nTimeOut); bool ConnectNamedPipe(int pipeHandle,int overlapped); bool DisconnectNamedPipe(int pipeHandle); int CreateFileW(string name,int desiredAccess,int SharedMode,int security,int creation,int flags,int templateFile); int WriteFile(int fileHandle,short &buffer[],int bytes,int &numOfBytes,int overlapped); int WriteFile(int fileHandle,char &buffer[],int bytes,int &numOfBytes,int overlapped); int WriteFile(int fileHandle,MqlTick &outgoing,int bytes,int &numOfBytes,int overlapped); int WriteFile(int fileHandle,int &var,int bytes,int &numOfBytes,int overlapped); int ReadFile(int fileHandle,short &buffer[],int bytes,int &numOfBytes,int overlapped); int ReadFile(int fileHandle,char &buffer[],int bytes,int &numOfBytes,int overlapped); int ReadFile(int fileHandle,MqlTick &incoming,int bytes,int &numOfBytes,int overlapped); int ReadFile(int fileHandle,int &incoming,int bytes,int &numOfBytes,int overlapped); int CloseHandle(int fileHandle); int GetLastError(void); int FlushFileBuffers(int pipeHandle); #import void OnStart() { //--- int buffer_size = PIPE_BUFFER_SIZE; string pipeName = "\\\\.\\pipe\\junkypipe"; int hPipe = CreateNamedPipeW(pipeName, PIPE_ACCESS_DUPLEX,PIPE_TYPE_RW_BYTE,PIPE_UNLIMITED_INSTANCES,buffer_size*sizeof(char), buffer_size*sizeof(char), 0, NULL ); if(hPipe == INVALID_HANDLE_VALUE) { Print("Failed to create the pipe"); }else { Print("Pipe Was Created Successfully"); } Print("Waiting For Client"); bool resultOfClientBeingConnect = ConnectNamedPipe(hPipe, NULL); if(resultOfClientBeingConnect) { Print("Some Client Has Connected"); char buffer[]; string message = "Hi There"; StringToCharArray(message, buffer); int numBytesWritten; WriteFile(hPipe, buffer, buffer_size*sizeof(char) + 1, numBytesWritten, 0); }else { Print("Failed to Connect"); } } //+------------------------------------------------------------------+
内容的提问来源于stack exchange,提问作者George Wilhelm Hegel
相关产品推荐
相关产品推荐

