"附加到进程"作为生成后事件 [英] "Attach to Process" as a post-build event

查看:156
本文介绍了"附加到进程"作为生成后事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行在w3wp.exe的进程承载的应用程序。

I have an application that runs hosted under the "w3wp.exe" process.

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

While debugging, I often find myself following these steps:

1 - 使一些变化

1 - Make some change

2 - 生成项目

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

3 - Attach to "w3wp.exe" using the "attach to process" dialog under the Tools menu.

4 - 在应用程序中执行一些操作,使我的code执行,这样我就可以通过它在调试步骤

4 - Perform some action in the application to make my code execute, so I can step through it in the debugger

我想在生成后脚本自动化步骤3中,从而使IDE构建完成后会自动连接到该进程。请注意,我已经启动应用程序作为构建后处理的一部分,这样我就可以在现有的在这一点上处理计数。

I'd like to automate step 3 in the post-build script, so that the IDE automatically attaches to the process after the build is completed. Note that I already launch the application as a part of the post-build process, so I can count on the process existing at this point.

有谁知道一种方法来自动化附加到进程命令?在命令行中的东西会特别好,但宏会做了。

Does anyone know a way to automate the "attach to process" command? Something from the command line would be especially nice, but a macro would do, too.

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

I'm using Visual Studio 2008 under Windows 7, 64 bit.

修改
@InSane基本上给了我正确的答案,但它不工作,因为我需要调试托管code,而不是本地code。看来vsjitdebugger默认为本地code,因此我的断点未命中。在IDE中,我可以指定管理code和调试器附加预期。那么,有什么办法可以指向vsjitdebugger托管code?

Edit @InSane basically gave me the right answer, but it does not work because I need to debug managed code, rather than native code. It seems that vsjitdebugger defaults to Native code, and thus my breakpoint is not hit. From inside the IDE, I can specify "managed code" and the debugger attaches as expected. So is there any way to point vsjitdebugger to managed code?

推荐答案

我终于能有一个例子,我发现在互联网上的其他地方来解决这个问题。我在这里分享它,因为这是对我很有帮助。

I was finally able to solve this problem with an example I found elsewhere on the internet. I'm sharing it here since this was helpful to me.

1 - 创建一个具有以下code一个新的命令行应用程序(这个例子是VB.NET)

1 - Create a new command line application with the below code (this example is in 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

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

2 - Open the solution you want to debug in Visual Studio

3 - 在你的生成后事件结束后,输入一个电话这一新的工具,如:

3 - At the end of your "post-build" events, enter a call to this new utility, as in:

c:\AutoAttach.exe w3wp.exe 20000

4 - 生成应用程序

4 - Build your application

这篇关于&QUOT;附加到进程&QUOT;作为生成后事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆