Command Line from Windows API


So, my question is a bit different from most. I'm not trying to open a cmd window or make the program start with a cmd window. I want my program to just open a window when the executable is clicked, but output from the console when the executable is run from the command line. I was wondering if that is possible? I know that it is possible in Linux.

check if GetConsoleWindow() is null...

null =====> windows (explorer / double click on exe)
not null =====> cmd

not sure thought give it a try and report back!
I don't understand your question at all. Its just gibberish to me. You want to open a window, but not open a window. You want to open a console, but not open a console, or maybe sometimes, but not all the time.
I think he wants to know if the app is running from command line to behave differently depending on this.
In Linux every program can use stdin and stdout, be it GUI or not, but on windows this behaviour needs to be emulated.
Last edited on
@freddie1 @moderan

Thanks Moderan! That is exactly what I wanted to say. I had a long day and was in a hurry so I wasn't able to put what I wanted into words.


I believe that this will work! Thanks. I'll let you know when I implement it!

Hmm, I tried to implement it, but it isn't exactly what I wanted. It determines if the program has a console window attached to it. It doesn't know if the program was executed from the console.


Maybe I can restate exactly what I am looking for to make my question a bit more clear. I am writing a windows application using winapi. When I compile the executable, the executable can be run from two ways. The first way is by double clicking on the file. The second way is by executing the file from the command line.

I want to make it so that when I run the program from the command line, it uses that to output messages.


This is the "solution" that I devised. If this is the only "solution" it's not worth doing at all IMHO.

	GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
	if ((csbi.dwCursorPosition.X == 0) &&
		(csbi.dwCursorPosition.Y == 0)) {
Last edited on
The reason that you can't really do it is because of the way that Windows loads the applications with explorer. You assume that there is a way for the program to know that explorer ran it, whereas AFAIK, explorer is simply a wrapper around the OS itself, the same as the console providing a different interface. Therefore, running from the console gives no differences than running from a normal system. As such, your option is the only one that would really work, though of course due to its hackish nature can have problems (e.g. user types 'cls && ./myprog.exe' or the like). The other option is to have two different programs to run, one from the console and one from explorer. These programs could just be wrappers, and the actual program functionality is provided by a DLL shared between the two programs.
Thanks for the information! I guess that the best way to solve this then is to compile one that opens the console and another one that doesn't. It's a little disappointing.

I think you are right. I got the program to output the command that executed the executable. When opening it with explorer it used its full path, but when using cmd it was just the executable. However, both were just paths to the exe. I think that you are right in that there is no way for the program to know.
try this :

i think its a step in the right direction
but its early morning so i could by wrong
I'd use a standard windows GUI application and use AllocConsole with ATTACH_PARENT_PROCESS when the app was invoked from command line. I think stdin and stdout must be redirected too.

if (!AttachConsole(ATTACH_PARENT_PROCESS)) {
    // GUI
} else {
    // Console, and you have a handle to the console that already exists.

I believe cmd.exe sets the CMDCMDLINE and CMDEXTVERSION environemntal variables when it starts. So if these are set your program was most probably started from a shell.
Topic archived. No new replies allowed.