CreateProcess子进程管道输出重定向
2021-08-30
107
0
这里时子进程的输出重客向到一个一个文件中,适用于子进程为控制台输出的程序。
SYSTEMTIME sys;
GetLocalTime(&sys);
SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES),NULL,TRUE };
char name[512] = { 0 };
sprintf_s(name, 512, "%4d-%02d-%02d %02d_%02d_%02d_%03d.txt", sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds);
cmdOutput = CreateFileA(name,
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
&sa, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (cmdOutput == INVALID_HANDLE_VALUE)
{
ExitProcess(0);
return 0;
}
PROCESS_INFORMATION pi;
STARTUPINFO si = { sizeof(STARTUPINFO) };
si.hStdInput = 0;
si.hStdOutput = cmdOutput;
si.dwFlags = STARTF_USESTDHANDLES;
if (CreateProcess(L"audio_recoder.exe", NULL, NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi))
{
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(cmdOutput);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}