private bool GetFirstModule(int pid, string name, out ModuleEntry32 module) { module = new ModuleEntry32(); var moduleSnapshot = IntPtr.Zero; for (int i = 0; i < 50; i++) { moduleSnapshot = Native.CreateToolhelp32Snapshot(SnapshotFlags.Module, (uint)pid); if (moduleSnapshot != IntPtr.Zero) { break; } // Ensure that the target process was not started in a suspended state, and try calling the function again. If the function fails with ERROR_BAD_LENGTH when called with TH32CS_SNAPMODULE or TH32CS_SNAPMODULE32, call the function again until it succeeds. var win32Error = Marshal.GetLastWin32Error(); if (win32Error != ErrorBadLength) { throw new Win32Exception(win32Error); } Thread.Sleep(1); } if (moduleSnapshot == IntPtr.Zero) { throw new Exception("Couldn't read modules of Hearthstone process"); } try { var mod = new ModuleEntry32 { dwSize = (uint)Marshal.SizeOf(typeof(ModuleEntry32)) }; if (!Native.Module32First(moduleSnapshot, ref mod)) { return(false); } do { if (mod.szModule == name) { module = mod; return(true); } }while(Native.Module32Next(moduleSnapshot, ref mod)); return(false); } finally { Native.CloseHandle(moduleSnapshot); } }
public static extern bool Module32Next(IntPtr hSnapshot, ref ModuleEntry32 lpme);