Click here to Skip to main content
15,881,757 members
Articles / Programming Languages / C++
Article

Printf() debugging in a console window from within an ActiveX control

Rate me:
Please Sign up or sign in to vote.
3.26/5 (12 votes)
30 Aug 20051 min read 45.4K   19   7
Creating a console window from within an ActiveX control.

Introduction

OK, here's the deal... In the process of developing an ActiveX control for inclusion in a browser, I had developed a need to watch a large amount of data. It became very cumbersome to use the standard breakpoint debugging, and to make matters worse, the control was misbehaving on a remote machine.

Needing to be able to see the data easily, I decided to see if it was possible to create a console window from within the control. A web search didn't turn up anything as far as I could see so I decided to try a quick test.

As it turns out, it was quite simple to do. To do printf() debugging from within a control, all you need to do is modify your OnCreate() method as follows:

 LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
 {
  // TODO : Add Code for message handler.
  // Call DefWindowProc if necessary. 
  /* create a seperate console window for
     printf (print, println and debug) output */
  AllocConsole();
  freopen ("CONOUT$", "w", stdout ); 
  return 0;
 }

You'll also need to include the appropriate headers <stdio.h> and you should now be able to use printf() from anywhere in your code.

There are certainly other methods of tracing output... but this is guaranteed to be available and viewable on machines that don't have any tools installed.

Note: although I haven't done it, it should also be possible to remap stdin and take input from the console window should you need to implement a simple debug console.

Just another one for the toolkit... use it as you will.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralDebug Techniques Pin
Anonymous31-Aug-05 21:15
Anonymous31-Aug-05 21:15 
GeneralRe: Debug Techniques Pin
Stephen Muccione1-Sep-05 1:10
Stephen Muccione1-Sep-05 1:10 
Questionprintf or fprintf...? Pin
Anonymous30-Aug-05 3:52
Anonymous30-Aug-05 3:52 
AnswerRe: printf or fprintf...? Pin
Stephen Muccione30-Aug-05 14:57
Stephen Muccione30-Aug-05 14:57 
GeneralRe: printf or fprintf...? Pin
LudditeSD14-May-09 5:22
LudditeSD14-May-09 5:22 
Generalsome notes Pin
pullover30-Aug-05 3:04
pullover30-Aug-05 3:04 
1. The call "GetStdHandle (STD_OUTPUT_HANDLE);" should be not necessary
2. There is no output for standard error device (STD_ERROR_HANDLE)

<br />
AllocConsole();<br />
//GetStdHandle(STD_OUTPUT_HANDLE);<br />
freopen ("CONOUT$", "w", stderr); <br />
freopen ("CONOUT$", "w", stdout); <br />

GeneralRe: some notes Pin
Stephen Muccione30-Aug-05 3:39
Stephen Muccione30-Aug-05 3:39 

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.