Sign in to follow this  
makingroulette

console window in a windows application

Recommended Posts

does anyone know how to create a console window, in a windows application, so that its easy to see data output while program s running if anyone knows another way of easily outputting text, numbers etc let me know it would help a lot

Share this post


Link to post
Share on other sites
Have you considered creating your program in console mode from the beginning? Using WinMain has no real advantages (for me at least), and you can still create windows, etc. if you use main instead.

Share this post


Link to post
Share on other sites
Quote:
Original post by DJBnJack
AllocConsole()? Used to work for me but stopped recently, not sure why.


AllocConsole won't attach the C run-time stdout and stderr to the new console. I think the Windows standard handles are attached, though. So, you'll need to change stdout and stderr appropriately. I had some code somewhere that did this... have to find it again....

[edit] Found the code:


HANDLE h_stdout;
int _stdout;

AllocConsole();

h_stdout = GetStdHandle( STD_OUTPUT_HANDLE );
_stdout = _open_osfhandle( h_stdout, 0 );
*stdout = *(_fdopen( _stdout, "r+" ));
setvbuf( stdout, NULL, _IONBF, 0 ); // sets to unbuffered - might not want this

Share this post


Link to post
Share on other sites
I don't remember where I got this from


bool GDebugConsole::open(GString &title)
{
if (isOpened())
return false;

if (!AllocConsole())
return false;

if (title.empty())
title = "G Engine Debug Console";

CONSOLE_SCREEN_BUFFER_INFO conInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &conInfo);
conInfo.dwSize.Y = 500;
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), conInfo.dwSize);

intptr_t stdHandle = reinterpret_cast<intptr_t>(GetStdHandle(STD_OUTPUT_HANDLE));
int conHandle = _open_osfhandle(stdHandle, _O_TEXT);

FILE* file = _fdopen(conHandle, "w");
*stdout = *file;
setvbuf(stdout, NULL, _IONBF, 0);

stdHandle = reinterpret_cast<intptr_t>(GetStdHandle(STD_INPUT_HANDLE));
conHandle = _open_osfhandle(stdHandle, _O_TEXT);
file = _fdopen(conHandle, "r");
*stdin = *file;
setvbuf(stdin, NULL, _IONBF, 0);

stdHandle = reinterpret_cast<intptr_t>(GetStdHandle(STD_ERROR_HANDLE));
conHandle = _open_osfhandle(stdHandle, _O_TEXT);
file = _fdopen(conHandle, "w");
*stderr = *file;
setvbuf(stderr, NULL, _IONBF, 0);

ios::sync_with_stdio();
SetConsoleTitle(title.getBuffer());

opened = true;

return true;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Axiverse
Have you considered creating your program in console mode from the beginning? Using WinMain has no real advantages (for me at least), and you can still create windows, etc. if you use main instead.



say huh? Do you mean by windows as using message boxes? :-)

You most certainly do need WinMain if you are building a Windows API application
as it is required by the linker. It passes in fundamentally necessary information to setup any kind of Win32/Windows API application.

Are you using some other GUI library on top of Win32/Windows API?

Share this post


Link to post
Share on other sites
Quote:
Original post by qesbit
You most certainly do need WinMain if you are building a Windows API application
as it is required by the linker. It passes in fundamentally necessary information to setup any kind of Win32/Windows API application.


Try this experiment. Create an empty console project. Find some code for a generic window app - nothing fancy, just register window class, create window, message loop - basic window app. Copy and paste the code for the basic window app into the empty console project. Put the code from WinMain into main. Use GetModuleHandle(NULL) whereever the hInstance for the program is needed. Supply a menu rc file and so on as needed - again nothing fancy. Compile and launch. Report back to us your findings.

Share this post


Link to post
Share on other sites
Thanks for the advice, Ive just copied code from Windows app to a console app. The hInstance variable is no longer used in creating a window, that was my main concern in making a console app with a window.

I was using DirectX API to build a 3D roulette computer game.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this