How can I check if my program was launched from the command line or from windows explorer?

A program doesn't know how it was launched. Another program could have launched it by calling one of several C functions, such as CreateProcess(), system(), ShellExecute(), etc. Or it could be launched by some other non-C program using functions available to it for that purpose.

I'm not sure if this will work all the time. But I managed to get what I want by checking the return of AttachConsole when my program starts up, it returns true if my program was started from the Command Line, and false otherwise. The goal of this was to use the console my program was started in, and if I didn't have one, create a new one.

This is the code I ended up with if anyone's interested:

if(!AttachConsole(-1)) { 
		if (!AllocConsole()) {
			printf ("Couldn't initialize console!");
                        exit (1);
		}
		hasparentprocess = 0;
	} else {
		hasparentprocess = 1;
	}
commented: Demonstrates a fierce determination to find the answers in the face of harsh criticism. Willing to share and contribute information that is actually useful. +5

>>I'm not sure if this will work all the time

It won't. console programs launched from Windows Explorer will always have a console unless the program itself deletes its own console. MS-Windows GUI programs never have a console.

Ok, thanks for your help. I decided to just always go with AllocateConsole since my program is a windows gui program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.