簡體   English   中英

Windows 中的命名管道

[英]Named pipe in windows

我正在嘗試通過命名管道實現兩個進程之間的通信。 更准確地說(不要認為它會影響問題),我希望兩個 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 : 管道的另一端有進程。

由於我對整個主題完全陌生,因此感謝任何其他改進核心的建議。

非常感謝!

我知道這個問題來自 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM