有人可以为我解释 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 示例在这里
stackoverflow 和其他地方的一些其他内容