How to connect Visual Studio to a process that is not already running?

I have a .NET program that for some reason cannot be started from Visual Studio (the Excel file is created from the Excel 2010 project template), for which I need to debug launch events.

If I want to debug events that occur after the initialization of the program, there are no problems. I run the program from Explorer, attach the process to Visual Studio, and add some breakpoints to the code. But here I need to set breakpoints on trigger events. I need to be able to attach processes to Visual Studio, not depending on the PID, but on the specific name of the process or any other solution that will work.

Of course, adding Thread.Sleep(1000) to my launch events to give me time to attach the process in Visual Studio is out of the question!

+63
debugging process visual-studio
Nov 17 '11 at 13:09
source share
7 answers

In fact, you can; You do not become attached to it, you begin it. In the properties of your project on the Debug tab, specify the path to the program to which you want to join in the Command text box.

You can also enter any command line arguments for the program in the Command Arguments field:

enter image description here

Make sure that β€œAttach” is set to β€œNo”.

+83
Nov 17 '11 at 13:21
source share

I was debugging a C ++ plugin in an external child process that crashed, throwing an exception on startup, and this worked fine for me:

Add the free Reateach Extension for Visual Studio . Ask him to connect to the process name before starting it. A modal dialog box appears stating that he is waiting for the process name to start.

Now start the process and the Visual Studio debugger will connect immediately, catching the exceptions and hitting breakpoints.

+20
Oct 14 '14 at 20:42 on
source share

Follow these steps if you have Visual Studio 2017-2019:

  1. File> Open> Project / Solution
  2. Choose your .exe
  3. Debug> Start Debugging

Much simpler than other suggestions: you do not need to mess with project properties, and no extensions are required.

+11
Jul 18 '17 at 15:52
source share

I found this answer when I was looking for something similar. In my case, I could not just use the executable as my project launcher because it had to be run in a very specific environment that I could not easily play back (namly: launched from cygwin).

I looked at the Reattach extension suggested by mrstrange, as well as the very similar Attach To Anything extension ... but my executable seemed to close too quickly for the extensions to notify and attach.

In the end, the following helped me: https://stackoverflow.com/a/166268/ which contains a link to the MSDN article How-to. Automatically starting the debugger , which in turn lists the following steps:

  1. Run the registry editor (regedit).
  2. In the registry editor, open the HKEY_LOCAL_MACHINE folder.
  3. Go to the HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Windows NT \ currentversion \ image file execution options.
  4. In the " Image File Execution Options " folder, find the name of the application that you want to debug, for example myapp.exe. If you cannot find the application you want to debug:
    but. Right-click the " Image File Execution Options " folder and select " New Key" in the context menu.
    b. Right-click the new key and select Rename from the context menu. from. Change the key name to the name of your application; myapp.exe, in this example.
  5. Right-click the myapp.exe folder and select " New String Value" from the context menu.
  6. Right-click the new string value and select Rename from the context menu.
  7. Change the name to debugger .
  8. Right-click the new string value and select Modify from the context menu. The Edit Row dialog box appears .
  9. In the Value field, enter vsjitdebugger.exe .
  10. Click OK .
  11. In the registry menu, click " Exit . "
  12. The directory containing vsjitdebugger.exe must be in the system path. To add it to the system path, follow these steps:
    but. Open Control Panel in classic view and double-click the System icon.
    b. Click Advanced System Settings .
    from. In the " System Properties " window, click on the " Advanced " tab.
    e. On the Advanced tab, click Environment Variables .
    e. In the Environment Variables dialog box, under System Variables, select Path, and then click the Edit button.
    e. In the Edit System Variable dialog box, add the directory to the Variable Value field. Use a semicolon to separate it from other entries in the list.
    d. Click OK to close the Edit System Variable dialog box.
    hour Click OK to close the Environment Variables dialog box.
    I am. Click OK to close the System Properties dialog box.
  13. Now use any method to launch the application. Visual Studio will launch and download the application.

Hope this helps someone else in the future!

+9
May 11 '16 at 15:12
source share

If there is no process, Visual Studio cannot attach to it.

However, you can configure your project launch program as something different from the output of your project.

+3
Nov 17 '11 at 13:12
source share

One small solution that can suit many people.

  • in the first line of code that exe will run, add this command

    System.Threading.Thread.Sleep (20000)

This will make exe sleep 20 seconds before he starts processing anything. Then you have 20 seconds to join the process, which can be quickly done with ctrl + alt + p, then find the process and type β€œjoin”.

A little answer, but the pleasure worked for me :-)

+1
Mar 11 '18 at 19:25
source share

You can show the MessageBox, this will block the application, then you will attach or reconnect the debugger to the process and click OK to continue:

 MessageBox.Show("Attach process to debugger and click ok!"); 

You can add it to the Form constructor (if you use winforms), so this will be done first, except for component initialization:

 public MainForm() { InitializeComponent(); MessageBox.Show("Attach process to debugger and click ok!"); } 

When you are done debugging, comment out this line.

+1
Apr 03 '18 at 13:49
source share