在 Visual Studio 中,如果要将调试器附加到任何进程,则可以选择要使用的某些特定引擎(代码类型)或一组引擎:
接下来(在您选择任何引擎和进程之后),如果您单击附加按钮,调试器附加操作将启动。然后还会触发与调试相关的事件。IDebugEventCallback2::Event
可用于抓取此类事件(例如,提取调试器实际附加到的进程的名称):
public int Event(IDebugEngine2 engine, IDebugProcess2 process, IDebugProgram2 program,
IDebugThread2 thread, IDebugEvent2 debugEvent, ref Guid riidEvent,
uint attributes)
{
if (debugEvent is IDebugProcessCreateEvent2)
{
string processname;
if(process != null)
process.GetName((uint) enum_GETNAME_TYPE.GN_FILENAME, out processname);
//...
}
}
有没有类似的方法来获取有关已选择引擎的一些信息?
更新:更详细的代码:
public class DebugEventsHunter : IVsDebuggerEvents, IDebugEventCallback2
{
private readonly IVsDebugger _debugger;
private uint _cookie;
public DebugEventsHunter(IVsDebugger debugger) { _debugger = debugger; }
public void Start()
{
_debugger.AdviseDebuggerEvents(this, out _cookie);
_debugger.AdviseDebugEventCallback(this);
}
public int Event(IDebugEngine2 engine, IDebugProcess2 process, IDebugProgram2 program,
IDebugThread2 thread, IDebugEvent2 debugEvent, ref Guid riidEvent, uint attributes)
{
if (debugEvent is IDebugProcessCreateEvent2)
{
// get process name (shown before)
}
if (debugEvent is IDebugEngineCreateEvent2)
{
// why execution flow never enters this scope?
IDebugEngine2 e;
((IDebugEngineCreateEvent2)debugEvent).GetEngine(out e);
}
// engine parameter is also always null within this scope
return VSConstants.S_OK;
}
public int OnModeChange(DBGMODE mode) { /*...*/ }
}
和用法:
var debugger = GetService(typeof(SVsShellDebugger)) as IVsDebugger;
var hunter = new DebugEventsHunter(debugger);
hunter.Start();