Menu

Windows GUI with Consol...

2003-09-10
2012-09-26
  • Nobody/Anonymous

    Background, i'm developing a new app...windows gui, and well I want and would like to use a console for things like printf, cout etc. So after some searching I found the following code, originally released from microsoft in 97 or something. I changed it so it would compile with Dev-C++...no hard...just header file problems.

    **************************************
    CODE FOLLOWS

    #include <windows.h>

    #include <process.h>
    #include <errno.h>
    #include <stdio.h>
    #include <fcntl.h>
    #include <io.h>

    #include <iostream>
    #include <stdlib.h>
    //#include <fstream.h>

    #define MAX_CONSOLE_LINES 255;
    HANDLE  g_hConsoleOut; // Handle to debug console
    extern void RedirectIOToConsole();

    // This function dynamically creates a "Console" window and points stdout and stderr to it.
    // It also hooks stdin to the window
    // You must free it later with FreeConsole

    void RedirectIOToConsole()
    {   
       int   hConHandle;   
       long  lStdHandle;   
       CONSOLE_SCREEN_BUFFER_INFO coninfo;
       FILE  *fp;   
      
       // allocate a console for this app   
       AllocConsole();   
      
       // set the screen buffer to be big enough to let us scroll text
       GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&coninfo);   
       coninfo.dwSize.Y = MAX_CONSOLE_LINES; 
      
       // How many lines do you want to have in the console buffer   
       SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),coninfo.dwSize);   
      
       // redirect unbuffered STDOUT to the console   
       g_hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);   
       lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);   
       hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);   
       fp = _fdopen( hConHandle, "w" );   
       *stdout = *fp;   
       setvbuf( stdout, NULL, _IONBF, 0 );   
      
       // redirect unbuffered STDIN to the console   
       lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);   
       hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);   
       fp = _fdopen( hConHandle, "r" );   
       *stdin = *fp;   
       setvbuf( stdin, NULL, _IONBF, 0 );   
      
       // redirect unbuffered STDERR to the console   
       lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);   
       hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);   
       fp = _fdopen( hConHandle, "w" );   
       *stderr = *fp;   
       setvbuf( stderr, NULL, _IONBF, 0 );       
      
       SetConsoleTitle("The Console Titlebar Text");          
      
       // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog    
       // point to console as well  Uncomment the next line if you are using c++ cio or comment if you don't   
      
       std::ios::sync_with_stdio();
    }

     
    • aditsu

      aditsu - 2003-09-10

      ...what problems?

      here's my compile log for your code:

      Compiler: Default compiler
      Building Makefile: "F:\work\cpp\tests\12\Makefile.win"
      Finding dependencies for file: F:\work\cpp\tests\12\test.cpp
      Executing  make...
      make.exe -f "F:\work\cpp\tests\12\Makefile.win" all
      g++.exe -c test.cpp -o test.o -I"C:/progs/Dev-Cpp/include/c++"  -I"C:/progs/Dev-Cpp/include/c++/mingw32"    -pedantic -Wall -W -Wno-long-long -fmessage-length=0
      g++.exe test.o  -o "Project1.exe" -L"C:/progs/Dev-Cpp/lib"
      C:/progs/Dev-Cpp/lib/libmingw32.a(main.o)(.text+0x7f):main.c: undefined reference to `WinMain@16'
      make.exe: *** [Project1.exe] Error 1
      Execution terminated

      Adrian

       
    • Nobody/Anonymous

      You app is trying to link and can not find main()
      Use Dev-C++ to create a windows app. Create another file and drop this code in. Create a header file if you want or declare "extern void RedirectIOToConsole();" in main.

      Insert "RedirectIOToConsole();" function call in main after variable declaration. Do windows stuff...You should see a console window if you run, with all cout going there.
      When finished call "FreeConsole()"
      return from main...finished over and out.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.