使用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():
Collapse | Copy
Code
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.