【发布时间】:2010-10-12 13:38:44
【问题描述】:
我正在为我们的产品编写一个 win32 实用函数,它需要通过 shell 调用任意程序并记录其输出。我们通过将标准输出从子进程重定向到管道来做到这一点:
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
CreatePipe(&hReadPipe, &hWritePipe, &saAttr, 0);
// Redirect the first process stdout to our write pipe
// so that we can read its output from the read pipe.
startUpInfo.dwFlags = STARTF_USESTDHANDLES;
startUpInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
startUpInfo.hStdOutput = hWritePipe;
startUpInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
CreateProcessA(NULL, szCmdLine, NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &startUpInfo[i], &procInfo);
可以通过这种方式调用的程序种类繁多,其中许多不受我们控制。目前,我们看到许多程序的输出在第一个字符之后似乎被截断的问题——通常是 WCHAR 字符串被错误地用作 CHAR 的确定信号。
如何判断子进程是作为 CHAR 还是 WCHAR 写入其 stdout 管道?
【问题讨论】: