如何将 VisualStudio 附加到尚未启动的进程?

是的。NET 程序,由于某些原因(从 Excel2010模板项目创建的 Excel 文件)无法从 VisualStudio 运行,因此需要对启动事件进行调试。

如果我想调试后来的程序初始化事件没有问题。我从资源管理器运行该程序,将该进程附加到 VisualStudio,并在代码中添加一些断点。但是在这里,我需要在启动事件上设置断点。我需要能够将进程附加到 VisualStudio,而不是依赖于 PID,而是依赖于特定的进程名或任何其他可行的解决方案。

当然,在我的启动事件中添加 Thread.Sleep(1000)以给我一些时间在 VisualStudio 中附加进程是不可能的!

60086 次浏览

If there's no process then Visual Studio can't attach to it.

However, you can set the startup program of your project to be something other than the output of your project.

Actually you can; you don't attach to it, you start it. On the properties of your project, on the Debugging tab, specify the path of the program you want to attach to in the "Command" textbox.

You can also enter any command-line arguments for the program in the "Command Arguments" box:

enter image description here

Ensure that "Attach" is set to "No".

I was debugging a C++ plugin in an externally spawned process that crashed by throwing an exception at startup and this worked perfectly for me:

Add the free Reattach Extension for Visual Studio. Ask it to reattach to the process name before it is launched. It will pop a modal dialog saying it is waiting for the process name to launch.

Now launch the process and the Visual Studio debugger will attach immediately, catching exceptions and hitting breakpoints.

I found this answer when I was looking for something similar. In my case, I couldn't simply use the executable as my project's startup program because it needed to be started in a very specific environment that I couldn't reproduce easily (namly: started from cygwin).

I took a look at the Reattach Extension as suggested by mrstrange and also the very similar Attach To Anything extension... but my executable seemed to close too quickly for the extensions to notify and attach.

What finally helped me was this: https://stackoverflow.com/a/4042545/1560865, which references the MSDN article How to: Launch the Debugger Automatically, which in-turn lists the following steps:

  1. Start the Registry Editor (regedit).
  2. In the Registry Editor, open the HKEY_LOCAL_MACHINE folder.
  3. Navigate to HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\currentversion\image file execution options.
  4. In the Image File Execution Options folder, locate the name of the application you want to debug, such as myapp.exe. If you cannot find the application you want to debug:
    a. Right-click the Image File Execution Options folder, and on the shortcut menu, click New Key.
    b. Right-click the new key, and on the shortcut menu, click Rename. c. Edit the key name to the name of your application; myapp.exe, in this example.
  5. Right-click the myapp.exe folder, and on the shortcut menu, click New String Value.
  6. Right-click the new string value, and on the shortcut menu, click Rename.
  7. Change the name to debugger.
  8. Right-click the new string value, and on the shortcut menu, click Modify. The Edit String dialog box appears.
  9. In the Value data box, type vsjitdebugger.exe.
  10. Click OK.
  11. From the Registry menu, click Exit.
  12. The directory containing vsjitdebugger.exe must be in your system path. To add it to the system path, follow these steps:
    a. Open the Control Panel in Classic view, and double-click System.
    b. Click Advanced System Settings.
    c. In System Properties, click the Advanced tab.
    d. On the Advanced tab, click Environment Variables.
    e. In the Environment Variables dialog box, under System variables, select Path, then click the Edit button.
    f. In the Edit System Variable dialog box, add the directory to the Variable value box. Use a semicolon to separate it from other entries in the list.
    g. Click OK to close the Edit System Variable dialog box.
    h. Click OK to close the Environment Variables dialog box.
    i. Click OK to close the System Properties dialog box.
  13. Now, use any method to start your application. Visual Studio will start and load the application.

Hope that this helps anyone else in the future!

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

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

You will have to tell Visual Studio where the debugging symbols are if it doesn't find them automatically.

This is easier than other suggestions: you don't have to mess with project properties, and no extensions are needed.

One little solution that might suit many people.

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

    System.Threading.Thread.Sleep(20000)

That will make the exe sleep for 20 seconds before it starts processing anything. Then you have 20 seconds to attach to the process, which can be done quickly with ctrl+alt+p, then find the process, then enter to attach.

Not much of an answer but worked a treat for me :--)

You can show a MessageBox, this would block the application, then you attach or reattach 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 would be executed before anything else, except for the initialization of components:

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

When you finish your debugging, comment out that line.

You can start the debugger from your code via

public static void Main(string[] args)
{
System.Diagnostics.Debugger.Launch();
}

But don't for get to remove this line before shipping your application. Maybe you want to use compiler flags to be sure:

    public static void Main(string[] args)
{
#if debug
System.Diagnostics.Debugger.Launch();
#endif
}