2016-08-24 89 views
-1

我已經創建了控制檯應用程序,我從中調用了Win32項目,並引發了訪問衝突異常。我在SetUnhandledExceptionFilter附加了我的自定義過濾器。當我使用!clrstack命令時,它顯示非託管調用堆棧,但MSDN表示Clrstack僅提供託管代碼的堆棧跟蹤。!ClrStack顯示非託管調用堆棧

https://msdn.microsoft.com/en-us/library/bb190764(v=vs.110).aspx

請幫助。

的Program.cs

public void ExceptionMethod() 
    { 

     ExceptionCreator.CreateAccessViolationException(); 

    } 

Win32項目:

ErrorReportWritter.h

#pragma once 
#include "stdafx.h" 
#include "DbgHelp.h" 

using namespace System; 
using namespace System::Runtime::InteropServices; 
using namespace System::Text; 

public ref class ErrorReportWritter 
{ 
public: 
    static void InstallHandler(); 
}; 

ErrorReportWritter.cpp

LONG WINAPI MyExceptionFilter(__in struct _EXCEPTION_POINTERS *ExceptionInfo) 
{ 
    //For test purpose, Dump location will be the solution location itself 
    HANDLE hFile = CreateFileA("Test.dmp", GENERIC_READ | GENERIC_WRITE, 

     0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 

    if ((hFile != NULL) && (hFile != INVALID_HANDLE_VALUE)) 
    { 
     // Create the maxidump 
     MINIDUMP_TYPE mdt = (MINIDUMP_TYPE)(MiniDumpWithFullMemory | 
      MiniDumpWithFullMemoryInfo | 
      MiniDumpWithHandleData | 
      MiniDumpWithThreadInfo | 
      MiniDumpWithUnloadedModules); 

     //MINIDUMP_TYPE mdt = MiniDumpNormal; 

     MINIDUMP_EXCEPTION_INFORMATION mei; 
     mei.ThreadId = GetCurrentThreadId(); 
     mei.ClientPointers = FALSE; 
     mei.ExceptionPointers = ExceptionInfo; 


     BOOL rv = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, mdt, &mei, 0, 0); 

     //TODO: put a check for failure return value and throw exception in that case 

     // Close the file 
     CloseHandle(hFile); 

    } 

    //TODO: Still need to decide if the search next functionality is needed for final solution 
    if (oldFilter == NULL) 
    { 
     return EXCEPTION_CONTINUE_SEARCH; 
    } 

    LONG ret = oldFilter(ExceptionInfo); 

    return ret; 
} 

void ErrorReportWritter::InstallHandler() 
{ 
    //Returns the address of the previous exception filter established with the function. 
    //A NULL return value means that there is no current top-level exception handler. 
    oldFilter = SetUnhandledExceptionFilter(MyExceptionFilter); 
} 

enter image description here

+0

調用堆棧的截圖附在此鏈接http://i.stack.imgur.com/ghUYs.png –

+1

你的非託管崩潰究竟在哪裏?您的Cpp項目也是託管代碼。 – nvoigt

+0

ExceptionCreator.cpp 的#include 「ExceptionCreator.h」 的#pragma評論(LIB, 「dbghelp.lib」)使用命名空間系統 ; 使用命名空間System :: Runtime :: InteropServices; 使用命名空間System :: Text; void ExceptionCreator :: CreateAccessViolationException() { \t Marshal :: StructureToPtr(42,IntPtr(42),true); } –

回答

0

您的代碼是管理 C++。 (或者C++/CLI或者你的Visual Studio版本支持的任何東西)。

要創建非託管 Win32的項目,例如在Visual Studio 2015年,得到了File - >New Project,然後選擇Templates - >Visual C++ - >Win32 Project