在Windows中命名管道

时间:2014-10-20 09:53:38

标签: c named-pipes

我正在尝试通过namedpipes实现两个进程之间的通信。为了更精确(不要认为它会影响问题),我希望两个Matlab实例相互通信。

到目前为止一切正常,但在10%的情况下,它表示管道末端还有另一个进程......

我的代码(缩短版)到目前为止(发件人):

pipe = CreateNamedPipe(uniquePipeName, // name of the pipe
    PIPE_ACCESS_DUPLEX, // 
    PIPE_TYPE_MESSAGE, // send data as a byte stream
    1, // only allow 1 instance of this pipe
    0, // no outbound buffer
    0, // no inbound buffer
    0, // use default wait time
    NULL // use default security attributes
    );
... some error handling...
result = ConnectNamedPipe(pipe, NULL);
if(!result)
{
    printerror()
    CloseHandle(pipe);
    return;
}
numBytesWritten = 0;
printf("Sending %lg\n", *uPtrs[0]);
result = WriteFile(pipe, // handle to our outbound pipe
    uPtrs[0], // data to send
    sizeof(double),

    &numBytesWritten, // will store actual amount of data sent
    NULL // not using overlapped IO
    );
if (!result) {
    printerror()
}
CloseHandle(pipe);

收件人:

    while(true)
{
    if (!WaitNamedPipe(uniquePipeName, NMPWAIT_USE_DEFAULT_WAIT))
        continue;   // timeout, try again
    pipe = CreateFile(
        uniquePipeName,
        GENERIC_READ, // only need read access
        0,//FILE_SHARE_READ,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL
        );
    if (pipe != INVALID_HANDLE_VALUE)
        break;
    else
    {
        {
            if (GetLastError() == ERROR_PIPE_BUSY || GetLastError() == 2)
            {
                FormatMessage(
                    FORMAT_MESSAGE_ALLOCATE_BUFFER | 
                    FORMAT_MESSAGE_FROM_SYSTEM |
                    FORMAT_MESSAGE_IGNORE_INSERTS,
                    NULL,
                    GetLastError(),
                    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                    (LPTSTR) &lpMsgBuf,
                    0, NULL );
                printf("%s\n",lpMsgBuf);
                CloseHandle(pipe);
                if (!WaitNamedPipe(uniquePipeName, NMPWAIT_USE_DEFAULT_WAIT))
                    continue;   // timeout, try again
            }
            else
            {
                printf("Failed to connect pipe. Error %.i\n",GetLastError());
                y[0] = 0;
                return;
            }
            //system("pause");
        }
    }
}
printf("Connected!\n");

result = ReadFile(
    pipe,
    &buffer_in, // the data from the pipe will be put here
    sizeof(double),
    //        127 * sizeof(wchar_t), // number of bytes allocated
    &numBytesRead, // this will store number of bytes actually read
    NULL // not using overlapped IO
    );


printf("Recieved: %lg\n",buffer_in);
printf("Recieved: %lg\n", buffer_out);

y[0] = buffer_in;
CloseHandle(pipe);

提交失败时的错误代码是:GetLastError:535:管道的另一端有一个进程。

由于我对整个科目都是全新的,所以任何其他改善核心的都会受到赞赏。

非常感谢!

1 个答案:

答案 0 :(得分:2)

我知道这个问题来自 2014 年,但我遇到了同样的问题并找到了解决方案。 在 Microsoft 文档示例 (https://docs.microsoft.com/en-us/windows/win32/ipc/multithreaded-pipe-server) 中,您可以看到他们检查您获得的确切错误消息 (535),在这种情况下,fConnected 设置为 true 并且程序继续。所以我猜这个错误实际上是成功的?

fConnected = ConnectNamedPipe(hPipe, NULL) ? 
     TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);