public static ServiceReturnCode DeleteService(string serviceName, string machineName) { ServiceReturnCode result = ServiceReturnCode.UnknownFailure; ServiceUtilWmiHelper helper = new ServiceUtilWmiHelper(machineName); ManagementObject service = helper.GetInstance(string.Format("Win32_Service.Name='{0}'", serviceName)); try { ManagementBaseObject mbo = service.InvokeMethod("Delete", null, null); result = (ServiceReturnCode)Enum.Parse(typeof(ServiceReturnCode), mbo["ReturnValue"].ToString()); } catch (ManagementException mex) { //if ErrorCode == ManagementStatus.NotFound, eat the error. result = ServiceReturnCode.ServiceNotFound; //otherwise, throw whatever happened if (mex.ErrorCode != ManagementStatus.NotFound) { throw mex; } } return(result); }
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 Name, DisplayName, PathName, StartName, ProcessId, State, Started, StartMode, AcceptStop FROM Win32_Service WHERE name = '{0}'", serviceName)); ServiceUtilWmiHelper helper = new ServiceUtilWmiHelper(machineName); return(helper.Query(query, WmiPath.Root)); }
public static ServiceReturnCode CreateService(string serviceName, string machineName, string displayName, string pathName, ServiceStartMode startMode, string startName = null, string password = null, string parameters = null, WindowsServiceType serviceType = WindowsServiceType.OwnProcess, ErrorControlAction errorControl = ErrorControlAction.UserIsNotified, bool interactWithDesktop = false, string loadOrderGroup = null, string[] loadOrderGroupDependencies = null, string[] svcDependencies = null) { ServiceReturnCode result = ServiceReturnCode.UnknownFailure; ServiceUtilWmiHelper helper = new ServiceUtilWmiHelper(machineName); ManagementClass mc = helper.GetManagementClass("Win32_Service"); ManagementBaseObject inparms = mc.GetMethodParameters("Create"); string execPath = pathName; if (startMode == ServiceStartMode.Unchanged) { startMode = ServiceStartMode.Automatic; } if (!string.IsNullOrEmpty(parameters)) { execPath = string.Format("{0} {1}", pathName, parameters); } inparms["Name"] = serviceName; inparms["DisplayName"] = displayName; inparms["PathName"] = execPath; inparms["ServiceType"] = serviceType; inparms["ErrorControl"] = errorControl; inparms["StartMode"] = startMode.ToString(); inparms["DesktopInteract"] = interactWithDesktop; inparms["StartName"] = startName; inparms["StartPassword"] = password; inparms["LoadOrderGroup"] = loadOrderGroup; inparms["LoadOrderGroupDependencies"] = loadOrderGroupDependencies; inparms["ServiceDependencies"] = svcDependencies; try { ManagementBaseObject mbo = mc.InvokeMethod("Create", inparms, null); result = (ServiceReturnCode)Enum.Parse(typeof(ServiceReturnCode), mbo["ReturnValue"].ToString()); if (result == ServiceReturnCode.Success) { UpdateServiceDescriptionWithVersion(serviceName, machineName, pathName); } } catch (Exception ex) { throw ex; } return(result); }
//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); }