另类调试方法
由于需要在没有安装VC的PC上进行调试,所以在没有IDE环境的情况下TRACE宏无法工作,而使用 MessageBox 并不是一个很好的方法。另一种简单的方法是向Console中输出文本。 在你的应用中增加如下代码: #ifdef _DEBUG FILE* __fStdOut = NULL; HANDLE __hStdOut = NULL; #endif void startConsoleWin(int width=80, int height=25, char* fname = NULL); void startConsoleWin(int width, int height, char* fname) { //创建一个Console窗口,指明宽度和高度,如果fname不为空则同时将输出写 入一个文件。 #ifdef _DEBUG AllocConsole();//分配 SetConsoleTitle("Debug Window"); __hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);//指明句柄为标准输出HANDLE COORD co = {width,height}; SetConsoleScreenBufferSize(__hStdOut, co);//指明缓冲区大小 if(fname) __fStdOut = fopen(fname, "w"); #endif } int wprintf(char *fmt, ...) { //类似于printf的函数,向Console写入文本 #ifdef _DEBUG char s[300]; va_list argptr; int cnt; va_start(argptr, fmt); cnt = vsprintf(s, fmt, argptr); va_end(argptr); DWORD cCharsWritten; if(__hStdOut)// 写Console WriteConsole(__hStdOut, s, strlen(s), &cCharsWritten, NULL); if(__fStdOut) fprintf(__fStdOut, s); return(cnt); #else return 0; #endif } 在CWinApp::InitInstance(...)中调用 startConsoleWin(...),然后你可以在需要输出的时候使用wprintf(....) 代替 TRACE 宏。 本文出自 51CTO.COM技术博客 |


goodgoodstudy
博客统计信息
热门文章
最新评论
友情链接