2
Contributors
4
Replies
6
Views
5 Years
Discussion Span
Last Post by tomtetlaw
Featured Replies
  • 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 … Read More

0

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.

Edited by Ancient Dragon: n/a

1

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;
	}
Comments
Demonstrates a fierce determination to find the answers in the face of harsh criticism. Willing to share and contribute information that is actually useful.
0

>>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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.