53. FAQ o Win API

Q> As in GUI application to open the console and to assign stdin, stdout, stderr? 

#include <windows.h>  
#include <stdio.h> 
#include <io.h> 
#include <fcntl.h> 

BOOL CreateConsole (void) 
{ 
  FreeConsole ();//just in case 
  if (AllocConsole ()) 
   { 
	 int hCrt = _open_osfhandle ((long) 
     GetStdHandle (STD_OUTPUT_HANDLE), _O_TEXT); 
	 *stdout = * (:: _fdopen (hCrt, "w")); 
     :: setvbuf (stdout, NULL, _IONBF, 0); 
     *stderr = * (:: _fdopen (hCrt, "w")); 
     :: setvbuf (stderr, NULL, _IONBF, 0); 
	 return TRUE; 
    }return FALSE; 
}

int APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
                     LPSTR lpCmdLine, int nCmdShow) 
{ 
CreateConsole (); 
printf ("WinMain with Console test\n"); 

MSG msg; 
    while (GetMessage (&msg, NULL, 0, 0)) 
    { 
       TranslateMessage (&msg); 
       DispatchMessage (&msg);  
    }
   return TRUE; 
}

2000 (c) DM