gpt4 book ai didi

c - 服务和应用程序之间的命名管道通信

转载 作者:可可西里 更新时间:2023-11-01 11:24:06 25 4
gpt4 key购买 nike

所以我有一个在启动时启动的服务,我有一个应用程序放在启动文件夹中。

因此客户端有时会很晚才连接到命名管道的服务器。

这是我服务中的代码。

hPipe = CreateNamedPipe(TEXT("\\\\.\\pipe\\popupPipe"),
PIPE_ACCESS_DUPLEX | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, // FILE_FLAG_FIRST_PIPE_INSTANCE is not needed but forces CreateNamedPipe(..) to fail if the pipe already exists...
PIPE_WAIT,
1, 1024 * 16, 1024 * 16,
NMPWAIT_USE_DEFAULT_WAIT,
NULL);

HRESULT
SendMessage(){
if (ConnectNamedPipe(hPipe, NULL) != FALSE) { // wait for someone to connect to the pipe
WriteFile(hPipe, (char *)message->buffer, sizeof(message->buffer), &dwWritten, NULL);
return S_OK;
}
return E_FAIL;
}

这是应用程序

hPipe = CreateFile(TEXT("\\\\.\\pipe\\popupPipe"),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);

if (hPipe == INVALID_HANDLE_VALUE)
return -1;

while (hPipe != INVALID_HANDLE_VALUE)
{
DWORD dwRead;
char buffer[100] = { 0 };
while (ReadFile(hPipe, buffer, sizeof(buffer), &dwRead, NULL) != FALSE);
if (dwRead == sizeof(buffer)) {
dwRead = 0;
buffer[100] = '\0';
temp = &buffer[1];
DisplayPopup(hInstance, cmdShow);
}
}
return 0;

但在客户端,应用程序总是返回 INVALID_HANDLE_VALUE

在服务中 SendMessage 被多次调用,所以即使它第一次失败,它应该在客户端连接时成功,但不是。

最佳答案

您不检查管道的创建是否成功。看微软的文档,应该是不成功,因为你混合了参数:

hPipe = CreateNamedPipe(TEXT("\\\\.\\pipe\\popupPipe"),
PIPE_ACCESS_DUPLEX | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, // FILE_FLAG_FIRST_PIPE_INSTANCE is not needed but forces CreateNamedPipe(..) to fail if the pipe already exists...
PIPE_WAIT,
1, 1024 * 16, 1024 * 16,
NMPWAIT_USE_DEFAULT_WAIT,
NULL);

应该是:

hPipe = CreateNamedPipe(TEXT("\\\\.\\pipe\\popupPipe"),
PIPE_ACCESS_DUPLEX, // FILE_FLAG_FIRST_PIPE_INSTANCE is not needed but forces CreateNamedPipe(..) to fail if the pipe already exists...
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
1, 1024 * 16, 1024 * 16,
NMPWAIT_USE_DEFAULT_WAIT,
NULL);

关于c - 服务和应用程序之间的命名管道通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47097996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com