1. 更改AfxWinMain函数
// stdafx.cpp : source file that includes just the standard includes // MyMainFunc.pch will be the pre-compiled header // stdafx.obj will contain the pre-compiled type information #include "stdafx.h" #include <eh.h> #include <process.h> #include <iostream.h> #include <string.h> #include <windows.h> #include <conio.h> FILE* fCrashDump = 0; EXCEPTION_DISPOSITION __cdecl _except_handler( struct _EXCEPTION_RECORD *ExceptionRecord, void * EstablisherFrame, struct _CONTEXT *ContextRecord, void * DispatcherContext ) { bool _bFind = false; DWORD Ebp = ContextRecord->Ebp; DWORD m_nAddress[100]={0}; DWORD p; int k; int i; int j = 0; __asm { mov ebx, Ebp; mov eax, [ebx+ 4]; mov ecx, j; imul ecx, 4; mov k, ecx; lea ecx, m_nAddress; add ecx, k; mov dword ptr [ecx], eax; inc j; mov i,0; jmp bb aa: mov edx,i; add edx,1; mov i,edx; bb: cmp i,100; jge cc; mov eax, [ebx]; mov ebx, eax; mov p, eax; mov eax, [eax + 4]; mov ecx, j; imul ecx, 4; mov k, ecx; lea ecx, m_nAddress; add ecx, k; mov dword ptr [ecx], eax; inc j; mov eax, p; mov eax, [eax-4]; cmp eax, 'JMCX'; jne aa; mov eax, p; mov eax, [eax - 8]; cmp eax, 'AFXW'; jne aa; mov _bFind, 1; cc: nop; } if (fCrashDump) { if (_bFind) fprintf(fCrashDump, "sig: i find it, 0X%X, 0X%X/n", 'JMCX', 'AFXW'); for (i = 0; i < 100; i++) { int k = m_nAddress[i]; if (k == 0) break; fprintf(fCrashDump, "0x%x/r/n", k); } } return ExceptionContinueSearch; } int AFXAPI xxx(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { DWORD _sig = 'JMCX'; DWORD _funno = 'AFXW'; DWORD handler = (DWORD)_except_handler; __asm { // Build EXCEPTION_REGISTRATION record: push handler; // Address of handler function push FS:[0]; // Address of previous handler mov FS:[0],ESP; // Install new EXECEPTION_REGISTRATION } ASSERT(hPrevInstance == NULL); int nReturnCode = -1; CWinThread* pThread = AfxGetThread(); CWinApp* pApp = AfxGetApp(); // AFX internal initialization if (!AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow)) goto InitFailure; // App global initializations (rare) if (pApp != NULL && !pApp->InitApplication()) goto InitFailure; // Perform specific initializations if (!pThread->InitInstance()) { if (pThread->m_pMainWnd != NULL) { TRACE0("Warning: Destroying non-NULL m_pMainWnd/n"); pThread->m_pMainWnd->DestroyWindow(); } nReturnCode = pThread->ExitInstance(); goto InitFailure; } nReturnCode = pThread->Run(); InitFailure: #ifdef _DEBUG // Check for missing AfxLockTempMap calls if (AfxGetModuleThreadState()->m_nTempMapLock != 0) { TRACE1("Warning: Temp map lock count non-zero (%ld)./n", AfxGetModuleThreadState()->m_nTempMapLock); } AfxLockTempMaps(); AfxUnlockTempMaps(-1); #endif AfxWinTerm(); __asm { // Remove our EXECEPTION_REGISTRATION record mov eax,[ESP]; // Get pointer to previous record mov FS:[0], EAX; // Install previous record add esp, 8; // Clean our EXECEPTION_REGISTRATION off stack } return nReturnCode; } int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { int nReturnCode = -1; fCrashDump = fopen("abcd.txt", "w"); __try { nReturnCode = xxx(hInstance, hPrevInstance, lpCmdLine, nCmdShow); } _except( EXCEPTION_EXECUTE_HANDLER) { if (fCrashDump) fprintf(fCrashDump, "hm, bad luck. byebye"); if (AllocConsole()) { freopen("CONOUT$","w+t",stdout); freopen("CONIN$","r+t",stdin); freopen("CONIN$","w+t",stderr); printf("hm bad luck, We do some crash dump work, /nplease send file abcd.txt to us for analyse/n"); printf("press any key to exit"); getch(); } } fclose(fCrashDump); return nReturnCode; }
2. 未处理异常
void CMyMainFuncView::OnDraw(CDC* pDC) { __asm { mov eax, 0; mov [eax], eax } CMyMainFuncDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here }