static ManagementObjectCollection GetProcessesByParentId(int pid, string machineName) { ObjectQuery query = new ObjectQuery( string.Format("SELECT * FROM Win32_Process WHERE ParentProcessId = '{0}'", (int)((UInt32)pid))); ServiceUtilWmiHelper helper = new ServiceUtilWmiHelper(machineName); return(helper.Query(query, WmiPath.Root)); }
static ManagementObjectCollection GetServicesByServiceName(string serviceName, string machineName) { ObjectQuery query = new ObjectQuery(string.Format( "SELECT * FROM Win32_Service WHERE name = '{0}'", serviceName)); ServiceUtilWmiHelper helper = new ServiceUtilWmiHelper(machineName); return(helper.Query(query, WmiPath.Root)); }
//query by ProcessName _and_ PId to guarantee it's the exact same Process & PId // (not a new PId with diff app, in case PId already got re-used). static int GetProcessesByProcessNamePId(string processName, int pid, string machineName) { int processId = 0; ObjectQuery query = new ObjectQuery( string.Format("SELECT ProcessId FROM Win32_Process WHERE Name = '{0}' AND ProcessId = '{1}'", processName, pid)); ServiceUtilWmiHelper helper = new ServiceUtilWmiHelper(machineName); ManagementObjectCollection processes = helper.Query(query, WmiPath.Root); foreach (ManagementObject process in processes) { object prId = process.GetPropertyValue("ProcessId"); try { processId = (int)((UInt32)prId); } catch { } } return(processId); }