Exemple #1
0
        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);
        }
Exemple #2
0
        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));
        }
Exemple #3
0
        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));
        }
Exemple #4
0
        public static ServiceReturnCode CreateService(string serviceName, string machineName, string displayName, string description,
                                                      string pathName, ServiceStartMode startMode, string startName = null, string password                = 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;
            }

            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)
                {
                    UpdateServiceDescription(serviceName, machineName, description);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(result);
        }
Exemple #5
0
        //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);
        }