VS2010 does not show unhandled exception message in a WinForms Application on a 64-bit version of Windows

前端 未结 5 2340
忘了有多久
忘了有多久 2020-11-21 05:43

When I create a new project, I get a strange behavior for unhandled exceptions. This is how I can reproduce the problem:

1) create a new Windows Forms Application (C

5条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-21 06:20

    In my experience, I only see this issue when I'm running with a debugger attached. The application behaves the same when run standalone: the exception is not swallowed.

    With the introduction of KB976038, you can make this work as you'd expect again. I never installed the hotfix, so I'm assuming it came as part of Win7 SP1.

    This was mentioned in this post:

    • The case of the disappearing OnLoad exception – user-mode callback exceptions in x64

    Here's some code that will enable the hotfix:

    public static class Kernel32
    {
        public const uint PROCESS_CALLBACK_FILTER_ENABLED = 0x1;
    
        [DllImport("Kernel32.dll")]
        public static extern bool SetProcessUserModeExceptionPolicy(UInt32 dwFlags);
    
        [DllImport("Kernel32.dll")]
        public static extern bool GetProcessUserModeExceptionPolicy(out UInt32 lpFlags);
    
    
        public static void DisableUMCallbackFilter() {
            uint flags;
            GetProcessUserModeExceptionPolicy(out flags);
    
            flags &= ~PROCESS_CALLBACK_FILTER_ENABLED;
            SetProcessUserModeExceptionPolicy(flags);
        }
    }
    

    Call it at the beginning of your application:

        [STAThread]
        static void Main()
        {
            Kernel32.DisableUMCallbackFilter();
    
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    

    I've confirmed (with the the simple example shown below) that this works, just as you'd expect.

    protected override void OnLoad(EventArgs e) {
        throw new Exception("BOOM");   // This will now get caught.
    }
    

    So, what I don't understand, is why it was previously impossible for the debugger to handle crossing kernel-mode stack frames, but with this hotfix, they somehow figured it out.

提交回复
热议问题