简体   繁体   English

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

[英]Named Pipe communication between service and application

So I have a service which starts at boot time, and I have an application which I have placed in the startup folder.所以我有一个在启动时启动的服务,我有一个应用程序放在启动文件夹中。

So the client sometimes connects very late to the server of the named pipe.所以客户端有时会很晚才连接到命名管道的服务器。

Here is my code in my service.这是我的服务中的代码。

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;
}

and here is the application这是应用程序

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;

but at the clients end the application always returns INVALID_HANDLE_VALUE但在客户端,应用程序总是返回INVALID_HANDLE_VALUE

In the service SendMessage is called multiple times so even if it fails the first times it should succeed when the client connects shouldn't it.在服务中SendMessage被多次调用,所以即使它第一次失败,它应该在客户端连接时成功。

You don't check if the creation of the pipe suceeds.您不检查管道的创建是否成功。 Looking at the Microsoft documentation, it probably does not succeed because you mix parameters:查看 Microsoft 文档,它可能不会成功,因为您混合了参数:

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);

should be:应该:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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