Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The problem of attachconsole

Tags:

c++

winapi

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.

like image 401
kyle Avatar asked Aug 16 '11 02:08

kyle


3 Answers

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.
like image 104
wilx Avatar answered Nov 20 '22 07:11

wilx


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

like image 2
Mike Fuchs Avatar answered Nov 20 '22 09:11

Mike Fuchs


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.

like image 1
Hulud Avatar answered Nov 20 '22 09:11

Hulud