简体   繁体   English

如何从命令行阻止 MFC 应用程序?

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

I modified an MFC example for OpenCascade, adding some functionality (it was the HLR example).我为 OpenCascade 修改了一个 MFC 示例,添加了一些功能(这是 HLR 示例)。 The application uses the document/view architecture, with document class doing most of the work.该应用程序使用文档/视图架构,文档类完成大部分工作。

Some of the new functions don't require a GUI, so the program exits before the GUI is opened, which I perform by calling exit(0) from a CDocument specialization.一些新函数不需要 GUI,因此程序在打开 GUI 之前退出,我通过从 CDocument 专业化调用exit(0)来执行此操作。

My problem is, for our workflow, the MFC application will be called from the Windows command line.我的问题是,对于我们的工作流程,将从 Windows 命令行调用 MFC 应用程序。 As soon as it's called, it returns control back to the shell and continues merrily along in the background, whether it opens a GUI or not.一旦它被调用,它就会将控制权返回给 shell,并在后台愉快地继续,无论它是否打开 GUI。 What I need the application to do is to block from the command line, whether the GUI is open or not.我需要应用程序做的是阻止命令行,无论 GUI 是否打开。

I've been reading up on CWinApp, and CMDIFrameWnd, but if you can make your application block from the command line, I can't figure out how to do it.我一直在阅读 CWinApp 和 CMDIFrameWnd,但是如果您可以从命令行创建应用程序块,我不知道该怎么做。

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.如果您使用链接器选项 /SUBSYSTEM:CONSOLE 将可执行文件设置为控制台应用程序,则命令行将阻塞,直到应用程序退出。 Remember that A console application can have a windows GUI.请记住,控制台应用程序可以具有 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.设置链接器设置 /SUBSYSTEM:CONSOLE 确实有一个问题,如果您将其作为链接器设置进行设置,则必须将入口点调整为 main() 而不是 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在以下线程中,有一些解决方法(感谢 Ulrich Eckhardt 提到入口点): 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. EXE 被标记为控制台或 Windows 程序,如果它是 Windows 程序,则控制被移交给 Windows 管理器,控制台将继续运行。

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.最好的办法是创建一个小型控制台应用程序,该应用程序调用CreateProcess来启动 Windows 应用程序,然后只需在 hProcess 句柄上使用WaitForSingleObject即可完成。

More technical information on why it's not possible is available on the The Old New Thing blog here:有关为什么不可能的更多技术信息,请访问 The Old New Thing 博客:

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

If you want to block an MFC app (or windowed app, in general) and output to the console (you'll need to AttachConsole() or AllocConsole() first), do your work in InitInstance (or an equivalent method), wait for any threads to complete in ExitInstance, and then run your program from the command line using "start /WAIT <your app> <your options>".如果您想阻止 MFC 应用程序(或窗口应用程序,通常)并输出到控制台(您需要先使用 AttachConsole() 或 AllocConsole()),请在 InitInstance(或等效方法)中进行工作,等待用于在 ExitInstance 中完成的任何线程,然后使用“start /WAIT <your app> <your options>”从命令行运行您的程序。 You don't need to write a special console app that waits... start does that already.您不需要编写一个特殊的控制台应用程序来等待…… start 已经做到了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM