|
|
|
Named Pipes for MT4
 |

Nov 10, 2008 1:09pm
|
 |
Will Code for Pips
|
|
|
|
Named Pipes for MT4
Somebody asked about doing Named Pipes from MT4 to communicate with another program, so here is the interface and some sample code. This is a DLL-free solution, using only MQL4. This assumes you're familiar with named pipes already. If you're not, you can read about them at http://msdn.microsoft.com/en-us/library/aa365780(VS.85).aspx
I really should make this into a library, but I don't have time right now. If someone wants to do that, feel free.
First the interface:
Code:
string PipeNamePrefix="\\\\.\\pipe\\";
int BufferSize = 256;
#define PIPE_ACCESS_INBOUND 1
#define PIPE_ACCESS_OUTBOUND 2
#define PIPE_ACCESS_DUPLEX 3
#define PIPE_TYPE_BYTE 0
#define PIPE_TYPE_MESSAGE 4
#define PIPE_READMODE_BYTE 0
#define PIPE_READMODE_MESSAGE 2
#define PIPE_WAIT 0
#define PIPE_NOWAIT 1
#define INVALID_HANDLE_VALUE 0xffffffff
#define GenericRead 0x80000000
#define GenericWrite 0x40000000
#define OPEN_EXISTING 3
extern string PipeName="MetaTrader";
int PipeHandle = INVALID_HANDLE_VALUE;
int Buffer[64]; // 4 bytes/int * 64 = 256
#import "kernel32.dll"
int CreateNamedPipeA(string pipeName, int openMode, int pipeMode,
int maxInstances, int outBufferSize, int inBufferSize,
int defaultTimeOut, int security );
int WaitNamedPipeA( string lpNamedPipeName, int nTimeOut );
bool PeekNamedPipe( int pipeHandle, int& buffer[], int bufferSize, int& bytesRead[],
int& totalBytesAvail[], int& bytesLeftThisMessage[] );
int CreateFileA( string name, int desiredAccess, int SharedMode,
int security, int creation, int flags, int templateFile );
int WriteFile( int fileHandle, int& buffer[], int bytes, int& numOfBytes[],
int overlapped );
int ReadFile( int fileHandle, int& buffer[], int bytes, int& numOfBytes[], int overlapped );
int CloseHandle( int fileHandle );
int GetError();
#import
To open the pipe as a client, do the following:
Code:
FullPipeName = PipeNamePrefix + PipeName;
if ( PipeHandle == INVALID_HANDLE_VALUE ) {
if ( WaitNamedPipeA( FullPipeName, 1 ) == 0 ) {
//Print( "No pipe available" );
return;
}
PipeHandle = CreateFileA( FullPipeName, GenericRead|GenericWrite,
0, 0, OPEN_EXISTING, 0, 0 );
Print( Symbol(), ": PipeHandle=", PipeHandle );
if ( PipeHandle == INVALID_HANDLE_VALUE ){
Print( "Pipe open failed" );
return;
}
}
To Read from the pipe is a little tricky. You can't read directly into strings, so I read into a int array, then convert the array to a string.:
Code:
ReadFile( PipeHandle, Buffer, BufferSize, bytesRead, 0 );
message = StringFromBuffer(bytesRead[0]);
string StringFromBuffer(int length) {
string message = "";
for ( int i = 0; i < length; i++ ) {
int c = Buffer[i / 4];
int off = i % 4;
int shift = 0;
if ( off == 1 )
shift = 8;
else if ( off == 2 )
shift = 16;
else if ( off == 3 )
shift = 24;
c = (c >> shift) & 0xff;
message = message + CharToStr( c );
}
return( message );
}
And to write to the pipe:
Code:
CopyToBuffer( orderMessage );
result = WriteFile( PipeHandle, Buffer, BufferSize, numOfBytes, 0 );
void CopyToBuffer( string message ) {
for ( int i = 0; i < 64; i++ )
Buffer[i] = 0;
for ( i = 0; i < StringLen( message ); i++ ) {
int off = i % 4;
int shift = 0;
if ( off == 1 )
shift = 8;
else if ( off == 2 )
shift = 16;
else if ( off == 3 )
shift = 24;
Buffer[i/4] |= StringGetChar( message, i ) << shift;
}
}
|

Nov 10, 2008 2:42pm
|
|
|
Thanks
4X: Thank you very much - I know this sort of thing takes a while to develop and debug. I can see that it will have use between VB (and/or VBA) and MT4 due to the restricted DDE interface. I am also working on an Ninja Trader app that could use IPC. I've found implementing old style C++ into C#.NET can be a real challenge! Thanks again, Ken.
Questions:
Buffer[i/4] |= StringGetChar( message, i ) << shift;
I don't understand C syntax enough to know what these 2 operators do:
|= ??? must be ORing (exclusive?)
<< ???
__________________
"A word to the wise is sufficient." - Grandma
"Those that can - do. Those that can't - preach" - Grandpa.
Last edited by Kenz987, Nov 10, 2008 2:47pm
Reason: post questions
|

Nov 10, 2008 6:22pm
|
 |
Will Code for Pips
|
|
|
|
Quote:
Originally Posted by Kenz987
Questions:
Buffer[i/4] |= StringGetChar( message, i ) << shift;
I don't understand C syntax enough to know what these 2 operators do:
|= ??? must be ORing (exclusive?)
<< ???
|
Yes it took quite a bit of work.
x |= y is the same as x = x |y and does a bitwise OR of x and y.
x << y is a shifts x left by y bits. So 1 << 8 is 256.
You can read up more about both of them here: http://book.mql4.com/basics/expressions
|

Nov 13, 2008 10:09pm
|
|
|
thanks
Hey 4X: I actually got this to work. Sending data from Excel to MT4. It took me some time to figure out what you were doing with all that shifting - now I see you are using all 4 bytes of the integer. I thought at first it was some clever way to store more data in 1 byte! ;=)
__________________
"A word to the wise is sufficient." - Grandma
"Those that can - do. Those that can't - preach" - Grandpa.
|

Nov 14, 2008 3:00pm
|
 |
Will Code for Pips
|
|
|
|
Good for you, hopefully it'll make you some pips.
Yes, the code basically packs 4 characters (of 8 bits) into an int (32 bits) for passing to the Read and Write calls. The reason for that is I couldn't get strings to work directly with the calls, so I had to go down a level.
|

Jan 2, 2009 10:56pm
|
 |
Member
|
|
|
|
Quote:
Originally Posted by Kenz987
Hey 4X: I actually got this to work. Sending data from Excel to MT4. It took me some time to figure out what you were doing with all that shifting - now I see you are using all 4 bytes of the integer. I thought at first it was some clever way to store more data in 1 byte! ;=)
|
Hello Kenz,
can you share you source code or parts of it? I'm trying to do the same thing and all I'm getting are locked apps and crashes all over..
Thanks in advance..
|

Jan 2, 2009 11:08pm
|
|
Programming for a better future.
|
|
|
|
another excellent piece of reference codes. Thanks for sharing 4xCoder. =)
Mods, can we have this as a sticky?
|

Feb 4, 2009 5:07am
|
|
|
Exclt to MT4 via named pipes
Quote:
Originally Posted by 4xCoder
Somebody asked about doing Named Pipes from MT4 to communicate with another program, so here is the interface and some sample code. This is a DLL-free solution, using only MQL4. This assumes you're familiar with named pipes already. If you're not, you can read about them at http://msdn.microsoft.com/en-us/library/aa365780(VS.85).aspx
I really should make this into a library, but I don't have time right now. If someone wants to do that, feel free.
First the interface:
[code]
string...
|
Hi 4xCoder,
Thanks very much for sharing the code.
I am looking to use the named pipes to export one integer value from excel to MT4 indicator. Actually, I have a complex calculations done on excel using the (O,H,L,C) that I import from MT4. the result of those complex callculation will be one integer variable with one of 3 values (+1,-1,0). I need to read this value from excel using an MT4 indicator so that I can confirm lmy trades.
Could you pelase share how to do so, an excel example will be very usefull. I am planing to use this 1 min TF.
Thanks in advance,
Cheers,
Way2Freedom
|
 |
|
1 Trader Viewing This Thread (0 are members)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
|