This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static CorDebugger GetDebugger() | |
{ | |
Guid classId = new Guid("9280188D-0E8E-4867-B30C-7FA83884E8DE"); | |
Guid interfaceId = new Guid("D332DB9E-B9B3-4125-8207-A14884F53216"); | |
dynamic rawMetaHost; | |
Microsoft.Samples.Debugging.CorDebug.NativeMethods.CLRCreateInstance(ref classId, ref interfaceId, out rawMetaHost); | |
ICLRMetaHost metaHost = (ICLRMetaHost)rawMetaHost; | |
var currentProcess = Process.GetCurrentProcess(); | |
var runtime_v40 = GetLoadedRuntimeByVersion(metaHost, currentProcess.Id, "v4.0"); | |
var debuggerClassId = new Guid("DF8395B5-A4BA-450B-A77C-A9A47762C520"); | |
var debuggerInterfaceId = new Guid("3D6F5F61-7538-11D3-8D5B-00104B35E7EF"); | |
//Get a debugger for this version of the runtime. | |
Object res = runtime_v40.m_runtimeInfo.GetInterface(ref debuggerClassId, ref debuggerInterfaceId); | |
ICorDebug debugger = (ICorDebug)res; | |
//We create CorDebugger that wraps the ICorDebug stuff making it easier to use | |
var corDebugger = new CorDebugger(debugger); | |
return corDebugger; | |
} | |
public static CLRRuntimeInfo GetLoadedRuntimeByVersion(ICLRMetaHost metaHost, Int32 processId, string version) | |
{ | |
IEnumerable<CLRRuntimeInfo> runtimes = EnumerateLoadedRuntimes(metaHost, processId); | |
foreach (CLRRuntimeInfo rti in runtimes) | |
{ | |
//Search through all loaded runtimes for one that starts with v4.0. | |
if (rti.GetVersionString().StartsWith(version, StringComparison.OrdinalIgnoreCase)) | |
{ | |
return rti; | |
} | |
} | |
return null; | |
} | |
public static IEnumerable<CLRRuntimeInfo> EnumerateLoadedRuntimes(ICLRMetaHost metaHost, Int32 processId) | |
{ | |
List<CLRRuntimeInfo> runtimes = new List<CLRRuntimeInfo>(); | |
IEnumUnknown enumRuntimes; | |
//We get a handle for the process and then get all the runtimes available from it. | |
using (ProcessSafeHandle hProcess = NativeMethods.OpenProcess((int)(NativeMethods.ProcessAccessOptions.ProcessVMRead | | |
NativeMethods.ProcessAccessOptions.ProcessQueryInformation | | |
NativeMethods.ProcessAccessOptions.ProcessDupHandle | | |
NativeMethods.ProcessAccessOptions.Synchronize), | |
false, // inherit handle | |
processId)) | |
{ | |
if (hProcess.IsInvalid) | |
{ | |
throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()); | |
} | |
enumRuntimes = metaHost.EnumerateLoadedRuntimes(hProcess); | |
} | |
// Since we're only getting one at a time, we can pass NULL for count. | |
// S_OK also means we got the single element we asked for. | |
for (object oIUnknown; enumRuntimes.Next(1, out oIUnknown, IntPtr.Zero) == 0; /* empty */) | |
{ | |
runtimes.Add(new CLRRuntimeInfo(oIUnknown)); | |
} | |
return runtimes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment