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);