如何封装OutputDebugString来使用

hanyj_3000 2011-07-11 08:41:06
不好意思,问个简单的问题,我想封装OutputDebugString来使用,我写的函数如下:

void MyOutputDebugString(WCHAR *Format,...)
{
WCHAR tempBuf[100] = {0};
DWORD time = GetTickCount();
swprintf(tempBuf,L"%d : ",time);
wcscat(tempBuf,Format);
wcscat(tempBuf,L"\n");

OutputDebugString(tempBuf);
}
main()
{
int i = 10;
MyOutputDebugString(L" i = %d",i);
}
我的目的是:1、打印事件发生的时间,2、把传进去的字串和值打印都输出到output窗口。
但是我像上面那样写函数,结果是“10299272 : i = %d”,那个i的值没出来。请问怎么回事。
...全文
306 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jungle_007 2011-07-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 kevin_perkins 的回复:]

何必要这么费劲,为什么不用CString?
C/C++ code

CString strDebug;
strDebug.Format(L"...", ...);
OutputDebugString(strDebug);
[/Quote]

LZ的方法比使用CString好多了,可重用,不需要庞大的MFC运行库
jernymy 2011-07-11
  • 打赏
  • 举报
回复
楼主可以参考一些可变参数的写法,规则等

以下仅供参考


// jernymy test
#include <stdio.h>
#include <stdarg.h>

#define MAX_BUF_SIZE 2048

void TestPnt(const char *pszFmt, ...)
{
va_list ptList;
char achBuf[MAX_BUF_SIZE] = {0};
int nBufLen;

va_start(ptList, pszFmt);
nBufLen = vsprintf(achBuf, pszFmt, ptList);
va_end(ptList);

// OutputDebugString(achBuf);
printf(achBuf);
printf("%s", achBuf);
}

int main(void)
{
int nIdx = 10;

TestPnt("nIdx:%d\n", nIdx);
return 0;
}


Kevin_Perkins 2011-07-11
  • 打赏
  • 举报
回复
何必要这么费劲,为什么不用CString?

CString strDebug;
strDebug.Format(L"...", ...);
OutputDebugString(strDebug);
healer_kx 2011-07-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lhcwjy 的回复:]

因为你用了可变参数,学习一下怎么访问可变参数吧
[/Quote]

Nod。
白云飘飘飘 2011-07-11
  • 打赏
  • 举报
回复
因为你用了可变参数,学习一下怎么访问可变参数吧

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧