8

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.

kyle
  • 99
  • 1
  • 3
  • http://blogs.msdn.com/b/oldnewthing/archive/2009/01/01/9259142.aspx – Ian Boyd Aug 16 '11 at 02:14
  • 1
    It has already displayed the prompt, right when your program started running. You'll have to make it wait. Use start /wait yourapp.exe. If that's not what you want then you'll need to use AllocConsole(). – Hans Passant Aug 16 '11 at 02:18
  • I had the same issue in golang, and thought that there's somewhere a bug / resource leak, and that the cmd was actually locked-up. Turns out that the prompt was actually printed earlier and everything was normal. – maja Jun 29 '18 at 08:14

3 Answers3

7

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.
wilx
  • 17,697
  • 6
  • 59
  • 114
  • 1
    Thanks for your answer. Whilst researching this I found another option where you launch the application using the [start command](http://www.computerhope.com/starthlp.htm). e.g. `start /wait application.exe paramaters`. – Dennis Aug 28 '12 at 09:16
2

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

Mike Fuchs
  • 12,081
  • 6
  • 58
  • 71
1

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.

Hulud
  • 69
  • 4