public static int GetProcessIdByName(string processName) { IntPtr handle = CreateToolhelp32Snapshot(0x2, 0); if ((int)handle > 0) { List <ProcessEntry32> list = new List <ProcessEntry32>(); ProcessEntry32 pe32 = new ProcessEntry32(); pe32.dwSize = (uint)Marshal.SizeOf(pe32); int bMore = Process32First(handle, ref pe32); while (bMore == 1) { IntPtr temp = Marshal.AllocHGlobal((int)pe32.dwSize); Marshal.StructureToPtr(pe32, temp, true); ProcessEntry32 pe = (ProcessEntry32)Marshal.PtrToStructure(temp, typeof(ProcessEntry32)); Marshal.FreeHGlobal(temp); list.Add(pe); bMore = Process32Next(handle, ref pe32); } CloseHandle(handle); foreach (ProcessEntry32 p in list) { if (p.szExeFile.Equals(processName)) { return((int)p.th32ProcessID); } } } return(0); }
private IntPtr FindTrueAOWHandle() { IntPtr aowHandle = IntPtr.Zero; uint maxThread = 0; IntPtr handle = CreateToolhelp32Snapshot(0x2, 0); if ((int)handle > 0) { ProcessEntry32 pe32 = new ProcessEntry32(); pe32.dwSize = (uint)Marshal.SizeOf(pe32); int bMore = Process32First(handle, ref pe32); while (bMore == 1) { IntPtr temp = Marshal.AllocHGlobal((int)pe32.dwSize); Marshal.StructureToPtr(pe32, temp, true); ProcessEntry32 pe = (ProcessEntry32)Marshal.PtrToStructure(temp, typeof(ProcessEntry32)); Marshal.FreeHGlobal(temp); if (pe.szExeFile.Contains("aow_exe.exe") && pe.cntThreads > maxThread) { maxThread = pe.cntThreads; aowHandle = (IntPtr)pe.th32ProcessID; } bMore = Process32Next(handle, ref pe32); } CloseHandle(handle); } return(aowHandle); }
public static Process Parent(this Process process) { int SnapShot = CreateToolhelp32Snapshot(0x00000002, 0); //2 = SNAPSHOT of all procs try { ProcessEntry32 pe32 = new ProcessEntry32(); pe32.dwSize = 296; int procid = process.Id; while (Process32Next(SnapShot, ref pe32) != 0) { string xname = pe32.szExeFile.ToString(); if (procid == pe32.th32ProcessID) { return System.Diagnostics.Process.GetProcessById(Convert.ToInt32(pe32.th32ParentProcessID)); } } } catch (Exception ex) { throw new Exception(System.Reflection.MethodBase.GetCurrentMethod() + " failed! [Type:" + ex.GetType().ToString() + ", Msg:" + ex.Message + "]"); } finally { CloseHandle(SnapShot); } return null; }
//Return ProcessList -> Dont change GlobalVariables public static List <string> GetListSimple() { IntPtr handle = CreateToolhelp32Snapshot(0x2, 0); List <string> newProcessList = new List <string>(); if ((int)handle > 0) { List <ProcessEntry32> list = new List <ProcessEntry32>(); ProcessEntry32 pe32 = new ProcessEntry32(); pe32.dwSize = (uint)Marshal.SizeOf(pe32); int bMore = Process32First(handle, ref pe32); while (bMore == 1) { IntPtr temp = Marshal.AllocHGlobal((int)pe32.dwSize); Marshal.StructureToPtr(pe32, temp, true); ProcessEntry32 pe = Marshal.PtrToStructure <ProcessEntry32>(temp); Marshal.FreeHGlobal(temp); list.Add(pe); bMore = Process32Next(handle, ref pe32); } CloseHandle(handle); //Create temp List to compare running tasks foreach (ProcessEntry32 p in list) { newProcessList.Add(p.szExeFile.ToLower()); } } return(newProcessList); }
public static ProcessEntry[] GetProcessEntries() { IntPtr hSnapshot = Kernel32.CreateToolhelp32Snapshot(SnapshotFlags.Process, 0); if (hSnapshot.IsZero()) { return(null); } ProcessEntry32 pe32 = default(ProcessEntry32); pe32.dwSize = (uint)ProcessEntry32.Size; if (!Kernel32.Process32First(hSnapshot, ref pe32)) { Kernel32.CloseHandle(hSnapshot); return(null); } List <ProcessEntry> processEntries = new List <ProcessEntry>(); do { processEntries.Add(new ProcessEntry(pe32)); } while (Kernel32.Process32Next(hSnapshot, ref pe32)); Kernel32.CloseHandle(hSnapshot); return(processEntries.ToArray()); }
public static Process Parent(this Process process) { int SnapShot = CreateToolhelp32Snapshot(0x00000002, 0); //2 = SNAPSHOT of all procs try { ProcessEntry32 pe32 = new ProcessEntry32(); pe32.dwSize = 296; int procid = process.Id; while (Process32Next(SnapShot, ref pe32) != 0) { string xname = pe32.szExeFile.ToString(); if (procid == pe32.th32ProcessID) { return(System.Diagnostics.Process.GetProcessById(Convert.ToInt32(pe32.th32ParentProcessID))); } } } catch (Exception ex) { throw new Exception(System.Reflection.MethodBase.GetCurrentMethod() + " failed! [Type:" + ex.GetType().ToString() + ", Msg:" + ex.Message + "]"); } finally { CloseHandle(SnapShot); } return(null); }
public static Process FindParentProcess() { var snapShot = CreateToolhelp32Snapshot(0x00000002, 0); //2 = SNAPSHOT of all procs try { var pe32 = new ProcessEntry32 { dwSize = 296 }; var procid = Process.GetCurrentProcess().Id; while (Process32Next(snapShot, ref pe32) != 0) { if (procid == pe32.th32ProcessID) { return(Process.GetProcessById(Convert.ToInt32(pe32.th32ParentProcessID))); } } } catch (Exception ex) { throw new Exception(System.Reflection.MethodBase.GetCurrentMethod() + " failed! [Type:" + ex.GetType() + ", Msg:" + ex.Message + "]"); } finally { CloseHandle(snapShot); } return(null); }
/// <summary> /// Gets the parent process id /// </summary> public static int?GetParentProcessId(int processId) { IntPtr snapshot = IntPtr.Zero; try { snapshot = CreateToolhelp32Snapshot(SnapshotFlags.Process, 0); ProcessEntry32 processEntry = Process32First(snapshot); do { if (processEntry.processId == processId) { return((int)processEntry.parentProcessId); } } while (Process32Next(snapshot, ref processEntry)); } catch (Exception) { } finally { if (snapshot != IntPtr.Zero) { CloseHandle(snapshot); } } return(null); }
internal ProcessEntry(ProcessEntry32 pe32) { ImageName = pe32.szExeFile; Id = pe32.th32ProcessID; Usage = pe32.cntUsage; Threads = pe32.cntThreads; ParentId = pe32.th32ParentProcessID; ModuleId = pe32.th32ModuleID; DefaultHeapId = pe32.th32DefaultHeapID; PriorityClassBase = pe32.pcPriClassBase; Flags = pe32.dwFlags; }
private static ProcessEntry32 Process32First(IntPtr snapshot) { ProcessEntry32 processEntry = new ProcessEntry32(size: null); if (!Process32First(snapshot, ref processEntry)) { int error = Marshal.GetLastWin32Error(); if (error != ERROR_NO_MORE_FILES) { throw new InvalidOperationException("Win32 Error 0x" + error.ToString("X8", CultureInfo.InvariantCulture)); } } return(processEntry); }
private void GetProcessesList() { var proc = NativeMethods.CreateToolhelp32Snapshot(SnapshotFlags.Process, 0); var entry = new ProcessEntry32(); entry.dwSize = (uint)Marshal.SizeOf(entry); if (NativeMethods.Process32First(proc, ref entry)) { do { allProcesses.Add(new Models.ProcessTree(entry)); } while (NativeMethods.Process32Next(proc, ref entry)); } NativeMethods.CloseHandle(proc); }
public static IEnumerable <ProcessEntry> GetProcesses() { using var snapShotHandle = CreateToolhelp32Snapshot(SnapshotFlags.TH32CS_SNAPPROCESS, 0); var entry = new ProcessEntry32 { dwSize = (uint)Marshal.SizeOf(typeof(ProcessEntry32)), }; var result = Process32First(snapShotHandle, ref entry); while (result != 0) { yield return(new ProcessEntry(entry.th32ProcessID, entry.th32ParentProcessID)); result = Process32Next(snapShotHandle, ref entry); } }
public static int GetNumberRunning(string pname) { int ProcCount = 0; int SnapShot = CreateToolhelp32Snapshot(0x00000002, 0); ProcessEntry32 pe32 = new ProcessEntry32(); pe32.dwSize = 296; while (Process32Next(SnapShot, ref pe32) != 0) { string xname = pe32.szExeFile.ToString(); if (pname.CompareTo(xname) == 0) { ProcCount++; } } CloseHandle(SnapShot); return(ProcCount); }
public static uint GetProcessIdForProcess(string pname) { uint procid = 0; int SnapShot = CreateToolhelp32Snapshot(0x00000002, 0); ProcessEntry32 pe32 = new ProcessEntry32(); pe32.dwSize = 296; while (Process32Next(SnapShot, ref pe32) != 0) { string xname = pe32.szExeFile.ToString(); if (pname.CompareTo(xname) == 0) { procid = pe32.th32ProcessID; break; } } CloseHandle(SnapShot); return(procid); }
/// <summary> /// /// </summary> /// <param name="hSnapshot"></param> /// <param name="pe32"></param> /// <returns></returns> public static bool Process32Next(IntPtr hSnapshot, ref ProcessEntry32 pe32, [CallerMemberName] string callerName = "") { if (!PInvokeDebugger.LoggingEnabled) { return(PInvoke_Process32Next(hSnapshot, ref pe32)); } bool returnValue = PInvoke_Process32Next(hSnapshot, ref pe32); PInvokeDebugInfo debugInfo = PInvokeDebugInfo.TraceDebugInfo( ModuleName, nameof(Process32Next), callerName, returnValue, false, nameof(hSnapshot), hSnapshot, nameof(pe32), pe32 ); PInvokeDebugger.SafeCapture(debugInfo); return(returnValue); }
public static ProcessEntry32[] GetProcesses(string ExeName) { System.Collections.ArrayList pal = new System.Collections.ArrayList(); int handle = CreateToolhelp32Snapshot(2, 0); if (handle > 0) { try { StringBuilder sb = new StringBuilder(" ", 256); ProcessEntry32 pe32 = new ProcessEntry32(); pe32.szExeFile = sb.ToString(); uint size = 548; // sizeof ints + twice size of sb (thanks to unicode) pe32.dwSize = (uint)size; int retval = Process32First(handle, ref pe32); while (retval == 1) { if (pe32.szExeFile.ToLower() == ExeName.ToLower()) { pal.Add(pe32); } size = 548; pe32.dwSize = size; retval = Process32Next(handle, ref pe32); } return((ProcessEntry32[])pal.ToArray(typeof(ProcessEntry32))); } finally { CloseHandle(handle); } } throw new Exception("Could not create snapshot."); }
/// <summary> /// Takes a snap shot of processes running on the computer, and searches for a /// specified process name [name.exe]. /// </summary> /// <param name="pname">Process name to be searched for.</param> /// <returns>true if process found and false otherwise.</returns> public static bool FindProcessByName(string pname) { int SnapShot = CreateToolhelp32Snapshot(0x00000002, 0); ProcessEntry32 pe32 = new ProcessEntry32(); pe32.dwSize = 296; while (Process32Next(SnapShot, ref pe32) != 0) { string xname = pe32.szExeFile.ToString(); if (pname.CompareTo(xname) == 0) { CloseHandle(SnapShot); return(true); } } CloseHandle(SnapShot); return(false); }
public static ProcessEntry32 GetProcess(int PID) { int handle = CreateToolhelp32Snapshot(2, 0); if (handle > 0) { try { StringBuilder sb = new StringBuilder(" ", 256); ProcessEntry32 pe32 = new ProcessEntry32(); pe32.szExeFile = sb.ToString(); uint size = 548; // sizeof ints + twice size of sb (thanks to unicode) pe32.dwSize = (uint)size; int retval = Process32First(handle, ref pe32); while (retval == 1) { if (pe32.th32ProcessID == PID) { return(pe32); } size = 548; pe32.dwSize = size; retval = Process32Next(handle, ref pe32); } } finally { CloseHandle(handle); } throw new Exception("Process with PID=" + PID.ToString() + " not found."); } throw new Exception("Could not create snapshot."); }
private static void GetChildProcesses(Process process, List <Process> children) { IntPtr snapShotHandle = CreateToolhelp32Snapshot(SnapshotFlags.TH32CS_SNAPPROCESS, 0); try { var entry = new ProcessEntry32(); entry.dwSize = (uint)Marshal.SizeOf(typeof(ProcessEntry32)); int result = Process32First(snapShotHandle, ref entry); while (result != 0) { if (process.Id == entry.th32ParentProcessID) { try { var child = Process.GetProcessById((int)entry.th32ProcessID); children.Add(child); GetChildProcesses(child, children); } catch (ArgumentException) { // process might have exited since the snapshot, ignore it } } result = Process32Next(snapShotHandle, ref entry); } } finally { if (snapShotHandle != IntPtr.Zero) { CloseHandle(snapShotHandle); } } }
public static extern int Process32Next(int handle, ref ProcessEntry32 pe);
private static extern bool Process32Next([In] IntPtr snapshot, [In, Out] ref ProcessEntry32 processEntry);
private static extern int Process32Next(SnapshotSafeHandle handle, ref ProcessEntry32 entry);
public static extern bool Process32Next(IntPtr snapshot, ref ProcessEntry32 entry);
public static extern int Process32First(IntPtr handle, ref ProcessEntry32 pe);
internal static extern bool Process32Next(IntPtr hSnapshot, ref ProcessEntry32 lppe);
private static extern int Process32Next(IntPtr handle, ref ProcessEntry32 entry);
public static int GetProcessIdByName(string processName) { IntPtr handle = CreateToolhelp32Snapshot(0x2, 0); if ((int)handle > 0) { List<ProcessEntry32> list = new List<ProcessEntry32>(); ProcessEntry32 pe32 = new ProcessEntry32(); pe32.dwSize = (uint)Marshal.SizeOf(pe32); int bMore = Process32First(handle, ref pe32); while (bMore == 1) { IntPtr temp = Marshal.AllocHGlobal((int)pe32.dwSize); Marshal.StructureToPtr(pe32, temp, true); ProcessEntry32 pe = (ProcessEntry32)Marshal.PtrToStructure(temp, typeof(ProcessEntry32)); Marshal.FreeHGlobal(temp); list.Add(pe); bMore = Process32Next(handle, ref pe32); } CloseHandle(handle); foreach (ProcessEntry32 p in list) { if(p.szExeFile.Equals(processName)) return (int)p.th32ProcessID; } } return 0; }
[DllImport("KERNEL32.DLL")] //[DllImport("toolhelp.dll") public static extern int Process32Next(int handle, ref ProcessEntry32 pe);
private static extern int Process32Next(int handle, ref ProcessEntry32 pe);
internal static extern bool PInvoke_Process32Next(IntPtr hSnapshot, ref ProcessEntry32 pe32);
public ProcessTree(ProcessEntry32 process) { processName = process.szExeFile; processId = (int)process.th32ProcessID; parrentId = (int)process.th32ParentProcessID; }
//Create integration test that defines current process count public static uint[] GetRunningProcessIDsSnapshot() { List <uint> processes = new List <uint>(); int processCount = 0; using (var toolhelpHandler = ToolHelp.CreateToolhelp32Snapshot(ToolHelpFlags.TH32CS_SNAPPROCESS | ToolHelpFlags.TH32CS_SNAPNOHEAPS, 0)) { ProcessEntry32 processEntry32 = new ProcessEntry32 { dwSize = (uint)Marshal.SizeOf(typeof(ProcessEntry32)) }; var success = ToolHelp.Process32First(toolhelpHandler, ref processEntry32); if (success) { //catch code there } Console.WriteLine("Error" + Marshal.GetLastWin32Error()); while (ToolHelp.Process32Next(toolhelpHandler, ref processEntry32)) { processes.Add(processEntry32.th32ProcessID); processCount++; //Console.WriteLine("Process id: " + processEntry32.th32ProcessID + " Process Name: " + processEntry32.szExeFile); } Console.WriteLine("Process count: " + processCount); } return(processes.ToArray()); }