Exemplo n.º 1
0
    static Process[] GetProcessesByPath(string path)
    {
        List <Process> result = new List <Process>();

        string processName = Path.GetFileNameWithoutExtension(path);

        foreach (var process in Process.GetProcessesByName(processName))
        {
            ToolHelpHandle hModuleSnap = NativeMethods.CreateToolhelp32Snapshot(NativeMethods.SnapshotFlags.Module, (uint)process.Id);
            if (!hModuleSnap.IsInvalid)
            {
                NativeMethods.MODULEENTRY32 me32 = new NativeMethods.MODULEENTRY32();
                me32.dwSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(me32);
                if (NativeMethods.Module32First(hModuleSnap, ref me32))
                {
                    if (me32.szExePath == path)
                    {
                        result.Add(process);
                    }
                }
                hModuleSnap.Close();
            }
        }

        return(result.ToArray());
    }
Exemplo n.º 2
0
 protected virtual void Dispose(bool dispose)
 {
     if (!Handle?.IsInvalid ?? false)
     {
         Handle?.Close();
     }
 }
Exemplo n.º 3
0
        public IEnumerable <ModuleInfo> GetModuleInfos()
        {
            ToolHelpHandle ptr  = null;
            var            list = new List <ModuleInfo>();

            try
            {
                ptr = CreateToolhelp32Snapshot(0x8, ProcessId);
                if (ptr.IsInvalid)
                {
                    throw new Win32Exception();
                }
                var me32 = new MODULEENTRY32();
                me32.dwSize = (uint)Marshal.SizeOf(me32);

                if (Module32First(ptr, ref me32))
                {
                    do
                    {
                        ModuleInfo info = new ModuleInfo
                        {
                            baseName    = me32.szModule,
                            fileName    = me32.szExePath,
                            baseOfDll   = me32.modBaseAddr,
                            sizeOfImage = (int)me32.modBaseSize,
                            Id          = (int)me32.th32ModuleID
                        };
                        list.Add(info);
                        me32.dwSize = (uint)Marshal.SizeOf(me32);
                    }while (Module32Next(ptr, ref me32));
                }
                if (Marshal.GetLastWin32Error() != 18)                 //ERROR_NO_MORE_FILES
                {
                    throw new Win32Exception();
                }
            }
            finally
            {
                if (ptr != null && !ptr.IsInvalid)
                {
                    ptr.Close();
                }
            }
            return(list);
        }