API编程 查看Debug信息的方法
//Debug其实主要是处理变参函数,然后发送数据到另一个Debug窗口
//Dlg.cpp
#include "DebugData.h"
void DebugDlg(LPTSTR lpDebugInfo, ...)
{
//处理参数信息
va_argList *pList = new va_argList;
if(pList == NULL)
return;
BOOL bSetNode = FALSE;
char sMsg[1024];
memset(sMsg, 0, 1024);
char *pStr = lpDebugInfo;
int i = 0;
while(*pStr != '/0')
{
if(*pStr == '%')
{
pStr++;
if(*pStr != '%')
{
i++;
bSetNode = TRUE;
}
if(*pStr == '')
break;
}
if(bSetNode)
{
switch(*pStr)
{
case 'd':
case 'x':
case 'o':
case 'c':
case 's':
{
va_argNode *pNode = new va_argNode(D);
pList->Add(pNode);
}
bSetNode = FALSE;
break;
case 'f':
{
va_argNode *pNode = new va_argNode(F);
pList->Add(pNode);
}
bSetNode = FALSE;
break;
default:
break;
}
}
pStr++;
}
va_list args;
va_start(args, lpDebugInfo);
va_argNode *pNode = pList->GetHead();
for(int j=0; j<i; j++)
{
va_argNode *pNextNode = pList->GetNext(&pNode);
if(pNextNode != NULL)
{
if(pNextNode->m_nType == D)
pNextNode->m_pData = (int *)va_arg(args, int *);
else
pNextNode->m_DData = (double)va_arg(args, double);
}
}
int nTokey = 0;
char *pOldPos = lpDebugInfo;
pStr = lpDebugInfo;
int nOldIndex = 0;
pNode = pList->GetHead();
for(j=0; j<strlen(lpDebugInfo); j++)
{
if(pStr[j] == '%')
{
j++;
if(pStr[j] == '%')
j++;
for(int nKey=0; ; nKey++)
{
char sKey = pStr[j+nKey];
if(sKey == '%')
{
j += nKey;
break;
}
if(sKey == '')
{
j += nKey;
break;
}
}
char sTmp1[1024];
memset(sTmp1, 0, 1024);
char sTmp2[1024];
memset(sTmp2, 0, 1024);
memcpy(sTmp1, pOldPos, j-nOldIndex);
if(nTokey < i)
{
va_argNode *pNextNode = pList->GetNext(&pNode);
if(pNextNode != NULL)
{
if(pNextNode->m_nType == D)
sprintf(sTmp2, sTmp1, pNextNode->m_pData);
else
sprintf(sTmp2, sTmp1, pNextNode->m_DData);
}
nTokey++;
}
strcat(sMsg, sTmp2);
pOldPos = &pStr[j];
nOldIndex = j;
j--;
}
}
if(strlen(sMsg) <= 0)
sprintf(sMsg, lpDebugInfo);
va_end( args );
if(pList != NULL)
{
delete pList;
pList = NULL;
}
//在Debug窗口中显示信息,创建一个主窗口类名为"DebugWnd"的TestAP,显示信息
HWND hDebugWnd = FindWindow("DebugWnd", NULL);
if(hDebugWnd != NULL)
{
COPYDATASTRUCT cd;
cd.dwData = 0;
cd.cbData = strlen(sMsg) + 1;
cd.lpData = sMsg;
SendMessage(hDebugWnd, WM_COPYDATA, NULL, LPARAM(&cd));
}
}
调用方法:
DebugDlg("currnet=%d %s time=%d f=%2.4f 16=(0x)%04x 16=(0o)%o 4=(02d)%02d %s", nCurSel, sLocal, time, 2.3f, 16, 16, 4, "OK");
//以下是 DebugData.h/DebugData.cpp
//DebugData.h
enum dataType{D, F};
struct va_argNode
{
public:
va_argNode(dataType type);
dataType m_nType;
union {
int *m_pData;
double m_DData;
};
struct va_argNode *m_pNextNode;
};
class va_argList
{
public:
va_argList();
virtual ~va_argList();
void Add(va_argNode *pNew);
va_argNode *RemoveHead();
va_argNode *GetHead();
va_argNode *GetNext(va_argNode **pNode);
protected:
private:
struct va_argNode *m_pArgList;
};
//DebugData.cpp
va_argNode::va_argNode(dataType type)
{
m_nType = type;
if(m_nType == D)
m_pData = 0;
else
m_DData = 0.0;
m_pNextNode = NULL;
}
va_argList::va_argList()
{
m_pArgList = NULL;
}
va_argList::~va_argList()
{
while(m_pArgList != NULL)
{
va_argNode *pNode = m_pArgList;
m_pArgList = m_pArgList->m_pNextNode;
delete pNode;
}
}
void va_argList::Add(va_argNode *pNew)
{
if(pNew == NULL)
return;
if(m_pArgList == NULL)
{
m_pArgList = pNew;
return;
}
va_argNode *pNode = m_pArgList;
while(pNode->m_pNextNode != NULL)
{
pNode = pNode->m_pNextNode;
}
pNode->m_pNextNode = pNew;
}
va_argNode *va_argList::RemoveHead()
{
va_argNode *pNode = m_pArgList;
if(m_pArgList != NULL)
m_pArgList = m_pArgList->m_pNextNode;
return pNode;
}
va_argNode *va_argList::GetHead()
{
va_argNode *pNode = m_pArgList;
return pNode;
}
va_argNode *va_argList::GetNext(va_argNode **pNode)
{
va_argNode *pNext = *pNode;
if(*pNode != NULL)
*pNode = (*pNode)->m_pNextNode;
return pNext;
}
//以下是DebugWnd 及显示的实现
//MainFrm.cpp
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_WM_COPYDATA()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
// if( !CFrameWnd::PreCreateWindow(cs) )
// return FALSE;
#ifdef WIN32
WNDCLASS m_WndClass;
m_WndClass.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
m_WndClass.lpfnWndProc = AfxWndProc;
m_WndClass.cbClsExtra = 0;
m_WndClass.cbWndExtra = 0;
m_WndClass.hInstance = AfxGetInstanceHandle();
m_WndClass.hIcon = theApp.LoadIcon(IDR_MAINFRAME);
m_WndClass.hCursor = ::LoadCursor(NULL,IDC_ARROW);
m_WndClass.hbrBackground = NULL;
m_WndClass.lpszMenuName = NULL;
m_WndClass.lpszClassName = "DebugWnd";
RegisterClass(&m_WndClass);
cs.lpszClass = m_WndClass.lpszClassName;
#endif
return CFrameWnd::PreCreateWindow(cs);
}
BOOL CMainFrame::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
CTestControlView *pView = (CTestControlView *)GetActiveView();
if(pView != NULL)
{
int nInfo = pCopyDataStruct->cbData;
if(pView->m_pInfo != NULL)
{
delete []pView->m_pInfo;
pView->m_pInfo = NULL;
}
char *pInfo = new char[nInfo];
strcpy(pInfo, (char *)pCopyDataStruct->lpData);
pView->m_pInfo = pInfo;
pView->Invalidate();
}
return CFrameWnd::OnCopyData(pWnd, pCopyDataStruct);
}
//testView.h
char *m_pInfo;
//testView.cpp
void CTestControlView::OnDraw(CDC* pDC)
{
CTestControlDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CString sMsg(m_pInfo);
pDC->TextOut(20, 20, sMsg);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论