在C#调试器附加到其他进程 [英] Attach debugger in C# to another process

查看:309
本文介绍了在C#调试器附加到其他进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想能够自动附加调试器,像这样: System.Diagnostics.Debugger.Launch(),除了不与当前进程到另一个named进程。我有一个进程名和PID来识别对方的过程。

I'd like to be able to automatically attach a debugger, something like: System.Diagnostics.Debugger.Launch(), except rather than the current process to another named process. I've got a process name and PID to identify the other process.

这可能吗?

推荐答案

编辑:
GSerjo 提供正确的解决方案。我想与大家分享关于如何提高它的一些想法(和解释)。我希望我的回答改善将是给别人谁遇到同样的问题是有用的。

GSerjo offered the correct solution. I'd like to share a few thoughts on how to improve it (and an explanation). I hope my improved answer will be useful to to others who experience the same problem.


  1. 打开Windows任务管理器(<大骨节病>控制 + <大骨节病>移 + <大骨节病> ESC )。

  2. 转至标签进程

  3. 右键单击该进程。

  4. 选择调试

  1. Open the Windows Task Manager (Ctrl + Shift + Esc).
  2. Go to the Tab Processes.
  3. Right click the process.
  4. Select Debug.

或者,在Visual Studio中,选择调试和GT;附加到进程...

Or, within Visual Studio, select Debug > Attach to Process....

的结果将取决于您是否有访问源$ C ​​$ C有所不同。

Results will vary depending on whether you have access to the source code.

A 注意事项:以下code是在这个意义上脆弱的某些价值观,
  如Visual Studio的版本号,是硬codeD。请记住这一点前进
  如果您打算分发您的程序。

A note of caution: The following code is brittle in the sense that certain values, such as the Visual Studio Version number, are hard-coded. Keep this in mind going forward if you are planning to distribute your program.

首先,要EnvDTE的引用添加到您的项目(右键单击在Solution Explorer中的引用文件夹中添加引用)。在下面的code,我将只显示使用指令异常;正常的,如使用系统被省略。

First of all, add a reference to EnvDTE to your project (right click on the references folder in the solution explorer, add reference). In the following code, I'll only show the unusual using directives; the normal ones such as using System are omitted.

你需要确保装饰因为你与COM交互的方法(应用程序的入口点)的<一个href=\"http://msdn.microsoft.com/en-us/library/system.stathreadattribute.aspx\"><$c$c>STAThreadAttribute.

Because you are interacting with COM you need to make sure to decorate your Main method (the entry point of your application) with the STAThreadAttribute.

然后,你需要定义 IOleMessageFilter 接口,让您与定义COM方法进行交互(注意<一个href=\"http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.comimportattribute.aspx\"><$c$c>ComImportAttribute).我们需要访问消息过滤器,所以我们可以重试,如果在Visual Studio COM组件块我们的呼吁之一。

Then, you need to define the IOleMessageFilter Interface that will allow you to interact with the defined COM methods (note the ComImportAttribute). We need to access the message filter so we can retry if the Visual Studio COM component blocks one of our calls.

using System.Runtime.InteropServices;

[ComImport, Guid("00000016-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IOleMessageFilter
{
    [PreserveSig]
    int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo);

    [PreserveSig]
    int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType);

    [PreserveSig]
    int MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType);
}

现在,我们需要实现以处理传入的消息这个接口:

Now, we need to implement this interface in order to handle incoming messages:

public class MessageFilter : IOleMessageFilter
{
    private const int Handled = 0, RetryAllowed = 2, Retry = 99, Cancel = -1, WaitAndDispatch = 2;

    int IOleMessageFilter.HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo)
    {
        return Handled;
    }

    int IOleMessageFilter.RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType)
    {
        return dwRejectType == RetryAllowed ? Retry : Cancel;
    }

    int IOleMessageFilter.MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType)
    {
        return WaitAndDispatch;
    }

    public static void Register()
    {
        CoRegisterMessageFilter(new MessageFilter());
    }

    public static void Revoke()
    {
        CoRegisterMessageFilter(null);
    }

    private static void CoRegisterMessageFilter(IOleMessageFilter newFilter)
    {
        IOleMessageFilter oldFilter;
        CoRegisterMessageFilter(newFilter, out oldFilter);
    }

    [DllImport("Ole32.dll")]
    private static extern int CoRegisterMessageFilter(IOleMessageFilter newFilter, out IOleMessageFilter oldFilter);
}

我的返回值定义为更好的可读性常数和重构了整个事情有点摆脱一些从MSDN的例子重复的,所以我希望你会发现它不言自明。 的extern INT CoRegisterMessageFilter 是我们对非托管的消息过滤器code连接 - 你可以的上MSDN extern关键字读了。

I defined the return values as constants for better readability and refactored the whole thing a bit to get rid of some of the duplication from the MSDN example, so I hope you'll find it self-explanatory. extern int CoRegisterMessageFilter is our connection to the unmanaged message filter code - you can read up on the extern keyword at MSDN.

现在所有的左边是表示使用的一些code:

Now all that's left is some code illustrating the usage:

using System.Runtime.InteropServices;
using EnvDTE;

[STAThread]
public static void Main()
{
    MessageFilter.Register();
    var process = GetProcess(7532);
    if (process != null)
    {
        process.Attach();
        Console.WriteLine("Attached to {0}", process.Name);
    }
    MessageFilter.Revoke();
    Console.ReadLine();
}

private static Process GetProcess(int processID)
{
    var dte = (DTE)Marshal.GetActiveObject("VisualStudio.DTE.10.0");
    var processes = dte.Debugger.LocalProcesses.OfType<Process>();
    return processes.SingleOrDefault(x => x.ProcessID == processID);
}

这篇关于在C#调试器附加到其他进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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