internal static ICLRRuntimeInfo FindRuntimeVersion(IntPtr processHandle)
 {
     if (iClrMetaHost == null){
         object clrMetaHost;
         Guid ifaceId = typeof(ICLRMetaHost).GUID;
         Guid clsid = clsidCLRMetaHost;
         NativeAPI.CLRCreateInstance(ref clsid, ref ifaceId, out clrMetaHost);
         iClrMetaHost = (ICLRMetaHost)clrMetaHost;
     }
     IEnumUnknown enumRuntimes = iClrMetaHost.EnumerateLoadedRuntimes(processHandle);
     List<ICLRRuntimeInfo> versions = new List<ICLRRuntimeInfo>();
     for (object iUnknw; enumRuntimes.Next(1, out iUnknw, IntPtr.Zero) == 0; /* empty */)
     {
         StringBuilder verion = new StringBuilder();
         int length = 26;//possible length is 24 for version +1
         ((ICLRRuntimeInfo)iUnknw).GetVersionString(verion, ref length);
         versions.Add((ICLRRuntimeInfo)iUnknw);
     }
     if (versions.Count > 1)
     {
         throw new Exception("Multiple .Net Versions Loaded in this Procces");
     }
     else if (versions.Count == 0)
     {
         throw new Exception(" Unmanaged process.");
     }
     return versions[0];
 }
 public DebugEng()
 {
     object clrMetaHost;
     Guid ifaceId = typeof(ICLRMetaHost).GUID;
     Guid clsid = clsidCLRMetaHost;
     NativeAPI.CLRCreateInstance(ref clsid, ref ifaceId, out clrMetaHost);
     iClrMetaHost = (ICLRMetaHost)clrMetaHost;
 }
        public ClrMetaHost()
        {
            Guid ifaceId = typeof(ICLRMetaHost).GUID;
            Guid clsid   = ClsidClrMetaHost;

            NativeMethods.CLRCreateInstance(ref clsid, ref ifaceId, out var o);
            _metaHost = (ICLRMetaHost)o;
        }
Exemple #4
0
        public CLRMetaHost()
        {
            object o;
            Guid   ifaceId = typeof(ICLRMetaHost).GetGuid();
            Guid   clsid   = clsidCLRMetaHost;

            NativeMethods.CLRCreateInstance(ref clsid, ref ifaceId, out o);
            m_metaHost = (ICLRMetaHost)o;
        }
Exemple #5
0
        private static ICorDebug CreateDebuggerInterface(ICLRMetaHost metaHost, string filePath)
        {
            var runtimes = metaHost.EnumerateInstalledRuntimes();
            var runtime  = GetRuntime(runtimes, GetRequestedRuntimeVersion(filePath));

            object rawDebuggerInterface;

            runtime.GetInterface(ref corDebugClsId, ref corDebugRiId, out rawDebuggerInterface);
            return((ICorDebug)rawDebuggerInterface);
        }
Exemple #6
0
        private static ICorDebug GetDebuggerInterface(ICLRMetaHost metaHost, Process process)
        {
            var runtimes = metaHost.EnumerateLoadedRuntimes(process.Handle);
            var runtime  = GetRuntime(runtimes, GetRequestedRuntimeVersion(process.MainModule.FileName));

            object rawDebuggerInterface;

            runtime.GetInterface(ref corDebugClsId, ref corDebugRiId, out rawDebuggerInterface);
            return((ICorDebug)rawDebuggerInterface);
        }
        /// <summary>
        /// Creates new process under the debugger.
        /// </summary>
        /// <param name="exepath">executable path</param>
        /// <param name="desiredVersion">the desired version of the runtime - you don't need to
        /// provide the whole version string as only the first n letters
        /// are compared, for example version string: "v2.0" will match
        /// runtimes versioned "v2.0.1234" or "v2.0.50727". If <code>null</code>
        /// is given, the first found runtime will be returned.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public static CorDebugger CreateDebuggerForExecutable(String exepath, String desiredVersion = null, CorDebuggerOptions options = null)
        {
            ICLRMetaHost metahost = NativeMethods.CLRCreateInstance(ref CLSID_ICLRMetahost, ref IID_ICLRMetahost);

            IEnumUnknown    runtimes = metahost.EnumerateInstalledRuntimes();
            ICLRRuntimeInfo runtime  = GetRuntime(runtimes, desiredVersion);

            if (runtime == null)
            {
                throw new RuntimeNotFoundException();
            }

            return(CreateDebugger(runtime, options ?? new CorDebuggerOptions()));
        }
        /// <summary>
        /// Attaches debugger to the running process.
        /// </summary>
        /// <param name="pid">Process id</param>
        /// <param name="desiredVersion">the desired version of the runtime - you don't need to
        /// provide the whole version string as only the first n letters
        /// are compared, for example version string: "v2.0" will match
        /// runtimes versioned "v2.0.1234" or "v2.0.50727". If <code>null</code>
        /// is given, the first found runtime will be returned.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public static CorDebugger CreateDebuggerForProcess(Int32 pid, String desiredVersion = null, CorDebuggerOptions options = null)
        {
            ICLRMetaHost metahost = NativeMethods.CLRCreateInstance(ref CLSID_ICLRMetahost, ref IID_ICLRMetahost);

            Process         proc     = Process.GetProcessById(pid);
            IEnumUnknown    runtimes = metahost.EnumerateLoadedRuntimes(proc.Handle);
            ICLRRuntimeInfo runtime  = GetRuntime(runtimes, desiredVersion);

            if (runtime == null)
            {
                throw new RuntimeNotFoundException();
            }

            return(CreateDebugger(runtime, options ?? new CorDebuggerOptions()));
        }
 public DebugeeProcess(int processID, ICLRMetaHost iClrMetaHost, DebugEng eventDashboard)
 {
     Process debugee = Process.GetProcessById(processID);
     this.processID = (uint) processID;
     this.processName = debugee.ProcessName;
     this.iClrMetaHost = iClrMetaHost;
     ICLRRuntimeInfo iRuntime = FindRuntimeVersion(debugee.Handle);
     ICorDebug icorDebug = GetDebugger(iRuntime);
     icorDebug.Initialize();
     callback = new ManagedCallback(eventDashboard);
     icorDebug.SetManagedHandler(callback);
     ICorDebugProcess iCorProcess = null;
     icorDebug.DebugActiveProcess((uint)processID, 0, out iCorProcess);
     isDebugMode = true;
     process = new TargetProcess(iCorProcess);
 }
Exemple #10
0
    public static string GetFileRuntime(ICLRMetaHost host, string filename)
    {
        if (filename == null)
        {
            throw new ArgumentNullException(nameof(filename));
        }

        if (host != null)
        {
            var buffer = new StringBuilder(1024);
            uint valueLength = (uint)buffer.Capacity;
            host.GetVersionFromFile(filename, buffer, ref valueLength);
            return buffer.ToString(0, (int)valueLength - 1);
        }
        else
        {
            return GetFileVersion(filename);
        }
    }
Exemple #11
0
    public static string GetFileRuntime(ICLRMetaHost host, string filename)
    {
        if (filename == null)
        {
            throw new ArgumentNullException(nameof(filename));
        }

        if (host != null)
        {
            var  buffer      = new StringBuilder(1024);
            uint valueLength = (uint)buffer.Capacity;
            host.GetVersionFromFile(filename, buffer, ref valueLength);
            return(buffer.ToString(0, (int)valueLength - 1));
        }
        else
        {
            return(GetFileVersion(filename));
        }
    }
Exemple #12
0
        public static void AttachToProcess(Int32 pid)
        {
            Process         proc     = Process.GetProcessById(pid);
            ICLRMetaHost    metahost = NativeMethods.CLRCreateInstance(ref metahost_clsid, ref metahost_riid);
            IEnumUnknown    runtimes = metahost.EnumerateLoadedRuntimes(proc.Handle);
            ICLRRuntimeInfo runtime  = RTHelper.GetRuntime(runtimes, "v4.0");


            ICorDebug codebugger = CreateDebugger(runtime);

            codebugger.Initialize();
            codebugger.SetManagedHandler(new ManagedCallback());


            ICorDebugProcess coproc;

            codebugger.DebugActiveProcess(Convert.ToUInt32(pid), 0, out coproc);

            Console.ReadKey();
        }
Exemple #13
0
        private static IntPtr GetRuntimeInterfaceImpl(
            [In, MarshalAs(UnmanagedType.LPStruct)] Guid clsid,
            [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid)
        {
            IntPtr result;

            if (clsid == Guid.Empty)
            {
                Guid         CLSID_CLRMetaHost = new Guid("9280188d-0e8e-4867-b30c-7fa83884e8de");
                ICLRMetaHost metahost          = (ICLRMetaHost)GetRuntimeInterfaceAsObject(CLSID_CLRMetaHost, typeof(ICLRMetaHost).GUID);

                result = metahost.GetRuntime(GetSystemVersion(), riid);
            }
            else
            {
                Marshal.ThrowExceptionForHR(CLRCreateInstance(clsid, riid, out result));
            }

            return(result);
        }
Exemple #14
0
    public static IEnumerable<string> GetProcessRuntimes(ICLRMetaHost host, SafeHandle hProcess)
    {
        if (host != null)
        {
            var buffer = new StringBuilder(1024);
            IEnumUnknown ppEnumerator = host.EnumerateLoadedRuntimes(hProcess.DangerousGetHandle());
            return ppEnumerator.Cast<ICLRRuntimeInfo>().Select(rti =>
            {
                var bufferLength = (uint)buffer.Capacity;
                rti.GetVersionString(buffer, ref bufferLength);
                return buffer.ToString(0, (int)bufferLength - 1);
            }).ToList();
        }
        else
        {
            string buffer = GetVersionFromProcess(hProcess);
            if (buffer != null)
            {
                return new[] { buffer };
            }
        }

        return Enumerable.Empty<string>();
    }
Exemple #15
0
    public static IEnumerable <string> GetProcessRuntimes(ICLRMetaHost host, SafeHandle hProcess)
    {
        if (host != null)
        {
            var          buffer       = new StringBuilder(1024);
            IEnumUnknown ppEnumerator = host.EnumerateLoadedRuntimes(hProcess.DangerousGetHandle());
            return(ppEnumerator.Cast <ICLRRuntimeInfo>().Select(rti =>
            {
                var bufferLength = (uint)buffer.Capacity;
                rti.GetVersionString(buffer, ref bufferLength);
                return buffer.ToString(0, (int)bufferLength - 1);
            }).ToList());
        }
        else
        {
            string buffer = GetVersionFromProcess(hProcess);
            if (buffer != null)
            {
                return(new[] { buffer });
            }
        }

        return(Enumerable.Empty <string>());
    }
Exemple #16
0
 public CLRMetaHost()
 {
     object o;
     Guid ifaceId = typeof(ICLRMetaHost).GUID;
     Guid clsid = clsidCLRMetaHost;
     NativeMethods.CLRCreateInstance(ref clsid, ref ifaceId, out o);
     m_metaHost = (ICLRMetaHost)o;
 }
        private static ICorDebug CreateDebuggerInterface(ICLRMetaHost metaHost, string filePath)
        {
            var runtimes = metaHost.EnumerateInstalledRuntimes();
            var runtime = GetRuntime(runtimes, GetRequestedRuntimeVersion(filePath));

            object rawDebuggerInterface;
            runtime.GetInterface(ref corDebugClsId, ref corDebugRiId, out rawDebuggerInterface);
            return (ICorDebug)rawDebuggerInterface;
        }
        private static ICorDebug GetDebuggerInterface(ICLRMetaHost metaHost, Process process)
        {
            var runtimes = metaHost.EnumerateLoadedRuntimes(process.Handle);
            var runtime = GetRuntime(runtimes, GetRequestedRuntimeVersion(process.MainModule.FileName));

            object rawDebuggerInterface;
            runtime.GetInterface(ref corDebugClsId, ref corDebugRiId, out rawDebuggerInterface);
            return (ICorDebug)rawDebuggerInterface;
        }
 private static extern int CLRCreateInstance([In] ref Guid clsid, [In] ref Guid riid, [Out, MarshalAs(UnmanagedType.Interface)] out ICLRMetaHost ppInterface);