简体   繁体   中英

The problem of attachconsole

I need to make a windows GUI application can run in console, so I attach the console to the process when the application is invoked with a command line. However, after the application exit, the console prompt with the path doesn't show unless the ENTER key is pressed. Is there any way that the prompt with the path can directly show up without pressing the enter key? Thanks.

Short answer: This is not possible.

Long answer: Well, it is sort of possible if you are willing to relax your requirements a little bit. You basically have three options:

  1. What you have done already. You can attach GUI application to a console but cmd.exe will not wait for your application to exit.
  2. Create a GUI application and open console in the GUI application. The console will only last as long as you application.
  3. Or you can restructure your application/source a bit and provide two executables, GUI one that starts GUI directly, another that is console executable.

In C#, I use SendKeys.SendWait("{ENTER}"); to do that. I think in C++, the keybd_event function does something similar.

Like Autodesk Maya with MayaBatch, you can build a small console application which basically run your GUI application with CreateProcess and wait with WaitForSingleObject.

You will have to use this "batch" version of your application in the console.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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