visual studio 2010 - ReadFile在VS c++ IDE中工作得很好,但在独立时就失败了
visual studio 2010 - ReadFile works fine inside VS C++ IDE but fails stand-alone
目前我有两个应用程序,一个带有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
);
解决了这个问题。以前,我正在将父进程的输出和错误处理程序更新到新创建的处理程序,以便在创建子进程时检索它们。然而,这似乎不起作用。当我修改代码以通过指针或引用传递管道处理程序时,事情开始正常工作。
但是,这并不能解释为什么在IDE中运行并且在独立模式下失败。
//set the handler
SetStdHandle(STD_OUTPUT_HANDLE, m_hChildStd_OUT_Wr);
SetStdHandle(STD_ERROR_HANDLE, m_hChildStd_OUT_Wr);
//get the handler
m_startupInfo.hStdError = GetStdHandle(STD_OUTPUT_HANDLE);
m_startupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
相关文章:
- 我编写了代码将十进制分数转换为其二进制等效数.它编译得很好,但在执行时挂起
- 很好的语法来获取对向量/数组数据的大小引用?
- 我认为我的代码很好,但它在 cin a 之后停止并且没有进一步?
- 尽管一切看起来都很好,但值不会交换
- 为什么不同类型层次结构的指针之间的dynamic_cast定义得很好?
- 在 C++11 中利用 int*_t、int_fast*_t 和 int_least*_t 之间的差异的一个很好的例子是
- 对于短字符串来说,这是一个很好的哈希函数吗?
- 为什么指标有时效果很好,有时效果不佳?写下霍夫曼代码
- Red Hat:使用<atomic>编译很好,但链接器找不到__atomic_store_16;什么库?
- 使用移位的无符号数字作为数组的索引号是一种很好的做法
- 是否很好地使用状态模式来维护当前选定的对象?
- C++|以一种很好的方式将树(不一定是二进制的)打印到stdout
- 静态库单独使用很好,但在引用时会抛出错误
- 如果验证容器的大小并在同一条件语句下访问元素,这是很好的做法吗?
- 可变参数函数模板不能很好地使用 std::function 作为参数
- 有没有办法关闭文件? fclose 不能很好地工作
- 引用的静态强制转换强制模板实例化,其中不完整的类型很好
- libc++:为什么流关闭后仍然很好
- 为许多类可能需要的所有常量变量制作独立的头文件是否是一种很好的做法?
- visual studio 2010 - ReadFile在VS c++ IDE中工作得很好,但在独立时就失败了