CreateProcess管道始终为空

时间:2019-02-13 20:12:27

标签: c++ winapi uwp

我正在尝试为C ++中的C#UWP应用程序生成一个.dll。我想使用Pipes获取CreateProcess命令的输出。

结果应该是.jar文件的consoleoutput,但是我得到的是一个空字符串。也许问题是STARTF_USESTDHANDLES标志无法添加。

根据此链接(https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/ns-processthreadsapi-_startupinfoa),此常量的值为“ 0x00000100”,因此我尝试使用256,但它没有任何改变。

HANDLE rPipe, wPipe;

//Create pipes to write and read data
CreatePipe(&rPipe, &wPipe, NULL, 0);

STARTUPINFO sInfo;
ZeroMemory(&sInfo, sizeof(sInfo));
PROCESS_INFORMATION pInfo;
ZeroMemory(&pInfo, sizeof(pInfo));
sInfo.cb = sizeof(sInfo);
//sInfo.dwFlags = STARTF_USESTDHANDLES;
sInfo.dwFlags = 256;
sInfo.hStdInput = NULL;
sInfo.hStdOutput = wPipe;
sInfo.hStdError = wPipe;

//if (!SetHandleInformation(wPipe, HANDLE_FLAG_INHERIT, 0))
    //return "Error SetHandleInformation";

//Create the process here.
string cmd = "java -jar decode.jar \"" + bitString + "\"";

// Assumes std::string is encoded in the current Windows ANSI codepage
int bufferlen = ::MultiByteToWideChar(CP_ACP, 0, cmd.c_str(), cmd.size(), NULL, 0);

if (bufferlen == 0)
{
    // Something went wrong. Perhaps, check GetLastError() and log.
    CloseHandle(wPipe);
    CloseHandle(rPipe);
    return "errorBuffer";
}

// Allocate new LPWSTR - must deallocate it later
LPWSTR cmdLine = new WCHAR[bufferlen + 1];

::MultiByteToWideChar(CP_ACP, 0, cmd.c_str(), cmd.size(), cmdLine, bufferlen);

// Ensure wide string is null terminated
cmdLine[bufferlen] = 0;

if (!CreateProcessW(L"C:\\Program Files (x86)\\Java\\jdk1.8.0_161\\bin\\java.exe", cmdLine, 0, 0, FALSE, NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW, 0, 0, &sInfo, &pInfo)) {
    CloseHandle(wPipe);
    CloseHandle(rPipe);
    return "errorProcess";
}


WaitForSingleObject(pInfo.hProcess, INFINITE);
CloseHandle(wPipe);

//now read the output pipe here.

char buf[100];
DWORD reDword;
string m_csOutput;
BOOL res;
do
{
    res = ReadFile(rPipe, buf, 100, &reDword, 0);
    m_csOutput += buf;
} while (res);

CloseHandle(rPipe);

return m_csOutput.c_str();

0 个答案:

没有答案