两个函数,输出重定向以及转换编码,想用的拿去用,思路就是这样,拿去自己封装函数即可
void OutputRedirection()
{
PROCESS_INFORMATION pi = {0};
STARTUPINFO si = {0};
si.cb = sizeof(si);
HANDLE hStdInRead = 0;
HANDLE hStdOutWrite = 0;
SECURITY_ATTRIBUTES seAttr;
seAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
seAttr.bInheritHandle = TRUE;
seAttr.lpSecurityDescriptor = NULL;
if(!CreatePipe(&hStdInRead, &hStdOutWrite, &seAttr, 0))
{
return;
}
if(!SetHandleInformation(hStdInRead, HANDLE_FLAG_INHERIT, 0))
{
return;
}
si.hStdError = hStdOutWrite;
si.hStdOutput = hStdOutWrite;
si.hStdInput = hStdInRead;
si.dwFlags |= (STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW);
si.wShowWindow = SW_HIDE;
BOOL bSuc = CreateProcess(L"Exe",
L"CmdLine",
NULL,
NULL,
TRUE,
0,
NULL,
NULL,
&si,
&pi);
if(bSuc)
{
CloseHandle(pi.hThread);
DWORD dwRet = WaitForSingleObject(pi.hProcess, 3000);
DWORD dwRead;
char szbuf[4096] = {0};
BOOL bPeek = PeekNamedPipe(hStdInRead, szbuf, 4096, &dwRead, NULL, NULL);
if(!bPeek)
{
return;
}
// szBuf = OutPut
}
}
std::wstring Utf82Unicode(std::string _str)
{
std::wstring strOut = L"";
int nLen = (int)_str.length();
int nResult = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)_str.c_str(), nLen, NULL, NULL);
if(nResult == 0)
{
return L"";
}
strOut.resize(nResult, L' ');
MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)_str.c_str(), nLen, (LPWSTR)strOut.c_str(), nResult);
return strOut;
}