mqlmetatrader5

What does "access violation " in context of named pipes in windows mean?


I'm trying to develop a trader bot. The bot itself is written in mql, while it communicates with a c++ code, to receive trading commands(buy and sell) from it and also, send the latest asset price to it. This communication is being done through named pipes(in the context of windows 10). The problem is that when the mql code tries to write something, I get the following error:

error

What does this error mean and How can I fix it? thanks.

Bu the way, the function that I use for writing is as follows:

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;
  }
//+-----------

P.S.: My original code was so huge that I could not put it here. I write a shorter version of it with the means of sharing it here. It still generates the same error.The code is in mql, btw.

//+------------------------------------------------------------------+
//|                                                        test3.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#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");
         }
   
  }
//+------------------------------------------------------------------+

Solution

  • Thanks to @RaymondChen, The problem was finally solved. The declaration of WriteFile function was wrong. The fileHandle and overlapped parameter should have been long or ulong instead of int.