Click here to Skip to main content
13,254,366 members (62,468 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


23 bookmarked
Posted 18 Jul 2011

Good Old & Dirty printf() Debugging in a Non-console C/C++ Application or DLL

, 25 Jul 2012
Rate this:
Please Sign up or sign in to vote.
How to open a console in a non-console C/C++ application or DLL and make printf/scanf work (stdin/stdout/stderr related functions)


Don't expect magic, and your dreams coming true by reading this short and dirty hack. Still this tip serves the answer to a question that I sometimes see here and there on the forums. As the subtitle says, this hack opens a console window in an application that otherwise doesn't have one, and makes printf()/scanf() and other <stdio.h> related functions work! I know that sophisticated programs use logging systems, so use that if you have one. I used this solution only once, when I wrote an injectable DLL (w-mode) that was injected into a game (starcraft), and I wanted the DLL to open a console window inside the guest process for logging. I did this in C++ only in debug builds, and used some logging macros that were preprocessed to printf() in debug mode and to nothing in release mode. That time, I used Visual C++ 6, but I tested it with Visual C++ 2005 and 2008 as well and it worked. So here is the code:

freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
You should put this somewhere to the beginning of the entrypoint function of your application or DLL (in your WinMain() or DllMain()). And my debug macros for newer (.NET) Visual Studio versions that support vararg macros:
#ifdef _DEBUG
# define Debug(fmtstr, ...) printf(fmtstr, ##__VA_ARGS__)
# define Debug(fmtstr, ...)

Put the above code to a global place, for example to your stdafx.h.

You could achieve the same by logging into a file, and by using a log watcher program that has its window open all the time and detects your logfile recreations/appendings like "tail -f" command of a linux/cygwin. Still, if you don't have a nice log watcher program or you just want to log out something in guest code and then undo your changes, this dirty hack might do the job.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralReason for my vote of 5 Good to know. I hope I never need i... Pin
TRK325-Jul-11 13:22
memberTRK325-Jul-11 13:22 
GeneralReason for my vote of 5 Sometimes nothing beats a well-place... Pin
Harold Bamford20-Jul-11 13:47
memberHarold Bamford20-Jul-11 13:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 25 Jul 2012
Article Copyright 2011 by pasztorpisti
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid