C++ 使用 Windows 命名管道

标签 c++ windows named-pipes boost-asio

由于某种原因,桅杆和从杆都出现故障,但是我可以找到任何很好的例子来说明它们是如何工作的,所以我不确定我哪里出错了。

主机在 ConnectNamedPipe 之后从不退出 WaitForSingleObject,从机在第一个 boost::asio::read 调用中抛出异常,“等待进程打开管道的另一端”,我通过 WaitNamedPipe是为了等待与主人的 ConnectNamedPipe 一起等待?

母版.cpp

asio::io_service ioservice; 
asio::windows::stream_handle in(ioservice); 
int main()
{
    HANDLE pipe = INVALID_HANDLE_VALUE;
    try
    {
        //create pipe
        pipe = CreateNamedPipe("\\\\.\\pipe\\FLTest",
            PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE | FILE_FLAG_OVERLAPPED,
            PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
            255, 50000,50000, 0, 0);
        if(pipe == INVALID_HANDLE_VALUE)
        {
            printWinError();
            return -1;
        }
        in.assign(pipe); 
        std::cout << "Created pipe" << std::endl;
        //spawn child
        STARTUPINFO         startInfo;
        ZeroMemory(&startInfo, sizeof(STARTUPINFO));
        startInfo.cb = sizeof(STARTUPINFO);
        PROCESS_INFORMATION procInfo;
        ZeroMemory(&procInfo, sizeof(PROCESS_INFORMATION));
        if(CreateProcess(0, "slave.exe", 0,0, FALSE, CREATE_NEW_CONSOLE,
            0, 0, &startInfo, &procInfo))
        {
            std::cout << "Slave process created" << std::endl;
        }
        else
        {
            printWinError();
            DisconnectNamedPipe(pipe);
            return -1;
        }

        OVERLAPPED overlapped = {0};
        overlapped.hEvent = CreateEvent(0,TRUE,FALSE,0);
        if(ConnectNamedPipe(pipe, &overlapped) == FALSE)
        {
            unsigned error = GetLastError();
            if(error != ERROR_PIPE_CONNECTED &&
                error != ERROR_IO_PENDING)
            {
                printWinError();
                DisconnectNamedPipe(pipe);
                return -1;
            }
        }
        WaitForSingleObject(overlapped.hEvent, INFINITE);
        CloseHandle(overlapped.hEvent);
        std::cout << "Pipe connected" << std::endl;

        for(int i = 0; i < 100; ++i)
        {
            boost::system::error_code error;
            unsigned n = i * 5;
            asio::write(in,asio::buffer((char*)&n, sizeof(unsigned)),
                asio::transfer_all(), error);
            if(error)throw boost::system::system_error(error);
        }
        std::cout << "Sent data" << std::endl;

        FlushFileBuffers(pipe);
        DisconnectNamedPipe(pipe);
        system("pause");
        return 0;
    }
    catch(const std::exception &e)
    {
        std::cout << e.what() << std::endl;
        if(pipe != INVALID_HANDLE_VALUE)
            DisconnectNamedPipe(pipe);
        system("pause");
        return -1;
    }
}

奴隶.cpp

asio::io_service ioservice; 
asio::windows::stream_handle in(ioservice); 
int main()
{
    try
    {
        WaitNamedPipe("\\\\.\\pipe\\FLTest", NMPWAIT_WAIT_FOREVER);

        std::cout << "Pipe avaible" << std::endl;
        HANDLE pipe = CreateNamedPipe("\\\\.\\pipe\\FLTest",
            PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
            PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
            255, 50000,50000,0,0);
        if(pipe == INVALID_HANDLE_VALUE)
        {
            printWinError();
            return -1;
        }

        in.assign(pipe); 
        std::cout << "Pipe connected" << std::endl;

        for(int i = 0; i < 100; ++i)
        {
            std::cout << "i: " << i << std::endl;
            boost::system::error_code error;
            unsigned n;
            asio::read(in,asio::buffer((char*)&n,sizeof(unsigned)),
                asio::transfer_all(), error);
            if(error)throw boost::system::system_error(error);
        }
        system("pause");
        return 0;
    }
    catch(const std::exception &e)
    {
        std::cout << e.what() << std::endl;
        system("pause");
        return -1;
    }
}

显然我完全错了,但是我在网上找不到任何东西可以与我的代码进行比较。

最佳答案

在您的奴隶中,您需要调用 CreateFile() 来打开管道,而不是 CreateNamedPipe。

   HANDLE pipe = CreateFile("\\\\.\\pipe\\FLTest",                 
      GENERIC_READ | GENERIC_WRITE,            
      0,                                          
      NULL,                                       
      OPEN_EXISTING,                              
      FILE_FLAG_OVERLAPPED,                     
      NULL
      );    

关于C++ 使用 Windows 命名管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1236460/

相关文章:

c# - 你能用一个命名管道客户端读写吗?

c++ - 尝试从命名管道读取时获取错误代码 998 对内存位置的访问无效

c++ - 如何使用 Unicode 中的 CFile::Read() 从文件中获取 CString 对象?

c++ - 未调用复制构造函数!

c++ - C++ 中 SetProcessAffinityMask 的示例用法?

windows - 如何确保应用程序始终在 Windows 启动后同时启动?

windows - 使用批处理根据分辨率重命名我的视频文件

c++ - Windows 上的命名管道

c++ - 使程序使用特定日期

java - 3D游戏几何