How can I get an MFC application to block from the command line?

删除回忆录丶 提交于 2019-12-04 19:37:16
drescherjm

If you set your executable to be a console application with the linker option /SUBSYSTEM:CONSOLE the command line will block till the application exits. Remember that A console application can have a windows GUI.

Setting the linker setting /SUBSYSTEM:CONSOLE does have one problem if you do that as a linker setting you will have to adjust entry point to be main() instead of winmain. In the following thread there are a few a workarounds for that (thanks for Ulrich Eckhardt mentioning the entry point) : Visual Studio 2012 C++ Standard Output

There is also a second negative of this approach. If the program is not run from a console window the application will create a console window for you. This may confuse users.

You can't. EXEs are marked as either console or windows programs and if it's a Windows program control is handed over to the Windows manager and the console will keep running.

Your best bet is to create a small console app that calls CreateProcess to launch the Windows app and then simply WaitForSingleObject on the hProcess handle for it to finish.

More technical information on why it's not possible is available on the The Old New Thing blog here:

http://blogs.msdn.com/b/oldnewthing/archive/2009/01/01/9259142.aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!