Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

bslayerw

Open a console for debugging in Win 32 app

This topic is 6029 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, Newbie C++ clown here, can anyone point me to an article on how to open a console from a Win 32 app. I would like to have a "debugging" (windows console) console and be able to do regular output statements to the console window while my application is running. Unless anyway has a better way of debugging besides using a log file. Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
AllocConsole();

//windows output
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD written;
WriteConsole(h, "WriteConsole output\n", 20, &written, NULL);

//C output - include
//#include <,stdio.h>
freopen("CONOUT$", "w", stdout);
printf("Printf output\n");

//C++ output - include
//#include
ofstream conout;
conout.open("CONOUT$");
cout = conout;
cout << "cout output" << endl;

Henry
Lecturer in Computer Games Technology
University of Abertay Dundee
Scotland
UK

Share this post


Link to post
Share on other sites
Thank you KlePt0 for the code, it works beautifully. I like the fact that it''s wrapped in a class. Now I can make it a Singleton and use it in anywhere in my application.

henryx thanks for the reply too, could you explain what this piece of code does: "//C++ output - include
//#include
ofstream conout;
conout.open("CONOUT$");
cout = conout;
cout << "cout output" << endl; "

Will this open a console too?


Share this post


Link to post
Share on other sites
KlePt0:
is whatever you sent him really big or something? if so, could you email it to me too? or if not, just post it?
i was about to ask this question when i saw his posts here already

--- krez (krezisback@aol.com)

Share this post


Link to post
Share on other sites
Hi Klepto
I tried sending you an email but I don''t think that it went through...
I was just hoping you could provide me with the same code as
well. I''ve been looking everywhere for a good way to do debugging since I don''t have a terminal to use DX''s method, OutputDebug (?). I would appreciate any help!
My email address is <REMOVED>
Thank you very much!

Jason Arora
jason@pubism.com
http://www.pubism.com

[Edited by - JasonA on November 20, 2008 10:08:00 PM]

Share this post


Link to post
Share on other sites
#include
#include

/*
AllocConsole sets up standard input, standard output, and standard error handles for the new console. The standard input handle is a handle to the console''s input buffer, and the standard output and standard error handles are handles to the console''s screen buffer. To retrieve these handles, use the GetStdHandle function.
This function is primarily used by graphics applications to create a console window. Graphics applications are initialized without a console. Console applications are normally initialized with a console.
*/

AllocConsole();

/*
The GetStdHandle function returns a handle for the standard input, standard output, or standard error device. If STD_OUTPUT_HANDLE is used as the parameter, the handle to standard output is returned.
*/

HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);

/*
The WriteConsole function writes a character string to a console screen buffer beginning at the current cursor location. Have a look at MSDN
*/

DWORD written;
WriteConsole(h, "WriteConsole output\n", 20, &written, NULL);


/*
Reassigns stdout to the console standard output
*/


freopen("CONOUT$", "w", stdout);

/* now we can use it*/

printf("Printf output\n");

/*
ofstream is a template class which describes an object that controls insertion of data into a stream buffer. Here we make an instance called conout
*/
ofstream conout;

/*
open the stream for console standard output
*/

conout.open("CONOUT$");

/*
assign the output stream to the console output stream
/*
cout = conout;

/*now we can use it*/

cout << "cout output" << endl; "

Note that you don’t need any of the stuff after the above printf line if you are happy enough using the printf statements.

hope this helps, both certainly work fine for me.

henry

Share this post


Link to post
Share on other sites
ps.

I should have said that I put most of this code together from various bits of code that I got from you *GREAT* coders on this site.

cheers

henry

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!