Microsoft example consider both program written from the same developer. In my case I was launching a generic program which output on stdout/stderr. I changed the Microsoft example in this way.
The following function run a generic externalProgram
with command line arguments
:
HANDLE m_hChildStd_OUT_Rd = NULL;
HANDLE m_hChildStd_OUT_Wr = NULL;
HANDLE m_hreadDataFromExtProgram = NULL;
HRESULT RunExternalProgram(std::string externalProgram, std::string arguments)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
SECURITY_ATTRIBUTES saAttr;
ZeroMemory(&saAttr, sizeof(saAttr));
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
// Create a pipe for the child process's STDOUT.
if (!CreatePipe(&m_hChildStd_OUT_Rd, &m_hChildStd_OUT_Wr, &saAttr, 0))
{
// log error
return HRESULT_FROM_WIN32(GetLastError());
}
// Ensure the read handle to the pipe for STDOUT is not inherited.
if (!SetHandleInformation(m_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
{
// log error
return HRESULT_FROM_WIN32(GetLastError());
}
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.hStdError = m_hChildStd_OUT_Wr;
si.hStdOutput = m_hChildStd_OUT_Wr;
si.dwFlags |= STARTF_USESTDHANDLES;
ZeroMemory(&pi, sizeof(pi));
std::string commandLine = extProgram + " " + arguments;
// Start the child process.
if (!CreateProcessA(NULL, // No module name (use command line)
(TCHAR*)commandLine.c_str(), // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
TRUE, // Set handle inheritance
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
return HRESULT_FROM_WIN32(GetLastError());
}
else
{
m_hreadDataFromExtProgram = CreateThread(0, 0, readDataFromExtProgram, NULL, 0, NULL);
}
return S_OK;
}
If CreateProcessA
succedeed, an external thread will be launched to asynchronousvly capture the output from the launched program. The code executed from the external thread is the following one:
DWORD __stdcall readDataFromExtProgram(void * argh)
{
DWORD dwRead;
CHAR chBuf[BUFSIZE];
BOOL bSuccess = FALSE;
for (;;)
{
bSuccess = ReadFile(m_hChildStd_OUT_Rd, chBuf, BUFSIZE, &dwRead, NULL);
if (!bSuccess || dwRead == 0) continue;
// Log chBuf
if (!bSuccess) break;
}
return 0;
}
I omitted any closing condition on all HANDLE
and threads involved in the code.