forked from fareloz/AttachToolbar
/
AttachToolbarController.cs
80 lines (70 loc) · 2.53 KB
/
AttachToolbarController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.IO;
using System.Windows.Forms;
using EnvDTE80;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Debugger.Interop;
namespace AttachToolbar
{
public class AttachToolbarController : IDebugEventCallback2
{
public AttachToolbarController(DTE2 dte)
{
_env = dte;
_dbg = _env.Debugger as Debugger2;
}
public void AttachTo(string processName, AttachEngineType attachEngineType)
{
bool found = false;
if (State.IsAttached) {
_dbg.DetachAll();
State.IsAttached = false;
}
foreach (Process2 process in _dbg.LocalProcesses)
{
string fileName = Path.GetFileName(process.Name);
if (fileName.Equals(processName, StringComparison.InvariantCultureIgnoreCase))
{
found = true;
Transport transport = _dbg.Transports.Item("default");
Engine[] engines = new[] { transport.Engines.Item(attachEngineType.GetEngineName()) };
try {
process.Attach2(engines);
}
catch {
found = false;
}
break;
}
}
State.IsAttached = found;
if (!found)
{
MessageBox.Show("Failed to attach to " + processName + ".",
"Attach Toolbar",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
public int Event(IDebugEngine2 engine, IDebugProcess2 process, IDebugProgram2 program,
IDebugThread2 thread, IDebugEvent2 debugEvent, ref Guid riidEvent, uint attributes)
{
// Ignore a few events right away.
if (debugEvent is IDebugModuleLoadEvent2 ||
debugEvent is IDebugThreadCreateEvent2 ||
debugEvent is IDebugThreadDestroyEvent2)
return VSConstants.S_OK;
if (process == null)
return VSConstants.S_OK;
if (debugEvent is IDebugProcessCreateEvent2) {
State.IsAttached = true;
}
else if (debugEvent is IDebugProcessDestroyEvent2) {
State.IsAttached = false;
}
return VSConstants.S_OK;
}
private readonly DTE2 _env;
private readonly Debugger2 _dbg;
}
}