Home
Tutorials
Code Snippets
Code Samples
Downloads
Links

The Blog
Our Projects
About
Contact

::Add RageStorm to Favorites!::

The Blog | Our Projects | Guest Book | About | Contact

 
Code Snippet - Console for Win32API App
Author:Arkon
Category:Win32API
File Size:694 Bytes
Uploaded at:22-Jan-03 07:11:10 pm
Description:
  You may now add a console window to your (GUI!) application which in there you can show information (debugging/results...).
  
#include <windows.h>
#include <io.h>
#include <stdio.h>
#include <fcntl.h>

// Create a console window for out Win32API app
if (AllocConsole() == 0) {
 MessageBox(0, "no console created", "Error", 0);
 return 1;
}

// Now this is the hack:
// We link the Windows STD_OUTPUT_HANDLE to the DOS stdout handle:
int hCrt = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
*stdout = *_fdopen(hCrt, "w");
setvbuf(stdout, NULL, _IONBF, 0);


//The old fashion way:
printf("Hello Console,");
//woohoo it works!

char *buf = "using Win32API now.";
DWORD wr = 0;
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, strlen(buf), &wr, NULL);

// Cleanup
FreeConsole();
User Contributed Comments(2)
 [1] Peter Bloomfield | 2005-12-22 15:19:13

You can use the same method to associate the standard input with the console as well. After the "setvbuf..." line, put the following:

hCrt = _open_osfhandle((long)GetStdHandle(STD_INPUT_HANDLE), _O_TEXT | _O_RDONLY );
*stdin = *_fdopen(hCrt, "r");


You can then, for example, use the input stream, like this:

char buffer[1024];
cin >> buffer;
cout << "You said: " << buffer;
 [2] A Y | 2006-08-17 14:51:18

This code is a life saver! :D
NOTE:
Comments that will hurt anyone in any way will be deleted.
Don't ask for features, advertise or curse.
If you want to leave a message to the author use the contacts,
if you have any question in relation to your comments please use the forum.
Comments which violate any of these requests will be deleted without further
notice. Use the comment system decently.

Post your comment:
Name:
email:
Comment:
::Top 5 Tutorials::
Embedded Python[117057]
2D Rotated Rectangles Collision Detection[88982]
Keyboard Hook[77321]
UDP[65824]
HTTP Proxy[41213]

::Top 5 Samples::
2D ColDet Rotated Rectangles[11561]
PS2 Mouse Driver[6958]
Wave Format Player[5792]
Reading FAT12[5620]
CodeGuru[5359]


All rights reserved to RageStorm © 2009