getting a printf to work in a VC++ standard win32 app

ElDonAntonio

Senior member
Aug 4, 2001
967
0
0
I'm trying to debug a piece of my code, and outputting a message would really be useful to me. Unfortunately I created my app in VC++ by specifying "Win32 Application", so I have no console, and printf's seems to get lost in noman's land.
My interface is created with qt, I guess I could create a popup window instead of a printf but that would be too cumbersome. Anyone knows a way to do this?

Thanks!
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
As you've noted, if you're creating a win32 application, you'll have an entry point in your program for WinMain(), and not main(). If you're using WinMain(), Windows does not create a console for you, thus you don't have a true stdout (there are ways around this using CreateProcess() and the STARTUPINFO struct).

For a win32 application, you can use MessageBox() to create the popup window you want.

Generally, debugging by popping up windows, or printing info to the console is secondary to the more powerful debugging facilities you have in VC++. You can step through the code, or stop at a breakpoint and see/change the values of variables in scope, so why the need to popup windows?

Good luck!
 

ElDonAntonio

Senior member
Aug 4, 2001
967
0
0
Hi Descartes,

yeah, of course I know of VC++'s debugging capabilities, but my application is a 3D Modeler & Raytracer, and I've got a problem in my rendering algorithm, I suspect some objects are being rendered several times. So the problem is, if I try and put a breakpoint in my rendering functions, it'll work, but as soon as I re-run the program, my app's window will try to appear and it'll call again the rendering functions. So I have no easy way of determining whether render() is being called in the same "pass" or whether it's being called because the window is trying to get displayed.

Nothing like a good printf in a case like this one...it's in those moments I dearly miss a Linux environment :) i'll try the popup, thanks!
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Any GUI program can dynamically create a console by using the Win32 API function AllocConsole().

Just do this,

(1) In the program initialization: call AllocConsole() (see MSDN doc for details. No params required)

(2) Save the output handle for the console using: GetStdHandle(STD_OUTPUT_HANDLE);

(3) To write any text to the console, use the function WriteConsole() (see MSDN doc for parameters)

(4) When done with the console, call FreeConsole()


It's as simple as that! Win32 == Programmer Heaven :)
 

thornc

Golden Member
Nov 29, 2000
1,011
0
0
Look into:
AllocConsole
GetStdHandle
WriteConsole
and others!!
perhaps you could create a printf wrap function, with the above functions!!
 

Adrian Tung

Golden Member
Oct 10, 1999
1,370
1
0
Just wondering, if you want to see messages while running your program, why don't you just use OutputDebugString and debug your program in windowed mode?


:)atwl
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
you could always write text to a log file, though it might get a little long :)

if your number of "objects" is reasonable you could create (in #ifdef _DEBUG code) a flag array, set it to zero at the start of each "pass", set to 1 when rendered, set a breakpoint if try to render and already =1.