【发布时间】:2015-06-08 02:03:20
【问题描述】:
目前我有两个应用程序,一个带有 GUI(使用 MFC 编写),另一个作为标准可执行文件。 GUI 应用程序(父级)使用CreateProcessW 调用触发标准应用程序(子级),并且父级通过匿名管道从其子级接收消息。当我在 VS IDE 中运行父级时,消息接收过程工作正常。但是,如果我独立运行父级,则父级不会收到来自其子级的任何消息(即父级在 ReadFile 调用中挂起,等待消息)。
对此有什么想法吗?
注意:创建匿名管道后,所有读取操作都发生在单独的线程中,它不会阻塞 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 是的,它运行。我可以在任务管理器中观察到。
-
我对此做了进一步的测试,我可以看到,当父级运行独立时,子级将数据写入控制台而不是重新定向它们(因此父级中的读取线程挂起等待数据)。但在 IDE 内部这工作正常。很奇怪!!!!
标签: c++ visual-studio-2010 mfc pipe inter-process-communicat