public static CProcess[] GetProcesses() { //temp ArrayList ArrayList procList = new ArrayList(); IntPtr handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if ((int)handle > 0) { try { PROCESSENTRY32 peCurrent; PROCESSENTRY32 pe32 = new PROCESSENTRY32(); //Get byte array to pass to the API calls byte[] peBytes = pe32.ToByteArray(); //Get the first process int retval = Process32First(handle, peBytes); while (retval == 1) { //Convert bytes to the class peCurrent = new PROCESSENTRY32(peBytes); //New instance of the Process class CProcess proc = new CProcess(new IntPtr((int)peCurrent.PID), peCurrent.Name, (int)peCurrent.ThreadCount, (int)peCurrent.BaseAddress); procList.Add(proc); retval = Process32Next(handle, peBytes); } } catch (Exception ex) { throw new Exception("Exception: " + ex.Message); } //Close handle CloseToolhelp32Snapshot(handle); return((CProcess[])procList.ToArray(typeof(CProcess))); } else { throw new Exception("Unable to create snapshot"); } }
static void Main(string[] args) { String ourProcess = "j9w.exe"; Program main = new Program(); CProcess Processes = new CProcess(); int current = System.Diagnostics.Process.GetCurrentProcess().Id; CProcess[] allProcesses = CProcess.GetProcesses(); foreach (CProcess theProcess in allProcesses) { //Console.WriteLine(theProcess.ProcessName.ToString()); if (theProcess.ProcessName.ToLower().Equals(ourProcess)) { theProcess.Kill(); break; } } }