简体   繁体   中英

ReadFile(Client end named pipe) Hangs - Win32 VC++

I have the following code as part of another module that sends messages to the client. This was for IPC. Two dll's are loaded by the exe and these two need to communicate

In DLL-1 I have the following line of code as the server named pipe.

pipe = CreateNamedPipe("\\\\.\\pipe\\S2D8",PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED /**1-way, send only with overlapped IO*/,
        PIPE_TYPE_MESSAGE,1,0,0, 0, NULL);
    if( INVALID_HANDLE_VALUE != pipe )
    {
        log("Created Named Pipe as Serverl\n");     
    }
    else 
    {
        log("Cannot create Named Pipe as Server\n");        
    }

And somewhere else in the DLL-1 I have the following for the server

bool result = ConnectNamedPipe(pipe, NULL);
            if (!result)
            {
                CloseHandle(pipe); // close the pipe

            }
            else
            {
                DWORD numWritten;
                WriteFile(pipe,KeyBoardBuffer,strlen(KeyBoardBuffer) * sizeof(char),&numWritten,0);
                log("Bytes writtern to pipe:%d\n",numWritten);
            }

When I look at the logs, I can see the that named pipe. Good so far.

While in DLL-2 I have the following as the client part

log("Connecting to named pipe at client\n");
        if(pipe2 == NULL || pipe2 == INVALID_HANDLE_VALUE)
        {

            pipe2 = CreateFile("\\\\.\\pipe\\S2D8", GENERIC_READ , 
                FILE_SHARE_READ | FILE_SHARE_WRITE,NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);

            if (pipe2 == INVALID_HANDLE_VALUE)
            {
                log("Cannot connect to named pipe at client%x\n", GetLastError());
                CloseHandle(pipe2);
            }
            else
            {
                log("Connected to named pipe at client! Going to read!!!\n");
                char buffer[256] = {'\0'};
                DWORD numBytesRead = 0;
                BOOL result = ReadFile(
                    pipe2,
                    buffer, // the data from the pipe will be put here
                    sizeof(buffer) * sizeof(char), // number of bytes allocated
                    &numBytesRead, // this will store number of bytes actually read
                    NULL // not using overlapped IO
                    );
                if (result) 
                {
                    kbBuffer[numBytesRead / sizeof(char)] = '\0'; // null terminate the string
                    log( "Number of bytes read: %d\n",numBytesRead);
                    log(kbBuffer );
                }
                else 
                {
                    log("Failed to read data from the pipe.\n");                
                }
            }
        }

And in my logs, I can see the line "Connecting to named pipe at client" and then "Connected to named pipe at client! Going to read!!!", after that there is nothing in the log, everything seems stuck.

Is the naming convention of pipe correct? Or is there any security settings I have to define?

I am using VS2010, Win7 x64.

Any guidance is much appreciated.

You're calling the wrong method. The pipe is supposed to pre-exist, so you should be calling OpenFile() , not CreateFile() .

Ah, I found the answer to the hang, I had to do a PeekNamedPipe(pipe2, NULL, 0, NULL, &bytesAvailable, NULL); and then check for the bytesAvailable to be greater than zero before I did a ReadFile()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM