Need help from pro coders, please ...

 

Hi all,

Good day,

I'm on my way learning about Dll (Dynamic Library Link) and how to create it in the right way. I found a code at Forex Factory by 4xCoder that gives the main body of it, but he didn't complete it.

Here is the code:

The first is the interface==>

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();
#importTo 

and To open the pipe as a client, do the following:

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 :

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:

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

Now, I kindly need help from bro coders to setup this and convert it to Dll if possible.

If you guys know the way using Delphi Xe for example, that would be great. If there are other options on how to make it DLL using other programs like VB, that would be fine, too.

Looking forward to hearing from you.

Best wishes,

SF

 

I just want to shoot this up so maybe pro coders can notice.

I'm wondering, Are they in a vacation?? All of them at once???

I'm pretty sure the problem is easy to solve, isn't it?

waiting for you guys.

SF

 

scarface:

Now, I kindly need help from bro coders to setup this and convert it to Dll if possible.

No slaves here. Either learn to code or pay someone.
 
scarface:

I just want to shoot this up so maybe pro coders can notice.

I'm wondering, Are they in a vacation?? All of them at once???

I'm pretty sure the problem is easy to solve, isn't it?

waiting for you guys.

I'm not sure from your messages whether (a) you are looking for general help on how to create DLLs, or (b) you are specifically looking to convert this named-pipe code into DLL form.

(a) is outside the scope of this forum. I don't think you're going to find anyone here prepared to hold your hand through such a very long tutorial.

(b) is a different matter. It would help to know why you want to convert the code into a DLL given that it's possible to use named pipes within MQ4, either using the above code or something like https://www.mql5.com/en/forum/127032. What's the need to turn this into a DLL?

 

scarface:

If you guys know the way using Delphi Xe for example, that would be great. If there are other options on how to make it DLL using other programs like VB, that would be fine, too.

Use Delphi or FPC/Lazarus.

You will create equally efficient code but with half the bugs and in half the time. There is no need to use C++ if you are not absolutely forced to use it by requirements beyond your control (And I cannot imagine what strange requirements this might be that would force one to chose C++ over Object Pascal for a new project).

On ForexFactory there is a thread about making DLLs with FPC/Lazarus, almost everything that is written there can also be 1:1 applied to Delphi.

Pipes are implemented in the class TPipeStream and implement the same easy to use high level interface as all other TStream descendants. No need to use these raw Windows API functions in an environment (the environment where you create your DLL) where there are standard libraries with convenient high level abstractions for things like this. And I am sure even C++ has already something comparable in one of the available libs nowadays.