如何使ASSERT象在VB那样直接中断

本文介绍了一种在VC++环境中自定义断言处理的方法,包括创建HwAssert.h头文件来定义新的断言宏,修改AFXASERT.CPP以实现自定义的断言处理函数,并调整DBGRPT.C以定制断言消息显示。通过示例展示了如何在对话框程序中使用自定义断言。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.1.1.1.          需要修改的文件

1.        自定义一个头文件 HwAssert.h

#ifndef HW_ASSERT_H

#define HW_ASSERT_H

 

// AFXAPI is used on global public functions

#ifndef AFXAPI

         #define AFXAPI __stdcall

#endif

 

extern "C" BOOL AFXAPI Hw_AfxAssertFailedLine(LPCSTR lpszFileName, int nLine);

 

#undef ASSERT

#define ASSERT(f) /

         do /

         { /

         if (!(f) && Hw_AfxAssertFailedLine(THIS_FILE, __LINE__)) /

                   AfxDebugBreak(); /

         } while (0) /

 

#endif        

 

2.        修改文件AFXASERT.CPP

AFXASERT.CPP 来自 E:/Program Files/Microsoft Visual Studio/VC98/MFC/SRC

#include "stdafx.h"

 

#ifdef _DEBUG   // entire file

 

#ifdef AFX_DBG1_SEG

#pragma code_seg(AFX_DBG1_SEG)

#endif

 

// NOTE: in separate module so it can replaced if needed

 

#ifdef _DEBUG

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

 

#ifdef _AFX_NO_DEBUG_CRT

LONG afxAssertBusy = -1;

LONG afxAssertReallyBusy = -1;

BOOL (AFXAPI* afxAssertFailedLine)(LPCSTR, int);

#endif

 

#ifndef _CRTIMP

#define _CRTIMP

#endif

 

extern "C" int __cdecl zq__CrtDbgReport(

        int,

        const char *,

        int,

        const char *,

        const char *,

        ...);

 

extern "C" BOOL AFXAPI Hw_AfxAssertFailedLine(LPCSTR lpszFileName, int nLine)

{

#ifndef _AFX_NO_DEBUG_CRT

         // we remove WM_QUIT because if it is in the queue then the message box

         // won't display

         MSG msg;

         BOOL bQuit = PeekMessage(&msg, NULL, WM_QUIT, WM_QUIT, PM_REMOVE);

         BOOL bResult = zq__CrtDbgReport(_CRT_ASSERT, lpszFileName, nLine, NULL, NULL);

         if (bQuit)

                   PostQuitMessage(msg.wParam);

         return bResult;

#else

         return TRUE;

#endif // _AFX_NO_DEBUG_CRT

}

#endif // _DEBUG

 

3.        修改文件 DBGRPT.C (来自VC安装盘/VC98/CRT/SRC

*       Copyright (c) 1988-1997, Microsoft Corporation. All rights reserved.

*

*Purpose:

*

*******************************************************************************/

//#include "stdafx.h"

#undef _CRTIMP

#define _CRTIMP

 

#ifdef _DEBUG

 

#include <CRTDBG.H> // vc 自带

#include <signal.h> // vc 自带

/*

#include <internal.h>

#include <mtdll.h>

#include <malloc.h>

#include <mbstring.h>

#include <stdarg.h>

#include <stdlib.h>

#include <stdio.h>

#include <dbgint.h>

#include <signal.h>

#include <string.h>

#include <awint.h>

*/

#ifdef _WIN32

 

#include <windows.h>

 

#define _CrtInterlockedIncrement InterlockedIncrement

        const char *

        );

 

 

_CRT_REPORT_HOOK _pfnReportHook;

 

//_CRTIMP long _crtAssertBusy = -1;

long _crtAssertBusy = -1;

 

int _CrtDbgMode[_CRT_ERRCNT] = {

#ifdef _WIN32

        _CRTDBG_MODE_DEBUG,

        _CRTDBG_MODE_WNDW,

        _CRTDBG_MODE_WNDW

_HFILE _CrtDbgFile[_CRT_ERRCNT] = { _CRTDBG_INVALID_HFILE,

                                    _CRTDBG_INVALID_HFILE,

                                    _CRTDBG_INVALID_HFILE

                                  };

 

 

//static const char * _CrtDbgModeMsg[_CRT_ERRCNT] = { "Warning", "Error", "Assertion Failed" };

static const char * _CrtDbgModeMsg[_CRT_ERRCNT] = { "Warning", "Error", "断言失败啦!" };

 

/***

*void _CrtDebugBreak - call OS-specific debug function

*

*Purpose:

*       call OS-specific debug function

*       else

*           return FALSE

*

*Exceptions:

*

*******************************************************************************/

int __cdecl zq__CrtDbgReport(

        int nRptType,

        const char * szFile,

        int nLine,

        const char * szModule,

        const char * szFormat,

        ...

        }

 

        if (_CrtDbgMode[nRptType] & _CRTDBG_MODE_WNDW)

        {

            char szLine[20];

 

            //retval = CrtMessageWindow(nRptType, szFile, nLine ? _itoa(nLine, szLine, 10) : NULL, szModule, szUserMessage);

            retval = _CRT_ASSERT;   // 要点就是这里了

            if (_CRT_ASSERT == nRptType)

                _CrtInterlockedDecrement(&_crtAssertBusy);

            return retval;

        }

 

        if (_CRT_ASSERT == nRptType)

 

1.1.1.2.          使用举例:

随便建立一个VC对话框工程ASSERTMINI,在ASSERTMINIDlg.cpp中加入如下代码:

#include "HwAssert.h"

BOOL CASSERTMINIDlg::OnInitDialog()

{

         CDialog::OnInitDialog();

 

         SetIcon(m_hIcon, TRUE);                       // Set big icon

         SetIcon(m_hIcon, FALSE);             // Set small icon

        

         // TODO: Add extra initialization here

         ASSERT(FALSE);

 

         return TRUE;  // return TRUE  unless you set the focus to a control

}

可以看到直接停在断言失败的那行代码ASSERT(FALSE);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值