Safe handle wrapper for Module32* and CreateToolhelp32Snapshot Win32 functions.
Inheritance: Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid
Exemple #1
0
 private static NativeMethods.ToolHelpHandle GetModuleSnap(int processId)
 {
     NativeMethods.ToolHelpHandle hModuleSnap = NativeMethods.CreateToolhelp32Snapshot(
         NativeMethods.SnapshotFlags.Module | NativeMethods.SnapshotFlags.Module32,
         processId);
     return(hModuleSnap);
 }
Exemple #2
0
        /// <summary>
        ///     Similar to System.Diagnostics.WinProcessManager.GetModuleInfos,
        ///     except that we include 32 bit modules when we run in x64 mode. See
        ///     http://blogs.msdn.com/b/jasonz/archive/2007/05/11/code-sample-is-your-process-using-the-silverlight-clr.aspx
        /// </summary>
        private IEnumerable <NativeMethods.MODULEENTRY32> GetModules()
        {
            NativeMethods.GetWindowThreadProcessId(Hwnd, out int processId);

            NativeMethods.ToolHelpHandle hModuleSnap = GetModuleSnap(processId);
            bool isValidModuleSnap = !hModuleSnap.IsInvalid;

            if (isValidModuleSnap)
            {
                foreach (NativeMethods.MODULEENTRY32 moduleentry32 in GetModulesFromValidSnap(hModuleSnap))
                {
                    yield return(moduleentry32);
                }
            }
        }
Exemple #3
0
        private static IEnumerable <NativeMethods.MODULEENTRY32> GetModulesFromValidSnap(NativeMethods.ToolHelpHandle hModuleSnap)
        {
            var me32 = new NativeMethods.MODULEENTRY32();

            using (hModuleSnap)
            {
                me32.dwSize = (uint)Marshal.SizeOf(me32);
                if (NativeMethods.Module32First(hModuleSnap, ref me32))
                {
                    do
                    {
                        yield return(me32);
                    } while (NativeMethods.Module32Next(hModuleSnap, ref me32));
                }
            }
        }