创建命名管道 C++ Windows

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26561604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:47:14  来源:igfitidea点击:

Create Named Pipe C++ Windows

windowsc++11ipcnamed-pipesfifo

提问by user3052078

I am trying to create a simple comunication between 2 processes in C++ ( Windows ) like FIFO in linux. This is my server:

我正在尝试在 C++ ( Windows ) 中的 2 个进程之间创建一个简单的通信,例如 linux 中的 FIFO。这是我的服务器:

int main()
{
    HANDLE pipe = CreateFile(TEXT("\\.\pipe\Pipe"), GENERIC_READ, 0, NULL, OPEN_EXISTING,    FILE_FLAG_OVERLAPPED, NULL);
    ConnectNamedPipe(pipe, NULL);
    while(TRUE){
        string data;
        DWORD numRead =1 ;
        ReadFile(pipe, &data, 1024, &numRead, NULL);
        cout << data << endl;

}
    CloseHandle(pipe);
    return 0;
}

And this is my client:

这是我的客户:

int main()
{
    HANDLE pipe = CreateFile(TEXT("\\.\pipe\Pipe"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
    ConnectNamedPipe(pipe, NULL);
    string message = "TEST";
    DWORD numWritten;
    WriteFile(pipe, message.c_str(), message.length(), &numWritten, NULL);
    return 0;
}

The code does't work , how can i fixed it to like FIFO ?

代码不起作用,我如何修复它以喜欢 FIFO?

回答by Lukas Thomsen

You cannot create a named pipe by calling CreateFile(..).

您不能通过调用来创建命名管道CreateFile(..)

Have a look at the pipe examples of the MSDN. Since these examples are quite complex I've quickly written a VERYsimple named pipe server and client.

看看MSDN管道示例。由于这些示例非常复杂,我很快编写了一个非常简单的命名管道服务器和客户端。

int main(void)
{
    HANDLE hPipe;
    char buffer[1024];
    DWORD dwRead;


    hPipe = CreateNamedPipe(TEXT("\\.\pipe\Pipe"),
                            PIPE_ACCESS_DUPLEX,
                            PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,   // FILE_FLAG_FIRST_PIPE_INSTANCE is not needed but forces CreateNamedPipe(..) to fail if the pipe already exists...
                            1,
                            1024 * 16,
                            1024 * 16,
                            NMPWAIT_USE_DEFAULT_WAIT,
                            NULL);
    while (hPipe != INVALID_HANDLE_VALUE)
    {
        if (ConnectNamedPipe(hPipe, NULL) != FALSE)   // wait for someone to connect to the pipe
        {
            while (ReadFile(hPipe, buffer, sizeof(buffer) - 1, &dwRead, NULL) != FALSE)
            {
                /* add terminating zero */
                buffer[dwRead] = '
int main(void)
{
    HANDLE hPipe;
    DWORD dwWritten;


    hPipe = CreateFile(TEXT("\\.\pipe\Pipe"), 
                       GENERIC_READ | GENERIC_WRITE, 
                       0,
                       NULL,
                       OPEN_EXISTING,
                       0,
                       NULL);
    if (hPipe != INVALID_HANDLE_VALUE)
    {
        WriteFile(hPipe,
                  "Hello Pipe\n",
                  12,   // = length of string + terminating '##代码##' !!!
                  &dwWritten,
                  NULL);

        CloseHandle(hPipe);
    }

    return (0);
}
'; /* do something with data in buffer */ printf("%s", buffer); } } DisconnectNamedPipe(hPipe); } return 0; }

And here is the client code:

这是客户端代码:

##代码##

You should replace the name of the pipe TEXT("\\\\.\\pipe\\Pipe")by a #define which is located in a commonly used header file.

您应该将管道名称替换TEXT("\\\\.\\pipe\\Pipe")为位于常用头文件中的 #define。