Detecting Console

How can I detect if my program has been started from the console and then use that console?
closed account (DSLq5Di1)
A Windows application launched from the console? AttachConsole(ATTACH_PARENT_PROCESS).
http://msdn.microsoft.com/en-us/library/windows/desktop/ms681952

Redirecting std::cout,
http://blog.signalsondisplay.com/?p=85
I've tried that but it obviously doesn't treat cmd.exe as the parent process, the command returns 0.
That article is wrong (no need to AttachConsole after AllocConsole, as AllocConsole attaches it). Unfortunately, it is dealing with a different problem to mine; how to simply create a new console window to run alongside the current.
closed account (DSLq5Di1)
I'm a little lost here, from what I understood in your original post.. you wished to use the console your program was launched from? if that is the case,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <windows.h>
#include <iostream>
#include <fstream>

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd)
{
	if (AttachConsole(ATTACH_PARENT_PROCESS))
	{
		std::ofstream console_out("CONOUT$");
		std::cout.rdbuf(console_out.rdbuf());

		std::cout << "Testing 1..2..3.." << std::endl;

		FreeConsole();
	}
	return 0;
}


That article is wrong (no need to AttachConsole after AllocConsole, as AllocConsole attaches it). Unfortunately, it is dealing with a different problem to mine; how to simply create a new console window to run alongside the current.

Yes, you either allocate a new console, or attach to an existing one. If you simply want a new console, use AllocConsole(). http://msdn.microsoft.com/en-us/library/windows/desktop/ms681944
Topic archived. No new replies allowed.