管道-WaitNamedPipes示例令人困惑(MSDN等)

时间:2019-01-17 15:48:41

标签: named-pipes

有人可以为我解释WaitNamedPipe吗?在MSDN和其他地方使用示例似乎并不像所建议的那样。可以按其他顺序使用它。

在示例中,客户端这样做

CreateFile (Pipe)
WaitNamedPipe
WriteFile
(Flushbuffers, CloseHandle)

对我而言,除非我

,否则WaitNamedFile总是超时
WaitNamedFile
CreateFile
WriteFile
(Flushbuffers, CloseHandle)

服务器-

hPipe = CreateNamedPipe(TEXT(HELPPIPE),
        PIPE_ACCESS_INBOUND,
        PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
        1,
        PIPEBUFFERSIZE,
        PIPEBUFFERSIZE,
        NMPWAIT_USE_DEFAULT_WAIT,
        NULL);

while (!g_Quit)
    {
        if (ConnectNamedPipe(hPipe, NULL))   // wait for someone to connect to the pipe
        {
            char buffer[PIPEBUFFERSIZE];
            DWORD dwRead;

            while (ReadFile(hPipe, buffer, sizeof(buffer) - 1, &dwRead, NULL))
            {
                // add terminating zero
                buffer[dwRead] = '\0';

                if (!processHelpCommand(buffer))
                {
                    g_Quit = true;
                }
            }
        }

        DisconnectNamedPipe(hPipe);
    }

客户端-

 if (WaitNamedPipe (TEXT(HELPPIPE), 2000))
{
    hPipe = CreateFile(TEXT(HELPPIPE), 
        GENERIC_WRITE, 
        0,
        NULL,
        OPEN_EXISTING,
        0,
        NULL);

    if (hPipe != INVALID_HANDLE_VALUE)
    {
        if (WriteFile(hPipe,
                    commandLine,
                    strlen (commandLine) + 1,
                    &dwWritten,
                    NULL))
        {
            success = TRUE;
        }
    }

    FlushFileBuffers(hPipe); 
    CloseHandle(hPipe);
}

MSDN示例为here

关于stackoverflow和其他地方的其他信息

1 个答案:

答案 0 :(得分:0)

还是我误解了MSDN示例;-)

我想我已经回答了我自己的问题。