Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

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

3.26/5 (12 votes)
30 Aug 20051 min read 1  
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