0
目前我有兩個應用程序,一個使用GUI(使用MFC編寫),另一個作爲標準可執行文件。 GUI應用程序(父級)使用CreateProcessW
調用觸發標準應用程序(子級),父級通過匿名管道從其子級接收消息。當我在VS IDE中運行父代時,消息接收過程正常工作。但是,如果我運行父代獨立,父代不會從其子代收到任何消息(即父代在ReadFile調用中掛起,等待消息)。ReadFile在VS C++ IDE中工作良好,但獨立運行失敗
對此有何看法?
注意:創建匿名管道後,所有讀取操作都在單獨的線程內發生,並且不會阻塞UI或主線程。下面給出了一些與子進程創建和使用參數相關的代碼。
// pipe creation code
SECURITY_ATTRIBUTES saAttr;
// Set the bInheritHandle flag so pipe handles are inherited.
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))
{
m_logger->log(__FILE__, __LINE__, EventSeverity::WARNING, "Pipe cannot be created, will not receive meassages from child processes");
}
// Ensure the read handle to the pipe for STDOUT is not inherited.
if (! SetHandleInformation(m_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
{
m_logger->log(__FILE__, __LINE__, EventSeverity::WARNING, "Could not make the read handler of the anonymous pipe not-inheritable");
}
SetStdHandle(STD_OUTPUT_HANDLE, m_hChildStd_OUT_Wr);
SetStdHandle(STD_ERROR_HANDLE, m_hChildStd_OUT_Wr);
//Child process creation code
m_startupInfo.lpDesktop = NULL;
m_startupInfo.lpReserved = NULL;
m_startupInfo.lpReserved2 = NULL;
m_startupInfo.lpTitle = NULL;
m_startupInfo.hStdError = GetStdHandle(STD_OUTPUT_HANDLE);
m_startupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
m_startupInfo.dwFlags = STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
m_startupInfo.cb = sizeof(m_startupInfo);
// launch the executable
m_isExecuting = CreateProcessW(app.exe, // lpApplicationName
m_pwszParam, // lpCommandLine
0, // lpProcessAttributes
0, // lpThreadAttributes
TRUE, // bInheritHandles
CREATE_NEW_PROCESS_GROUP, // dwCreationFlags
NULL, // lpEnvironment
curent working directory // lpCurrentDirectory
&m_startupInfo, // lpStartupInfo
&m_processInfo // lpProcessInformation
);
子進程是否實際運行?也許當前目錄是錯誤的。你在做什麼錯誤檢查? –
@JonathanPotter是的,它運行。我可以在任務管理器中觀察它。 – Suranjith
我做了進一步的測試,我可以看到的是,當父母運行獨立的孩子寫入數據到控制檯,而不是重新定向他們(因爲讀取線程在父母等待數據)。但在IDE內部,這工作正常。非常受歡迎! – Suranjith