C++ 从 CreateProcess() 获取 UTF-8 输出

标签 c++ winapi utf-8 createprocess

我无法让它工作,所以我从 CreateProcess() 获取 UTF-8 输出到 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;
}

我尝试了几件事,但没有成功。

感谢任何帮助!

最佳答案

命令的输出是一个字节流。所以你把它当作一个字节流来读。两个程序就使用的编码达成一致。

例如:


然后当您读取应用程序输出时,您使用约定的编码对字节流进行解码。例如。使用 MultiByteToWideChar function .

关于C++ 从 CreateProcess() 获取 UTF-8 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39007332/

相关文章:

C++ COM口的打开、读写

c++ - 如何限制用户只能选择某些驱动器?

html - 浏览器不使用 utf-8 但设置了元标记

Python ASCII 编解码器在写入 CSV 时无法编码字符错误

java string.getBytes ("UTF-8") 等效的 javascript

c++ - 可以删除(或默认)非特殊 C++ 成员函数吗?

c++ - 在 C/C++ 中避免内存泄漏的方法

winapi - 如何从隐藏窗口应用程序中显示对话框?

c++ - 在 dll 中保存额外的数据,他们是怎么做到的?

c++ - 使用 C++98 标准编写的代码可以用新的编译器编译吗?