【发布时间】:2026-01-12 05:05:02
【问题描述】:
我无法让它工作,所以我将 UTF-8 输出从 CreateProcess() 获取到 wstring。
目前我正在运行此方法,但没有 UTF-8 输出:
HANDLE g_hChildStd_OUT_Rd = NULL;
HANDLE g_hChildStd_OUT_Wr = NULL;
HANDLE g_hChildStd_ERR_Rd = NULL;
HANDLE g_hChildStd_ERR_Wr = NULL;
PROCESS_INFORMATION CreateChildProcess(void);
void ReadFromPipe(PROCESS_INFORMATION);
string run(char *command){
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
if ( ! CreatePipe(&g_hChildStd_ERR_Rd, &g_hChildStd_ERR_Wr, &sa, 0) ) {
exit(1);
}
if ( ! SetHandleInformation(g_hChildStd_ERR_Rd, HANDLE_FLAG_INHERIT, 0) ){
exit(1);
}
if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &sa, 0) ) {
exit(1);
}
if ( ! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0) ){
exit(1);
}
char *szCmdline=command;
PROCESS_INFORMATION piProcInfo;
STARTUPINFO siStartInfo;
bool bSuccess = FALSE;
ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.hStdError = g_hChildStd_ERR_Wr;
siStartInfo.hStdOutput = g_hChildStd_OUT_Wr;
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
bSuccess = CreateProcess(NULL,
szCmdline, // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
CREATE_NO_WINDOW, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo); // receives PROCESS_INFORMATION
CloseHandle(g_hChildStd_ERR_Wr);
CloseHandle(g_hChildStd_OUT_Wr);
if ( ! bSuccess ) {
exit(1);
}
DWORD dwRead;
CHAR chBuf[BUFSIZE];
bool bSuccess2 = FALSE;
std::string out = "", err = "";
for (;;) {
bSuccess2=ReadFile( g_hChildStd_OUT_Rd, chBuf, BUFSIZE, &dwRead, NULL);
if( ! bSuccess2 || dwRead == 0 ) break;
std::string s(chBuf, dwRead);
out += s;
}
dwRead = 0;
for (;;) {
bSuccess2=ReadFile( g_hChildStd_ERR_Rd, chBuf, BUFSIZE, &dwRead, NULL);
if( ! bSuccess2 || dwRead == 0 ) break;
std::string s(chBuf, dwRead);
err += s;
}
return out;
}
我尝试了几件事,但没有成功。
感谢任何帮助!
【问题讨论】:
-
您为什么希望子进程输出 UTF8 ? Windows 上的 fyi std::wstring 通常用于 UTF16。
-
当使用 CreateProcess() 执行命令时会打印出一些字符,如 č,ć,ž,所以这就是为什么我需要 wstring。
-
它们很可能是您需要确定的代码页上的 MBCS。
-
管道处理原始字节,而不是字符。您遇到问题的输出中的原始字节实际上是什么样的?如果您在此处发布字节以及您期望的字符串输出,那么有人可能会帮助识别正在使用的编码。
-
我得到了这样的输出:prntscr.com/c7982a,但应该是这样的:prntscr.com/c7989y
标签: c++ winapi utf-8 createprocess