1
Vote

Unable to catch DEBUG_EXCEPTION_UNHANDLED in WinForms applications

description

Hello,
i'm trying to catch an DEBUG_EXCEPTION_UNHANDLED exception in StartListening function but when my WinForms application raise an exception I get ONLY stopReason.EventType == DEBUG_EXCEPTION_FIRST_CHANCE. Then, when process ended, I get m_process.StopReason is ProcessExitedStopReason.
How can I catch only unhandled exceptions of a WinForms application?
More over I have disabled the popup that asks if you want continue or exit the application using this snippet:

Partial Friend Class MyApplication
Private Sub MyApplication_UnhandledException(sender As Object, e As UnhandledExceptionEventArgs) Handles Me.UnhandledException
  e.ExitApplication = True
End Sub
End Class

Thanks for your support,
Filippo Bottega.

comments

filippobottega wrote Oct 2, 2015 at 9:20 AM

Solved!

If you want to attach to a WinForms Application you have to disable Application Framework under project properties of the application.
Then you have to create this module:
Imports System.Security.Permissions

Module Program

  <SecurityPermission(SecurityAction.Demand, Flags:=SecurityPermissionFlag.ControlAppDomain)>
  Public Sub Main()
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException)
    Application.EnableVisualStyles()
    Application.SetCompatibleTextRenderingDefault(False)
    Application.Run(New MainForm)
  End Sub
End Module
Then you have to change the startup object to Sub Main.

Regards,
Filippo Bottega.