I´m trying to integrate MT5 with an external Named Pipe Server application written in C# through named pipes. The problem is the app requires a Asyncronous named pipe, and I can´t find a way to connect to it from MT5.
I´m using the example from https://www.mql5.com/en/articles/503:
//--- wait for pipe server
Print("Client: pipe opened");
C# server source code:
// Delegate for passing received message back to caller
public delegate void DelegateMessage(string Reply);
public event DelegateMessage PipeMessage;
public void Listen(string PipeName)
// Set to class level var so we can re-use in the async callback method
_pipeName = PipeName;
// Create the new async pipe
NamedPipeServerStream pipeServer = new NamedPipeServerStream(PipeName,
PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
// Wait for a connection
(new AsyncCallback(WaitForConnectionCallBack), pipeServer);
catch (Exception oEX)
private void WaitForConnectionCallBack(IAsyncResult iar)
// Get the pipe
NamedPipeServerStream pipeServer = (NamedPipeServerStream)iar.AsyncState;
// End waiting for the connection
byte buffer = new byte;
// Read the incoming message
pipeServer.Read(buffer, 0, 255);
// Convert byte buffer to string
string stringData = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
Debug.WriteLine(stringData + Environment.NewLine);
// Pass message back to calling form
// Kill original sever and create new wait server
pipeServer = null;
pipeServer = new NamedPipeServerStream(_pipeName, PipeDirection.In,
1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
// Recursively wait for the connection again and again....
new AsyncCallback(WaitForConnectionCallBack), pipeServer);
MT5 can connect and send a message once, but then hangs at next connection. I searched through documentatin but found no way to open an async named pipe. Ideas?
I have been using asynchronous pipes with MT4, but the code you are referring to seems to be pure synchronous implementation. To open the async pipe you have to pass the OVERLAPPED structure in argument to Kernel32::ReadFile(pipe, structure, size, transfered, overlappedStructure)
Sorry, did not understand... the C# server code I included above is for an asynchronous named pipe:
pipeServer = new NamedPipeServerStream(_pipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte,PipeOptions.Asynchronous );
My problem is how to open an async named pipe on mql5 client. You seems to make reference to Kernel32.dll. Please note I am NOT importing kernel32.dll. I´m trying to use the native named pipe interface from mql5. And why not use kernel32.dll? Well, because it works just fine on MT4 and on MT5 32 bit versions, but fails miserably on MT5 64 bit. Believe-me, I already tried that. Same code that works fine on MT5 32 bits always return invalid handle on 64 bits. That is why I´m trying build in MT5 CNamePiPe library. Unfortunately, it seems that it also has its own bugs.
Did anyone got MT5 64bits talking to an asynchronous named pipe server?
I see, I did not know that Mql5 had implemented asynchronous file operations. Apologies.
Thanks for information you provided! I also failed with `CFilePipe` @ MetaTrader 5!
But I succeeded with imported `CreateFileW` and `WriteFile` on Windows 10 x86-64 (MetaTrader 5.00 build 1583) and asynchronous named pipe server (written on pure Windows API).
Do you mind sharing your working code? I was able to use kernel32.dll based named pipes at Windows 10 x86-64 on previous MT5 versions, but it seems version 1755 64 bits is broken again. I'm able to CreateFileW, but when I try WriteFile I get an access violation error. Funny thing is the same exact code works on MT5 1755 32 bits.