C++ 从 CreateProcess() 获取 UTF-8 输出 [英] C++ Getting UTF-8 output from CreateProcess()

查看:50
本文介绍了C++ 从 CreateProcess() 获取 UTF-8 输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让它工作,所以我从 CreateProcess() 得到 UTF-8 输出到 wstring.

I can't make it work so I get UTF-8 output from CreateProcess() into wstring.

目前我正在运行此方法来执行此操作,但没有 UTF-8 输出:

Currently I am running this method to do that but without UTF-8 output:

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;
}

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

I tried several things but did not succeed in making it working.

感谢任何帮助!

推荐答案

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

The output of a command is a byte stream. So you read it as a byte stream. It's up to the two programs to agree on the encoding to use.

例如:

  • If you execute a .NET (C#/VB.NET) console application, the application can use the Console.OutputEncoding, to set the encoding the Console.Write[Line] method will use.

Console.OutputEncoding = Text.Encoding.UTF8;

  • 类似地,PowerShell 脚本可以使用 [Console]::OutputEncoding 来设置 Write-OutputWrite-Host cmdlet 将使用.

  • Similarly, a PowerShell script can use the [Console]::OutputEncoding, to set the encoding the Write-Output or Write-Host cmdlets will use.

    [Console]::OutputEncoding = [Text.Encoding]::UTF8
    

  • cmd.exe 或批处理文件可以使用 chcp 命令.

  • The cmd.exe or a batch file can use the chcp command.

    chcp 65001
    

  • Win32 应用程序可以使用 SetConsoleOutputCP 函数 设置其输出编码,如果它使用 WriteConsole.如果应用程序使用 WriteFile,它只需要编写已经按照需要编码的字节(例如使用 WideCharToMultiByte).

    SetConsoleOutputCP(CP_UTF8);
    

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

    When you then read the application output, you decode the byte stream using the agreed encoding. E.g. using the MultiByteToWideChar function.

    这篇关于C++ 从 CreateProcess() 获取 UTF-8 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆