如何在Win32中的两个子进程之间设置管道?

时间:2010-11-30 04:53:38

标签: c++ winapi pipe

对于我的生活,我无法弄清楚为什么这不起作用。基本上,我创建了继承位设置为true的管道,并创建了两个子进程,并使用STARTUPINFO结构来设置输入和输出句柄,但管道似乎已损坏(第二个进程没有输出到控制台,即使输出是预期的)

我知道问题不在于我的测试程序(BitTwiddler.exe),因为我使用CMD.exe执行了相同的操作,一切都按预期工作。

以下是我所拥有的最小复制品。我做错了什么?

#include "windows.h"

int main()
{
    PROCESS_INFORMATION piSource, piDest;
    HANDLE hPipeIn, hPipeOut;
    HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    STARTUPINFOW suSource, suDest;
    ZeroMemory(&suSource, sizeof(suSource));
    ZeroMemory(&suDest, sizeof(suDest));
    suSource.cb = suDest.cb = sizeof(STARTUPINFOW);
    suSource.dwFlags = suDest.dwFlags = STARTF_USESTDHANDLES;
    SECURITY_ATTRIBUTES sa;
    sa.nLength = sizeof(sa);
    sa.lpSecurityDescriptor = 0;
    sa.bInheritHandle = TRUE;
    if (CreatePipe(&hPipeIn, &hPipeOut, &sa, 0) == 0)
    {
        return GetLastError();
    }
    suSource.hStdInput = hIn;
    suSource.hStdError = suSource.hStdOutput = hPipeIn;
    suDest.hStdInput = hPipeOut;
    suDest.hStdError = suDest.hStdOutput = hOut;
    std::wstring cmdLineA(L"BitTwiddler 1"), cmdLineB(L"BitTwiddler 0");
    cmdLineA.push_back(0); cmdLineB.push_back(0);
    if (CreateProcessW(0, &cmdLineA[0], 0, 0, TRUE, 0, 0, 0, &suSource, &piSource) == 0)
    {
        return GetLastError();
    }
    CloseHandle(piSource.hThread);
    if (CreateProcessW(0, &cmdLineB[0], 0, 0, TRUE, 0, 0, 0, &suDest, &piDest) == 0)
    {
        return GetLastError();
    }
    CloseHandle(piDest.hThread);
    HANDLE hArray[2];
    hArray[0] = piSource.hProcess;
    hArray[1] = piDest.hProcess;
    WaitForMultipleObjects(2, hArray, TRUE, INFINITE);
    CloseHandle(hArray[0]);
    CloseHandle(hArray[1]);
    return 0;
}

(如果有人感兴趣,BitTwiddler是:

#include <windows.h>
#include <sstream>
#include <iostream>
#include <string>

int main(int argc, char *argv[])
{
    std::size_t opt = 0;
    argc--; argv++;
    if (argc == 0)
    {
        return 0;
    }
    else
    {
        std::istringstream converter(*argv);
        converter >> opt;
    }
    switch(opt)
    {
    case 0:
        {
            std::wstring currentLine;
            while(std::getline(std::wcin, currentLine))
            {
                std::wcout << "Got somepin: " << currentLine << std::endl;
            }
        }
        break;
    case 1:
        for (;;)
        {
            std::wcout << L"Hello World!" << std::endl;
            Sleep(1000);
        }
        break;
    case 2:
        return -1;
    default:
        std::wcout << "Unknown option.";
        return 0;
    }
    return 0;
}

),但我认为这并不重要。

1 个答案:

答案 0 :(得分:5)

你错放了读写结束:)

CreatePipe有原型

BOOL CreatePipe(
  PHANDLE hReadPipe,  // can only read from this
  PHANDLE hWritePipe,  // can only write to this
  LPSECURITY_ATTRIBUTES lpPipeAttributes,
  DWORD nSize
);

您不能从只写句柄调用ReadFile(或在您的情况下为std :: getline),反之亦然。如果您通过简单的std::getline来电替换了ReadFile来电,那么您会收到ACCESS_DENIED错误,并确认此事实,因为子进程中的STD_INPUT_HANDLE未被打开GENERIC_READ

修复方法如下:

suSource.hStdError = suSource.hStdOutput = hPipeOut;  // must be the write pipe!
suDest.hStdInput = hPipeIn;  // must be the read pipe.

也许你指定的名字令人困惑。如果按照形式参数调用它们,则错误会更清楚:

suSource.hStdError = suSource.hStdOutput = hReadPipe;  // clearly wrong.
suDest.hStdInput = hWritePipe;  // as above -- expects a read-handle.