   Code: Including a Dos Console In Your Win32 Environment - Long Post Naturally
  i dont mean for this post to be overkill but my previous thread might find itself on the next page soon and for those that are looking for this(being that i said i would post it)might think i never did plus for the ones that havent been on the forums in a while. i am glad to do this -- something i can contribute to the site for a change. i have commented the code mostly so i dont think i need to explain much. There is a link at the bottom to tell more in detail about what is going on if you are interested. **********************************************************#include <windows.h> // Header File For Windows#include <wincon.h>  // header using AllocConsole()#include <fcntL.h>   // _O_TEXT#include <io.h> // _open_osfhandle call in RedirectIOHandle Function/*this function is called by a virtual key, to allocate a  console within a GUI app, accepts input from the keyboard and allows cin/cout to be derived from stdinstdout and stderr default calls. */void RedirectIOToConsole(){int hConHandle;long lStdHandle;CONSOLE_SCREEN_BUFFER_INFO coninfo;FILE *fp;// allocate a console for this appAllocConsole();// set the screen buffer to be big enough to let us scroll textGetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);coninfo.dwSize.Y = MAX_CONSOLE_LINES;SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);// redirect unbuffered STDOUT to the consolelStdHandle = (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 consolelStdHandle = (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 consolelStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);fp = _fdopen( hConHandle, "w" );*stderr = *fp;setvbuf( stderr, NULL, _IONBF, 0 );// make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog // point to console as wellios::sync_with_stdio();}void keyBoardStuff(){  char ch;if (keys[VK_INSERT])    //going to add a console handle for user input{  RedirectIOToConsole(); //allocate a console for the GUI app cout << "Enter Number 1: ";   cin >> num1;  cout << endl << "Enter Number 2: ";cin >> num2;  cout << endl << "Enter Number 3: ";  cin >> num3;   cout << endl << "ECHO READ:    NUM1: " << num1 << "  NUM2: " << num2 << "  NUM3: " << num3 << endl << endl;   cout << "Are you sure you want to keep these values? Type Y for yes and N for no: ";   cin >> ch;  cout << endl;if(ch == 'y' || ch == 'Y')    console = FALSE;   if(console == FALSE)   {FreeConsole();    keys[VK_INSERT] = FALSE;   }outfile << "CONSOLE STATUS: " << console << endl;} //end if(keys[VK_INSERT]) } //end keyboard stuff..........a tidbit from WinMain(....)....DrawGLScene();// Draw The Scene  keyBoardStuff();   SwapBuffers(hDC);// Swap Buffers (Double Buffering).........***********************************************************the idea itself is pretty simple , just getting things in order and doing a little bit of testing is all it takes. i used a virtual key of choice to bring up the console but you can make it whatever you would like. basically AllocConsole() intiliazes a console window and FreeConsole() "Deallocates" it. The ReDirectIOTOConsole Function sets up the window to temporarily take control of the standard read and write calls such as stdin, cin, cout etc etc just like you were working with a console app. You can also use the console to read from file and write to a file although i did not include that in this code. i am currently working out the tweaks with that but this is a good start and a basic console interface. this is very useful for the end user(perhaps someone to interact at runtime with your OGL app but also a great help in debugging. Hey Rhino, or Khawk or mod it may be a good idea to paste this as a sticky -- many people have wondered how to do this and some said it even couldnt be done. if you would like to know more about AllocConsole() and FreeConsole then go to http://www.msdn.com and type in either of these in the search text box and you will be there! also you will find easily all the other functions that are possible with the explanation of each to let you do anything else once you have initilized your console. Like i said, as of yet i havent had time to get the code together, test it and post it as i have many other things to do here at work. i hope this will help many and maybe for some solve some of the nasty input and output bugs that have been a nuisance. **NOTE**To learn about what some of these calls do in more detail then go here to this site:http://dslweb.nwnexus.com/~ast/dload/guicon.htm [edited by - opengl_guru on February 4, 2004 4:56:09 PM]
  Or you could start a glut window from a console program and thus have an OpenGL window with the console in the background and just use cout to write to the console.  You can also change a console project into a Win32 project with on simple compiler directive in VC++6, although I can''t remember what that directive is right now. ;  Please, please, please try source or code tags.I nearly always create my projects as Win32 console applications. This has a couple upsides:1) Things actually compile and run in Linux without bitching about entry points2) I get a console, free of chargeBut it''s still a good tip, just add source tags... 