我希望能够自动附加一个调试器,例如: System.Diagnostics.Debugger.Launch()
,除了当前进程而不是当前进程到另一个命名进程。我有一个进程名称和 PID 来识别另一个进程。
这可能吗?
编辑:
GSerjo
提供了正确的解决方案。我想分享一些关于如何改进它的想法(和解释)。我希望我改进的答案对遇到同样问题的其他人有用。
Processes
。Debug
。或者,在 Visual Studio 中,选择Debug > Attach to Process...
.
结果会有所不同,具体取决于您是否有权访问源代码。
注意事项:以下代码很脆弱,因为某些值(例如 Visual Studio 版本号)是硬编码的。如果您打算分发您的程序,请记住这一点。
首先,在你的项目中添加对 EnvDTE 的引用(右键单击解决方案资源管理器中的引用文件夹,添加引用)。在下面的代码中,我只会展示不寻常的 using 指令;正常的如using System
省略。
因为您正在与 COM 交互,所以您需要确保Main
使用STAThreadAttribute
.
然后,您需要定义IOleMessageFilter
允许您与定义的 COM 方法交互的接口ComImportAttribute
(注意)。我们需要访问消息过滤器,以便在 Visual Studio COM 组件阻止我们的调用之一时重试。
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);
}
现在,我们需要实现这个接口来处理传入的消息:
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
是我们与非托管消息过滤器代码的连接 - 您可以在 MSDN 上阅读 extern 关键字。
现在剩下的就是一些说明用法的代码:
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);
}
看一下这个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using EnvDTE;
using NUnit.Framework;
namespace UnitTests
{
[TestFixture]
public class ForTest
{
[STAThread]
[Test]
public void Test()
{
var dte = (DTE) Marshal.GetActiveObject("VisualStudio.DTE.10.0");
MessageFilter.Register();
IEnumerable<Process> processes = dte.Debugger.LocalProcesses.OfType<Process>();
var process = processes.SingleOrDefault(x => x.ProcessID == 6152);
if (process != null)
{
process.Attach();
}
}
}
public class MessageFilter : IOleMessageFilter
{
//
// Class containing the IOleMessageFilter
// thread error-handling functions.
// Start the filter.
//
// IOleMessageFilter functions.
// Handle incoming thread requests.
#region IOleMessageFilter Members
int IOleMessageFilter.HandleInComingCall(int dwCallType,
IntPtr hTaskCaller, int dwTickCount, IntPtr
lpInterfaceInfo)
{
//Return the flag SERVERCALL_ISHANDLED.
return 0;
}
// Thread call was rejected, so try again.
int IOleMessageFilter.RetryRejectedCall(IntPtr
hTaskCallee, int dwTickCount, int dwRejectType)
{
if (dwRejectType == 2)
// flag = SERVERCALL_RETRYLATER.
{
// Retry the thread call immediately if return >=0 &
// <100.
return 99;
}
// Too busy; cancel call.
return -1;
}
int IOleMessageFilter.MessagePending(IntPtr hTaskCallee,
int dwTickCount, int dwPendingType)
{
//Return the flag PENDINGMSG_WAITDEFPROCESS.
return 2;
}
#endregion
public static void Register()
{
IOleMessageFilter newFilter = new MessageFilter();
IOleMessageFilter oldFilter = null;
CoRegisterMessageFilter(newFilter, out oldFilter);
}
// Done with the filter, close it.
public static void Revoke()
{
IOleMessageFilter oldFilter = null;
CoRegisterMessageFilter(null, out oldFilter);
}
// Implement the IOleMessageFilter interface.
[DllImport("Ole32.dll")]
private static extern int
CoRegisterMessageFilter(IOleMessageFilter newFilter, out
IOleMessageFilter oldFilter);
}
[ComImport, Guid("00000016-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal 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);
}
}
更简单的方法。
public static void Attach(DTE2 dte)
{
var processes = dte.Debugger.LocalProcesses;
foreach (var proc in processes.Cast<EnvDTE.Process>().Where(proc => proc.Name.IndexOf("YourProcess.exe") != -1))
proc.Attach();
}
internal static DTE2 GetCurrent()
{
var dte2 = (DTE2)Marshal.GetActiveObject("VisualStudio.DTE.12.0"); // For VisualStudio 2013
return dte2;
}
用法:
Attach(GetCurrent());
一个选项是运行;vsjitdebugger.exe -p ProcessId
可以Process.Start
在 ac# app 中使用它来执行此操作。
如果您在将调试器附加到手动附加太快的进程时遇到麻烦,请不要忘记您有时可以添加Console.ReadKey();
到代码的第一行,然后您就有了所有时间需要手动附加它。令人惊讶的是,我花了一段时间才弄清楚:D