在 Visual Studio 中捕获控制台应用程序的输出
我有一个简单的控制台应用程序,在 Visual Studio 2010 中为我提供了一些预构建功能。它继续生成其他子进程(尽管等待这些子进程的终止)。
当我在 Visual Studio 外部运行该应用程序时,它的所有消息, 我在构建窗口中看不到任何输出,
有人知道这是为什么吗
?
等,出现在控制台窗口(即标准输出)中,但是当我在 VS 下运行这个应用程序时, :经过进一步检查,我已设法显示应用程序的标准输出,但没有出现子进程的标准输出有什么想法吗?
我使用以下代码重新定向子进程的标准输出:
STARTUPINFO si;
GetStartupInfo( &si );
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdError = GetStdHandle( STD_ERROR_HANDLE );
si.hStdInput = GetStdHandle( STD_INPUT_HANDLE );
si.hStdOutput = GetStdHandle( STD_OUTPUT_HANDLE );
PROCESS_INFORMATION pi;
// Create the process.
if ( !CreateProcess( applicationName.GetCStr(), cmd.CStr(), NULL, NULL, TRUE, 0, NULL, workingDir.GetCStr(), &si, &pi ) )
{
// Failed to create process!!
return false;
}
I have a simple console app providing me with some pre-build functionality in Visual Studio 2010. It goes on to spawn other child processes (though waits for the termination of those child processes.
When I run the app outside visual studio all its messages, etc, appear in the console window (ie stdout). However when I run this app under VS then I don't see any of the output in the build window.
Does anybody know why this is?
Its very annoying.
Edit: On further inspection I have managed to get the stdout of my app to appear but the stdout of the child processes are not appearing. Any ideas?
I re-direct the stdout of the child processes using the following code:
STARTUPINFO si;
GetStartupInfo( &si );
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdError = GetStdHandle( STD_ERROR_HANDLE );
si.hStdInput = GetStdHandle( STD_INPUT_HANDLE );
si.hStdOutput = GetStdHandle( STD_OUTPUT_HANDLE );
PROCESS_INFORMATION pi;
// Create the process.
if ( !CreateProcess( applicationName.GetCStr(), cmd.CStr(), NULL, NULL, TRUE, 0, NULL, workingDir.GetCStr(), &si, &pi ) )
{
// Failed to create process!!
return false;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您需要为子进程创建显式句柄以将它们连接起来。 MSDN 有一篇(或多篇)相关文章,请参阅 例如http://msdn.microsoft.com/en-us/library/ms682499%28v=vs.85%29.aspx。
子进程继承开放对象句柄,但有自己的标准输入/标准输出等。句柄,除非您通过适当的环路来显式地为其创建它们。无论如何,这是我的理解;阅读文章了解更多信息。
I believe you need to make explicit handles for the child process to hook them up. MSDN has an article on it (or several), see http://msdn.microsoft.com/en-us/library/ms682499%28v=vs.85%29.aspx for example.
Child processes inherit open object handles, but have their own stdin/stdout/etc. handles, unless you go through the proper hoops to create them explicitly for it. That's my understanding, anyway; read the article for more info.