public static string GetModuleNameWin32(int processId) { var hSnapShot = new HandleRef(Ref, CreateToolhelp32Snapshot(NativeMethods.Toolhelp32Flags.SnapProcess, 0)); if (hSnapShot.Handle == IntPtr.Zero) { return(null); } string sModule = null; var uProcess = new NativeMethods.PROCESSENTRY32(); // Create a snapshot from all running processes. uProcess.dwSize = Marshal.SizeOf(uProcess); // Loop all processes in the snapshot and find the process id. var bProcessFound = Process32First(hSnapShot, ref uProcess); while (bProcessFound) { if (uProcess.th32ProcessID == processId) { // Save the module name and exit loop. sModule = uProcess.szExeFile.Trim('\0'); break; } // Continue enumeration of processes? bProcessFound = Process32Next(hSnapShot, ref uProcess); } SafeNativeMethods.CloseHandle(hSnapShot); return(sModule); }
public static string GetModuleNameWin32(int processId) { var hSnapShot = new HandleRef(Ref, CreateToolhelp32Snapshot(NativeMethods.Toolhelp32Flags.SnapProcess, 0)); if (hSnapShot.Handle == IntPtr.Zero) return null; string sModule = null; var uProcess = new NativeMethods.PROCESSENTRY32(); // Create a snapshot from all running processes. uProcess.dwSize = Marshal.SizeOf(uProcess); // Loop all processes in the snapshot and find the process id. var bProcessFound = Process32First(hSnapShot, ref uProcess); while (bProcessFound) { if (uProcess.th32ProcessID == processId) { // Save the module name and exit loop. sModule = uProcess.szExeFile.Trim('\0'); break; } // Continue enumeration of processes? bProcessFound = Process32Next(hSnapShot, ref uProcess); } SafeNativeMethods.CloseHandle(hSnapShot); return sModule; }
public static extern bool Process32Next( HandleRef hSnapshot, ref NativeMethods.PROCESSENTRY32 lppe );