1

NOTE: This is not a duplicate of Is there a setting that is preventing the unhandled exception dialog from displaying in apps that I compile? because that post is asking the opposite question (how to get the dialog I want to eliminate) and the answers provided, although they provide some pertinent information to my question, it does not answer the question which is how do I get my application to create a crash dump when an unhandled exception is thrown from a Windows Form Event.

Why on earth would anyone want there users to see this dialog is beyond me.

.NET error dialog

But that's besides the point of this post. How do I get my application to produce a crash dump instead? I can't figure it out.

EDIT: Let's say for example, my executable is myapplication.exe. When it crashes I want WERFAULT.EXE to handle it so it will create a crash dump that my customers can send me so I can investigate the problem. If I throw an unhandled exception from one of my threads I get a crash dump. But when I throw an unhandled exception from within a Windows Form event the dialog above is shown.

I created a myapplication.exe.config file and entered the following as directed at the bottom of the details of this dialog.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.windows.forms jitDebugging="true" />
</configuration>

And I put the myapplication.exe.config file in my installation directory besides my application.exe file.

Now the application just goes away. No crash dump, even though I have my registry configured to create crash dumps. As directed here.

In my installer I create the necessary registry changes to force my application to create crash dumps. In

HKLM\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps\myapplication.exe

registry key I set DumpType to 2, DumpFolder to our application data directory and DumpCount to 1. But process just goes away and no crash dump is created.

I know the machine is configured to create crash dumps correctly because I can add code to other applications to make them crash and crash dumps get created correctly.

EDIT: If I added to my main entry point Application.SetUnhandledExceptionMode to UnhandledExceptionMode.ThrowException I don't get the dialog, but the process just goes away. No crash dump.

tdemay
  • 649
  • 8
  • 23
  • Libraries such as NBug have that functionality for years. No need to reinvent the wheels. – Lex Li Jan 18 '18 at 18:10
  • @LexLi, huh? What functionality do you think I'm asking for? – tdemay Jan 18 '18 at 18:18
  • "Handle application crashes gracefully", unless that's not what you are asking. – Lex Li Jan 18 '18 at 18:19
  • @LexLi, nope.... not what I'm asking. When application crashes, I want a crash dump from my customers. And I can't figure out how to get it to create a crash dump instead of providing the dialog above. If you think my question is unclear I'd welcome suggestion on how to improve it. – tdemay Jan 18 '18 at 18:21
  • That dialog is provided by .NET Framework and Windows, when you have an unhandled exception. So unless you use a library like NBug to properly handle the exceptions, you cannot avoid it. Such libraries can also help generate crash dumps (via Win32 API instead of WER), and give you more control on the end user experience. So whenever reading a comment from others, make sure you do respect their willingness to help. – Lex Li Jan 18 '18 at 18:24
  • @LexLi, sorry if you didn't think I was. I did. Just looked like a misunderstanding. Trying to get to the bottom of it. But I'm not looking at a new feature. Creating crash dumps is a function of windows if it's configured properly. This app does create crash dumps, but depending on the exception we get this dialog instead. It should be a configuration issue to get it to produce crash dumps, I just can't figure out what that configuration is. There shouldn't be a need for another product. Unless I'm missing something. – tdemay Jan 18 '18 at 18:39
  • Have you tried your settings with an application named `myapplication.exe`? https://stackoverflow.com/q/30457570/480982 – Thomas Weller Jan 18 '18 at 19:04
  • @ThomasWeller, Yes. That's the problem. How do I get werfault.exe to trigger when .net windows form applications crash? werfault.exe will handle crashes sometimes, depending on where the exception is thrown from. – tdemay Jan 18 '18 at 19:06
  • For me, `jitDebugging="true"`only affects debug builds. – Thomas Weller Jan 18 '18 at 19:44
  • Make sure that there is no DWORD `Disabled` with a value of `1` in `HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps`. I updated my answer at https://stackoverflow.com/q/30457570/480982 – Thomas Weller Jan 18 '18 at 19:51
  • You can't get a crash dump until your app actually crashes with an unhandled exception. As long as you see this dialog, that is not going to happen. Change the Application.SetUnhandleExceptionMode() call in your Main() method. – Hans Passant Jan 18 '18 at 21:27
  • @HansPassant, yeah... I just tried that out. I found this article: https://social.msdn.microsoft.com/Forums/en-US/9dc0126a-3b39-46c6-bd15-44c130cbeb48/just-in-time-debugger-for-c-managed-code?forum=netfxtoolsdev But when I set it to ThrowException the process just terminates. No crash dump. – tdemay Jan 19 '18 at 18:11
  • @HansPassant. This is not an "exact" duplicate of the post you mentioned. My question is different as it is asking about how to create a crash dump. Perhaps the wording of my post is unclear or putting the screen show of the exception dialog in the post might have suggested my question was about that. But it isn't. The post you referred to does not tell how to generate a crash dump. If I can edit my post to make it more clear I'd appreciate any input you might have. Thanks. and thanks for your help. It gave me some good leads. – tdemay Jan 23 '18 at 23:31
  • @LexLi if I could have addressed my last comment to two people I would have tagged you in it too. Please see my last comment to HansPassant. And thank you for your help. – tdemay Jan 23 '18 at 23:32

1 Answers1

2

Answered my own question. As far as I can tell there is no configuration that will enable Windows Forms application to create a crash dump when an unhandled exception is thrown from a Windows form event. But you can trigger it in code.

.NET catches exceptions thrown in it's form events and displays the .NET error dialog shown above. This can be controlled by Application.SetUnhandledExceptionMode and Application.ThreadException event handler.

The premise of my solution is to handle the thread exception event and rethrow the exception outside of a windows form event. If the exception is not caught it will create a crash dump if the machine is configured to do so.

The only drawback from this is that the stack trace will not be the stack trace of when the initial exception occurred and you can't get it from the exception object that is thrown in a crash dump. So you will want to log the stack trace somewhere.

    private class ApplicationThreadException : Exception
    {
        public new Exception InnerException { get; internal set; }
        public ApplicationThreadException(Exception e)
        {
            InnerException = e;
        }
    }

    static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
    {
        throw new ApplicationThreadException(e.Exception);
    }

    [STAThread]
    static void Main(string[] args)
    {
        Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
        try
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MyForm());
        }
        catch (ApplicationThreadException e)
        {
            throw e.InnerException;
        }
        catch (Exception e)
        {
            // do what ever
        }
    }
tdemay
  • 649
  • 8
  • 23
  • Thanks, this answer put me on the right track. [The steps that worked for me are here.](https://gist.github.com/PyroGenesis/05bd414d580ef662a4d50120ea9643f4) – Burhan Jan 18 '22 at 23:32