0

I need to programatically(C#) generate crash dump. The only method i know is to subscribe to the event UnhandledException. However my handler does NOT get hit. Code follows:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
    AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
    Application.ThreadException += Application_ThreadException;
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
}

Application is WinForm based. I need to generate the full memory dumps without showing any actions to my users. The APIs used are of Dbghelp ones but it only generates minidumps. Therefore it would ALSO be helpful to know of any other APIs to use which can generate full memory dumps.

Sandeep
  • 57
  • 1
  • 8

2 Answers2

0

You first show a form, then you register the event handlers and then the Main() method ends. There's no exception that could happen in the last 3 lines of code. Reorder your code like so:

[STAThread]
static void Main()
{
    // Prepare for exception handling
    AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
    Application.ThreadException += Application_ThreadException;
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

    // Now do the work where exceptions can happen
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

Note that in some cases, e.g. stack overflow, heap corruption or .NET execution engine corruption, the program is in a state where it cannot do anything meaningful any more. Exception handlers that operate inside the program are always affected by this. See How to take a good crash dump for .NET for ways on capturing a crash dump from outside your program.

Community
  • 1
  • 1
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • How about .NET stackoverflow exception? Can it be handled using this piece of code? – Sandeep Mar 02 '17 at 11:06
  • Problem is that i need to write a code which must get integrated within existing application which is being deployed over the production environment. So whenever this applications crashes due to any exception, code must capture the dumps. The link you shared talks about using external tools which needs to be run separately. This is undesirable because we need the dumps for crash occurred during the use of application by the Customer and the crash may not occur quite frequently or later. – Sandeep Mar 02 '17 at 12:10
  • WER LocalDumps is not a tool that must be run, it's just a Registry Key. It's integrated in the OS. And, as I said, you cannot solve that issue within the application. For one of our applications, I completely removed the unhandled exception handler, because it didn't work reliably. – Thomas Weller Mar 02 '17 at 12:15
0

I have found this resource that answers your question (as i understand it) exacly. its by an MS employee named Alex, i think.

it uses the windows API call:

[DllImportAttribute("dbghelp.dll")]
[return: MarshalAsAttribute(UnmanagedType.Bool)]
private static extern bool MiniDumpWriteDump(
    [In] IntPtr hProcess,
    uint ProcessId,
    SafeFileHandle hFile,
    DumpType DumpType,
    [In] IntPtr ExceptionParam,
    [In] IntPtr UserStreamParam,
    [In] IntPtr CallbackParam);

The author provided sample code, the this address, i cannot access it at the moment, i hope it helps you.

Menahem
  • 3,974
  • 1
  • 28
  • 43