private static bool HasPython(int processId) { // calling CreateToolhelp32Snapshot directly is significantly (~5x) faster than accessing // Process.Modules. This gets called during the Debug->attach to process API so that ends // up being a difference of less than 1 sec vs. 5 seconds. IntPtr h = NativeMethods.CreateToolhelp32Snapshot(SnapshotFlags.Module, (uint)processId); if (h != NativeMethods.INVALID_HANDLE_VALUE) { uint marshalSize = (uint)Marshal.SizeOf(typeof(MODULEENTRY32)); var me = new MODULEENTRY32(); me.dwSize = (uint)marshalSize; if (NativeMethods.Module32First(h, ref me)) { do { if (IsPythonModule(me.szModule)) { return true; } me.dwSize = marshalSize; } while (NativeMethods.Module32Next(h, ref me)); } NativeMethods.CloseHandle(h); } return false; }
public static extern bool Module32Next(IntPtr hSnapshot, ref MODULEENTRY32 lpme);