Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 是否可能在win32用户应用程序中捕获未处理的异常?(setunhandledexceptionfilter())_Windows_Winapi_Unhandled Exception - Fatal编程技术网

Windows 是否可能在win32用户应用程序中捕获未处理的异常?(setunhandledexceptionfilter())

Windows 是否可能在win32用户应用程序中捕获未处理的异常?(setunhandledexceptionfilter()),windows,winapi,unhandled-exception,Windows,Winapi,Unhandled Exception,我花了很多时间在win32进程中使用所谓的setunhandledexceptionfilter API捕获未处理的异常 但当WERWindows错误报告显示时,我并没有捕捉到异常,这对于watson博士来说是众所周知的 如果我的应用程序中没有第三方,是否不可能捕获所有异常 我认为有处理的方法,但我不明白 我不习惯Windows开发环境。这就是为什么我在谷歌搜索中失去了理智 下面是我在vc110Visual Studio 2012中的测试用例 chat test[65]; int main()

我花了很多时间在win32进程中使用所谓的setunhandledexceptionfilter API捕获未处理的异常

但当WERWindows错误报告显示时,我并没有捕捉到异常,这对于watson博士来说是众所周知的

如果我的应用程序中没有第三方,是否不可能捕获所有异常

我认为有处理的方法,但我不明白

我不习惯Windows开发环境。这就是为什么我在谷歌搜索中失去了理智

下面是我在vc110Visual Studio 2012中的测试用例

chat test[65];
int main() { 
 // after attaching unhandled exception call-back using setunhandledexceptionfilter()
 // die point (ACCESS_VIOLATION c0000005)
 for (int k=0; k<1000000; k++) 
     test[k]=65;

你的申请有那么糟糕吗?您不能处理哪些异常?是的,您使用SetUnhandledExceptionFilter。很好,你做错了。没有人不保证未来。正如你所知,当我测试某个案例时,有很多异常,我遇到了这种情况。请发布你用来尝试捕获异常的代码。我有编写这种类型过滤器的经验。你好,我附上了我的源代码,它只用于未处理的异常。如果有任何疑问,请告诉我。谢谢你的帮助。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/stat.h>
#include <assert.h>
#include <process.h>
#include <direct.h>
#include <conio.h>
#include <time.h>
#include <Windows.h>

#include <tchar.h>
#include <dbghelp.h>
#include <stdio.h>
#include <crtdbg.h>
#include <WinBase.h>
#pragma comment ( lib, "dbghelp.lib" )


void CreateMiniDump( EXCEPTION_POINTERS* pep ); 
BOOL CALLBACK MyMiniDumpCallback(
    PVOID                            pParam, 
    const PMINIDUMP_CALLBACK_INPUT   pInput, 
    PMINIDUMP_CALLBACK_OUTPUT        pOutput 
); 

///////////////////////////////////////////////////////////////////////////////
// Minidump creation function 
//

#if 0
LONG WINAPI lpTopLevelExceptionFilter(EXCEPTION_POINTERS* ExceptionInfo);
#endif

void CreateMiniDump( EXCEPTION_POINTERS* pep ) 
{
    time_t t;
    struct tm *tinfo;
    wchar_t dump_name[128];
    HANDLE hFile;
    time(&t);
    tinfo = localtime(&t);

    wcsftime(dump_name, 128, L"MiniDump[%Y%m%d][%H_%M_%S].dmp", tinfo);

    // file format MiniDump[YYYYMMDD][HH_MM_SEC]
    hFile = CreateFile(dump_name, GENERIC_READ | GENERIC_WRITE, 
        0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); 

    if( ( hFile != NULL ) && ( hFile != INVALID_HANDLE_VALUE ) ) 
    {
        // Create the minidump 

        MINIDUMP_EXCEPTION_INFORMATION mdei; 
        MINIDUMP_CALLBACK_INFORMATION mci; 
        MINIDUMP_TYPE mdt;
        BOOL rv;

        mdei.ThreadId           = GetCurrentThreadId(); 
        mdei.ExceptionPointers  = pep; 
        mdei.ClientPointers     = FALSE; 



        mci.CallbackRoutine     = (MINIDUMP_CALLBACK_ROUTINE)MyMiniDumpCallback; 
        mci.CallbackParam       = 0; 

        mdt       = (MINIDUMP_TYPE)(MiniDumpWithIndirectlyReferencedMemory | MiniDumpScanMemory| MiniDumpWithThreadInfo); 

        rv = MiniDumpWriteDump( GetCurrentProcess(), GetCurrentProcessId(), 
            hFile, mdt, (pep != 0) ? &mdei : 0, 0, &mci ); 

        if( !rv ) 
            _tprintf( _T("MiniDumpWriteDump failed. Error: %u \n"), GetLastError() ); 
        else 
            _tprintf( _T("Minidump created.\n") ); 

        // Close the file 

        CloseHandle( hFile ); 

    }
    else 
    {
        _tprintf( _T("CreateFile failed. Error: %u \n"), GetLastError() ); 
    }

}


///////////////////////////////////////////////////////////////////////////////
// Custom minidump callback 
//

BOOL CALLBACK MyMiniDumpCallback(
    PVOID                            pParam, 
    const PMINIDUMP_CALLBACK_INPUT   pInput, 
    PMINIDUMP_CALLBACK_OUTPUT        pOutput 
) 
{
    BOOL bRet = FALSE; 


    // Check parameters 

    if( pInput == 0 ) 
        return FALSE; 

    if( pOutput == 0 ) 
        return FALSE; 


    // Process the callbacks 

    switch( pInput->CallbackType ) 
    {
        case IncludeModuleCallback: 
        {
            // Include the module into the dump 
            bRet = TRUE; 
        }
        break; 

        case IncludeThreadCallback: 
        {
            // Include the thread into the dump 
            bRet = TRUE; 
        }
        break; 

        case ModuleCallback: 
        {
            // Does the module have ModuleReferencedByMemory flag set ? 

            if( !(pOutput->ModuleWriteFlags & ModuleReferencedByMemory) ) 
            {
                // No, it does not - exclude it 

                wprintf( L"Excluding module: %s \n", pInput->Module.FullPath ); 

                pOutput->ModuleWriteFlags &= (~ModuleWriteModule); 
            }

            bRet = TRUE; 
        }
        break; 

        case ThreadCallback: 
        {
            // Include all thread information into the minidump 
            bRet = TRUE;  
        }
        break; 

        case ThreadExCallback: 
        {
            // Include this information 
            bRet = TRUE;  
        }
        break; 

        case MemoryCallback: 
        {
            // We do not include any information here -> return FALSE 
            bRet = FALSE; 
        }
        break; 

        case CancelCallback: 
            break; 
    }

    return bRet; 

}

LONG WINAPI exception_filter_func(EXCEPTION_POINTERS* pep)
{
    if (pep == NULL) {
        return EXCEPTION_EXECUTE_HANDLER;
    }

    if (pep->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW) {
        HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CreateMiniDump, pep, 0, NULL);
        WaitForSingleObject(hThread, INFINITE);
        CloseHandle(hThread);
    } else {
        CreateMiniDump(pep);
    }

    return EXCEPTION_EXECUTE_HANDLER;
}
char test[65];

int main(int argc, char **argv)
{
    int k;
    SetUnhandledExceptionFilter(exception_filter_func);
    // exception occured (ACCESS_VIOLATION)
    for (k=0; k<1000000; k++) 
      test[k]=65;
}