3

I'm building a Visual Basic application in Visual Studio 2010. Some of my options can only be applied on restarting the application.

I have not gotten a single crash when application is running normally. Nor does it crash when I apply settings, manually exit and restart. On the other hand, as soon as I try to do a automatic restart from the application, I get an exception on one out of 5-10 restarts.

I've tried to run the debugger, but as soon as the application restarts, the Visual Studio debugger is turned off and does not turn back on when the application launches again. Nor does it launch again with same configurations. It seems the debugger launched application configuration and the manually launched application configuration files are different.

Is there a way I can get around this? Keep the debugger on across restarts? Or should I undertake a different strategy?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
thebunnyrules
  • 1,520
  • 15
  • 22
  • Out of curiosity, why are you running Visual Studio 2010? – Dai Jul 12 '17 at 16:44
  • 2
    I'm don't really trust anything Microsoft has put out after 2013. My App's aimed for Windows 7 and below, so getting VS 2013+ is not necessary. – thebunnyrules Jul 12 '17 at 16:51
  • Quality of life improvements alone would be worth it. As to your question, [this](https://stackoverflow.com/questions/8167610/how-to-attach-visual-studio-to-a-process-that-is-not-started-yet) should show you how to attach to a process as it starts up. – Bradley Uffner Jul 12 '17 at 19:00
  • What do you mean by Quality of life? As in the later VSs are easier to use, better designed, less buggy? Thanks for the link, I'll have a read through it. :) – thebunnyrules Jul 12 '17 at 19:10

1 Answers1

1

Method 1: Attaching the debugger from within the application

If the application sometimes crashes at startup add a call to Debugger.Launch() in the application's Startup event. Doing so will cause Visual Studio to open a window where you can choose to attach its debugger.

You can check the Debugger.IsAttached property in order to determine whether a debugger is already attached or not.

Steps to subscribe to the Startup event:

  1. Right-click your project in the Solution Explorer and press Properties.

  2. Go to the Application tab.

  3. Press View Application Events.

  4. Select MyApplication (events) in the left combo box.

  5. Select Startup in the right combo box.

Code:

Imports System.Diagnostics

...

Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
    If Debugger.IsAttached = False Then
        Debugger.Launch()
    End If
End Sub

Method 2: Letting Windows start the debugger, which in turn starts your application

An alternative solution is to add your application to the Image File Execution Options registry key, which allows you to specify a debugger which should launch the application for you.

NOTE: Adding your application to Image File Execution Options causes Windows to automatically launch the specified debugger instead of your application whenever you try to open it. Your application's path is passed as a command line argument and it is then up to the debugger to launch your application, attaching itself to it.

Malwarebytes have some info about Image File Execution Options on their blog: An introduction to Image File Execution Options.

Here's how you'd do it:

  1. Open Regedit.

  2. Navigate to the HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options key.

  3. Locate the key with the name of your application (if it exists), or create a new one in the format of yourApplicationName.exe.

  4. Create a new String value (REG_SZ) and name it debugger.

  5. Set the value of debugger to vsjitdebugger.exe.

  6. Go ahead and start debugging!

For more information see: How to: Launch the Debugger Automatically - MSDN

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75