Download MetaTrader 5

Why my NamePipe does not work?

To add comments, please log in or register
One billion tasks have already been executed with MQL5 Cloud Network. Test trading robots even faster!
user0123
24
user0123 2014.02.19 14:30 

From the post "http://www.forexfactory.com/showthread.php?t=125117", I was inspired with the idea to use namepipe to communicate with Excel.
However, when I run the following script in MT4, I always receive an alert "CreateNamedPipe failed". What did I do wrong?

//+------------------------------------------------------------------+
//|                                                   PipeServer.mq4 |
//+------------------------------------------------------------------+

#define PIPE_TYPE_MESSAGE 4
#define PIPE_READMODE_MESSAGE 2
#define PIPE_WAIT 0
#define PIPE_ACCESS_DUPLEX 3
#define PIPE_UNLIMITED_INSTANCES 255
#define NMPWAIT_USE_DEFAULT_WAIT 0
#define INVALID_HANDLE_VALUE -1
#define ERROR_PIPE_CONNECTED 535

#import "kernel32.dll"
        int CreateNamedPipeA(string PipeName,int dwOpenMode,int dwPipeMode,int nMaxInstances,int nOutBufferSize,int nInBufferSize,int nDefaultTimeOut,int lpSecurityAttributes);
        int ConnectNamedPipe(int hPipe,int lpOverlapped);
        int ReadFile(int hPipe, int& inBuffer[],int NumberOfBytesToRead, int& bytesRead[], int lpOverlapped);
        int WriteFile(int hPipe, string sBuffer, int NumberOfBytesToWrite, int& bytesWritten[], int lpOverlapped);
        int FlushFileBuffers(int hPipe);
        int DisconnectNamedPipe(int hPipe);
        int CloseHandle(int hPipe);
#import

string lastMessage="";
extern string Pipe = "Pipe1";

int start()
{
        string PipeName = "\\\\.\\pipe\\"+Pipe;
        int PipeMode = PIPE_TYPE_MESSAGE|PIPE_READMODE_MESSAGE|PIPE_WAIT;
        int hPipe = CreateNamedPipeA(PipeName,PIPE_ACCESS_DUPLEX,PipeMode,PIPE_UNLIMITED_INSTANCES,1024,1024,NMPWAIT_USE_DEFAULT_WAIT,NULL);
        if (hPipe == INVALID_HANDLE_VALUE) {
                Alert("CreateNamedPipe failed");
        return (-1);
        }
        while(!IsStopped())     {

                Comment("Pipe server ready ...\nSend \"STOP\" to stop server\n",lastMessage);
                bool fConnected = ConnectNamedPipe(hPipe, NULL) != 0;
                if(!fConnected)
                        fConnected = GetLastError() == ERROR_PIPE_CONNECTED;
                        
                if (fConnected) {
                        int inBuffer[256];
                        int bytesRead[1];
                        bool fSuccess = ReadFile(hPipe,inBuffer,4*ArraySize(inBuffer),bytesRead,NULL) !=0;
                        if (!fSuccess || (bytesRead[0] == 0)) break;
                        string inString="";
                        for(int i=0; i<bytesRead[0]; i++)
                                inString = inString + CharToStr( (inBuffer[i/4] >> ((i & 3)*8)) & 0xff);
                        lastMessage = "Last message from client: "+inString;
                        string outString = "Received ["+inString+"]";
                        int bytesWritten[1];
                        fSuccess = WriteFile(hPipe,outString,StringLen(outString)+1,bytesWritten,NULL) != 0;
                        if (! fSuccess || bytesWritten[0] != StringLen(outString)+1) break;
                        if(inString=="STOP") {
                                Comment("Pipe server stopped.");
                                FlushFileBuffers(hPipe);
                                DisconnectNamedPipe(hPipe);
                                CloseHandle(hPipe);                             
                                return(0);
                        }                       
                }
                FlushFileBuffers(hPipe);
                DisconnectNamedPipe(hPipe);
        }       
        CloseHandle(hPipe);                     
        return(0);
}
Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2014.02.19 14:59  
user0123:
From the post "http://www.forexfactory.com/showthread.php?t=125117", I was inspired with the idea to use namepipe to communicate with Excel.
However, when I run the following script in MT4, I always receive an alert "CreateNamedPipe failed". What did I do wrong?


Please use the SRC button to post code . . .
whroeder1
14532
whroeder1 2014.02.20 01:12  
user0123: What did I do wrong?
int hPipe = CreateNamedPipeA
ANSI version when strings are now Unicode
user0123
24
user0123 2014.02.20 06:33  
WHRoeder:
ANSI version when strings are now Unicode


Thank for reply. Since I am a beginner of C++ programming, I still don't know how to fix the problem.
Can you tell me how to modify the code so that it can be run in MT4?

whroeder1
14532
whroeder1 2014.02.20 14:36  
user0123: Can you tell me how to modify the code so that it can be run in MT4?
How many other posts about this subject, ANSI DLL calls, and/or build 6xx have you read? https://www.mql4.com/search#!keyword=unicode&method=2
Do Hung
151
Do Hung 2014.04.21 04:56  

Hi, Everything is ok. But "inString" gets only first character of comming text. What should I have to change? Please help.

bool fSuccess = ReadFile(hPipe,inBuffer,4*ArraySize(inBuffer),bytesRead,NULL) !=0;
                        if (!fSuccess || (bytesRead[0] == 0)) break;
                        string inString="";
                        for(int i=0; i<bytesRead[0]; i++)
                                inString = inString + CharToStr( (inBuffer[i/4] >> ((i & 3)*8)) & 0xff);
                        lastMessage = "Last message from client: "+inString;
                        string outString = "Received ["+inString+"]";
Bacha Rehman
4465
Bacha Rehman 2014.05.05 12:12  

I also am facing the same problem .. the server only receive first character of the string. This is crazy from meta-qoutes not to maintain backward compatibilities in build 600+. That means we have to code all over again to make our running code compatible with their new builds .. crazy .. In build 600+ there is an example of pipes which totally uses different logic, structures etc. I know the problem is with this loop but don't know how to solve

for(int i=0; i<bytesRead[0]; i++)
                                inString = inString + CharToStr( (inBuffer[i/4] >> ((i & 3)*8)) & 0xff);
To add comments, please log in or register