如何在创建进程时附加调试器?

All*_*nek 17 .net debugging visual-studio-2008

我想调试一个在启动时立即失败的.NET应用程序(并且没有错误消息或日志存在),但我无法将调试器附加到它,因为该过程几乎在我运行后立即存在.我没有应用程序的源代码,所以我不能做"开始调试".我尝试使用Visual Studio宏来启动进程,附加到它,然后中断,但宏太慢,当它找到进程时,进程已经退出:

Imports System
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module Module1
    Sub RunAndAttach()
        Try
            Dim dbg As Debugger3 = DTE.Debugger
            Dim trans As Transport = dbg.Transports.Item("Default")
            Dim sysProc As Process = System.Diagnostics.Process.Start(New ProcessStartInfo("C:\Temp\CrashingApp.exe") With {.WorkingDirectory = "C:\Temp"})
            Dim proc As EnvDTE90.Process3 = dbg.GetProcesses(trans, "ALLON-PC").Item("CrashingApp.exe")
            If (Not sysProc.HasExited) Then
                proc.Attach()
                proc.Break(False)
            Else
                MsgBox("Process " + proc.Name + " has already has exited.")
            End If
        Catch ex As System.Exception
            MsgBox(ex.Message)
        End Try
    End Sub

End Module
Run Code Online (Sandbox Code Playgroud)

有没有办法将调试器附加到新创建的进程,如F5呢?

谢谢!

Joh*_*her 21

  1. 创建一个新项目(控制台项目很好).
  2. 右键单击该项目,然后选择"属性".
  3. 单击"调试"选项卡.
  4. 选择"启动外部程序:"
  5. 选择程序.
  6. 点击F5.