"附加到进程"作为构建后事件

Jos*_*ons 6 automation visual-studio-2008 post-build

我有一个在"w3wp.exe"进程下运行的应用程序.

在调试时,我经常发现自己遵循以下步骤:

1 - 做出一些改变

2 - 构建项目

3 - 使用"工具"菜单下的"附加到进程"对话框附加到"w3wp.exe".

4 - 在应用程序中执行一些操作以使我的代码执行,因此我可以在调试器中逐步执行它

我想在后构建脚本中自动执行第3步,以便IDE在构建完成后自动附加到进程.请注意,我已经将应用程序作为构建后过程的一部分启动,因此我可以指望此时存在的过程.

有没有人知道自动化"附加到进程"命令的方法?命令行中的某些内容会特别好,但宏也可以.

我在Windows 7,64位下使用Visual Studio 2008.

编辑 @InSane基本上给了我正确的答案,但它不起作用,因为我需要调试托管代码,而不是本机代码.看来vsjitdebugger默认使用Native代码,因此我的断点没有被击中.在IDE内部,我可以指定"托管代码",调试器按预期方式附加.那么有没有办法将vsjitdebugger指向托管代码?

Jos*_*ons 7

我终于能够通过互联网上其他地方找到的一个例子来解决这个问题.我在这里分享,因为这对我有帮助.

1 - 使用以下代码创建一个新的命令行应用程序(此示例在VB.NET中).

Option Strict Off
Option Explicit Off
Imports System
'On my machine, these EnvDTE* assemblies were here:
'C:\Program Files (x86)\Common Files\microsoft shared\MSEnv\PublicAssemblies
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Imports System.Threading

Module modMain
    Function AttachToProcess(ByVal processName As String, _
                             ByVal Timeout As Integer) As Boolean
        Dim proc As EnvDTE.Process
        Dim attached As Boolean
        Dim DTE2 As EnvDTE80.DTE2

        Try
            DTE2 = _
            System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.9.0")

            For Each proc In DTE2.Debugger.LocalProcesses
                If (Right(proc.Name, Len(processName)).ToUpper = processName.ToUpper) Then
                    proc.Attach()
                    System.Threading.Thread.Sleep(Timeout)
                    attached = True
                End If
            Next
        Catch Ex As Exception
            Console.Write("Unable to Attach to Debugger : " & Ex.Message)
        End Try

        Return attached
    End Function

    Sub Main()
        'to call w/ Command Line arguments follow this syntax
        'AttachProcess <<ProcessName>> <<TimeOut>>
        'AttachProcess app.exe 2000
        Dim AppName As String = "w3wp.exe"
        Dim TimeOut As Integer = 20000 '20 Seconds
        Try
            If Environment.GetCommandLineArgs().Length > 1 Then
                AppName = Environment.GetCommandLineArgs(1)
            End If

            If Environment.GetCommandLineArgs().Length > 2 Then
                If IsNumeric(Environment.GetCommandLineArgs(2)) Then
                    TimeOut = Environment.GetCommandLineArgs(2)
                End If
            End If
            Environment.GetCommandLineArgs()
            AttachToProcess(AppName, TimeOut)
            Console.WriteLine("Attached!!")

        Catch Ex As Exception
            Console.Write("Unable to Attach to Debugger : " & Ex.Message)
        End Try
    End Sub
End Module
Run Code Online (Sandbox Code Playgroud)

2 - 在Visual Studio中打开要调试的解决方案

3 - 在"构建后"事件结束时,输入对此新实用程序的调用,如下所示:

c:\AutoAttach.exe w3wp.exe 20000
Run Code Online (Sandbox Code Playgroud)

4 - 构建您的应用程序

  • 我们需要为相应的visual studio版本更新字符串"VisualStudio.DTE.9.0".对于VisualStudio2012,它将是"VisualStudio.DTE.11.0" (2认同)