Monday, August 25, 2014

Attaching a console in Win32

how to get a console in a few simple lines of code in windows:

DWORD pid;
pid = GetCurrentProcessId();
AllocConsole();
AttachConsole(pid);
freopen("CON", "w", stdout);
printf("hello, i am the console\n");


update:
secure version:

DWORD pid;
pid = GetCurrentProcessId();
AllocConsole();
AttachConsole(pid);
FILE *streamfp = NULL;
freopen_s(&streamfp, "CON", "w", stdout);
printf("hello, i am the console\n");