使用Printf()在控制台窗口中调试activex控件
文章来自:
http://www.codeproject.com/Articles/11420/Printf-debugging-in-a-console-window-from-within-a
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.
在开发浏览器使用的activex控件的过程中,我需要监测许多数据。使用标准断点会变得很笨重,而且事情会变得更糟糕,控件在远程的机器上会表现异常。
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:
结果证明,它是很容易实现的。为了可以在控件中使用printf(),你所需要的就是按照如下修改oncreate():
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.
你应该包括<stdio.h>并且可以在任何地方使用printf()函数。
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.
注释:虽然我没有试验,它应该可以重映射stdin并且可以从控制台窗口获取输入信息。
Just another one for the toolkit... use it as you will.