简体   繁体   中英

Running a console application from the command prompt without it launching a new console window

Is there a way to prevent a console application from launching a new console window if it is executed from the command prompt? I don't know why I've never noticed this before (or if I just haven't been annoyed by it before) but I would like to be able to open a command prompt, navigate to where my console application (.exe) is and run it without it launching a new console window.

I've seen threads about how to hide the console window but my application isn't really meant to run in an automated fashion so if they run it from Explorer, I would like them to see it.

A console application, when run from an existing command-line, will not create a new console window: Windows will re-use the existing window.

PE executables (ie .exe files) are marked with a flag which tells the OS if it's a console application or not: if it is marked as a console application then Windows will re-use the console window used to launch it, or create a new window if it was launched from a GUI process (such as Windows Explorer). In .NET applications, this flag is controlled by the Application Type drop-down in Project Properties (where it lists either "Windows Application", "Console Application", or "Class Library").

If your program is creating a new window, then it probably has the wrong flag set OR the program code is manually calling AllocConsole which will create a new console window regardless of how the process was created or the PE console flag.

Update There is one other reason: When a console application runs under a different privilege level than its (non-elevated) parent console window, then UAC will require it to be in its own window.

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