Home
Search Forums
Keywords:
Search Titles Only
User Name:
Exact Match
Show Results As:
Advanced Options
Reply
 
Thread Tools Search this Thread
  #1  
Old Nov 10, 2008 1:09pm
4xCoder's Avatar
Will Code for Pips
 
Member Since Dec 2005
Default 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;
   }
}
Reply With Quote
  #2  
Old Nov 10, 2008 2:42pm
Member
 
Member Since Aug 2006
Default 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
Reply With Quote
  #3  
Old Nov 10, 2008 6:22pm
4xCoder's Avatar
Will Code for Pips
 
Member Since Dec 2005
Default

Quote:
Originally Posted by Kenz987 View Post
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
Reply With Quote
  #4  
Old Nov 13, 2008 10:09pm
Member
 
Member Since Aug 2006
Default 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.
Reply With Quote
  #5  
Old Nov 14, 2008 3:00pm
4xCoder's Avatar
Will Code for Pips
 
Member Since Dec 2005
Default

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.
Reply With Quote
  #6  
Old Jan 2, 2009 10:56pm
pippero's Avatar
Member
 
Member Since Oct 2007
Default

Quote:
Originally Posted by Kenz987 View Post
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..
Reply With Quote
  #7  
Old Jan 2, 2009 11:08pm
Programming for a better future.
 
Member Since Jun 2008
6 Vouchers  567 Posts
Default

another excellent piece of reference codes. Thanks for sharing 4xCoder. =)

Mods, can we have this as a sticky?
Reply With Quote
  #8  
Old Feb 4, 2009 5:07am
Member
 
Member Since Oct 2008
Default Exclt to MT4 via named pipes

Quote:
Originally Posted by 4xCoder View Post
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
Reply With Quote
Reply

1 Trader Viewing This Thread (0 are members)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads
Thread Thread Starter Forum Replies Last Post
5 decimal places in MT4? 5 secs timeframe in MT4 cywong Broker Discussion 4 Sep 30, 2008 1:25am
MT4 hello1234 Programming Discussion 2 Jul 20, 2008 5:40am
IBFX Named Best Online FX Provider corbinlayton Broker Discussion 6 Jul 18, 2008 12:22pm
MT4 Mobile vs. MT4 Mobile SE - Significant difference? gindrol Programming Discussion 0 Feb 23, 2008 12:25pm
Any effect with charting MT4 but executing trades with non-MT4 broker? freevey Broker Discussion 17 Dec 19, 2007 10:09am