Ejemplo n.º 1
0
        /// <summary>
        /// Send a stop command to a Service and wait until the Service is stopped
        /// </summary>
        /// <param name="ServiceName"></param>
        /// <returns>Returns an ArrayList of Services which has stopped because of service dependencies </returns>
        public ArrayList StopService(string ServiceName)
        {
            try
            {
                ManagementObjectCollection Dependencies;
                ManagementObject           Service;
                WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
                oProv.mScope.Path.NamespacePath       = @"root\cimv2";
                oProv.mScope.Options.EnablePrivileges = true;
                Service      = oProv.GetObject("Win32_Service.Name='" + ServiceName + "'");
                Dependencies = oProv.ExecuteQuery("Associators of {Win32_Service.Name='" + ServiceName + "'} Where AssocClass=Win32_DependentService Role=Antecedent");

                ArrayList Result = new ArrayList();
                foreach (ManagementObject MO in Dependencies)
                {
                    if (MO.GetPropertyValue("State").ToString().ToLower() == "running")
                    {
                        Result.AddRange(StopService(MO.GetPropertyValue("Name").ToString()));
                    }
                }
                Result.Add(ServiceName);
                bStopService(Service);
                return(Result);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Get the Win32_Service ManagementObject of a Service
        /// </summary>
        /// <param name="ServiceName"></param>
        /// <returns>Win32_Service ManagementObject</returns>
        public ManagementObject GetService(String ServiceName)
        {
            WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());

            oProv.mScope.Path.NamespacePath = @"root\cimv2";

            return(oProv.GetObject("Win32_Service.Name='" + ServiceName + "'"));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Delete a single File
        /// </summary>
        /// <param name="FilePath"></param>
        public void DeleteFile(string FilePath)
        {
            WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());

            oProv.mScope.Path.NamespacePath = @"root\cimv2";
            ManagementObject MO = oProv.GetObject("CIM_DataFile.Name='" + FilePath + "'");

            MO.InvokeMethod("Delete", null);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Kill a process by ProcessID
        /// </summary>
        /// <param name="ProcessID"></param>
        /// <returns></returns>
        public int KillProcess(int ProcessID)
        {
            WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());

            oProv.mScope.Path.NamespacePath       = @"root\cimv2";
            oProv.mScope.Options.EnablePrivileges = true;
            ManagementObject     MO       = oProv.GetObject("Win32_Process.Handle='" + ProcessID.ToString() + "'");
            ManagementBaseObject inParams = MO.GetMethodParameters("Terminate");
            ManagementBaseObject Res      = MO.InvokeMethod("Terminate", inParams, null);

            return(int.Parse(Res.GetPropertyValue("ReturnValue").ToString()));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Set the Service start mode
        /// </summary>
        /// <param name="ServiceName"></param>
        /// <param name="StartMode">
        /// <list type="table">
        /// <listheader><term>Value</term><description>Meaning</description></listheader>
        /// <item><term>Boot</term><description>Device driver started by the operating system loader. This value is valid only for driver services.</description></item>
        /// <item><term>System</term><description>Device driver started by the operating system initialization process. This value is valid only for driver services.</description></item>
        /// <item><term>Automatic</term><description>Service to be started automatically by the service control manager during system startup.</description></item>
        /// <item><term>Manual</term><description>Service to be started by the service control manager when a process calls the StartService method.</description></item>
        /// <item><term>Disabled</term><description>Service that can no longer be started.</description></item>
        /// </list>
        /// </param>
        /// <returns></returns>
        public int SetServiceStartMode(string ServiceName, string StartMode)
        {
            WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());

            oProv.mScope.Path.NamespacePath = @"root\cimv2";
            ManagementObject     Service  = oProv.GetObject("Win32_Service.Name='" + ServiceName + "'");
            ManagementBaseObject inParams = Service.GetMethodParameters("ChangeStartMode");

            inParams["StartMode"] = StartMode;
            ManagementBaseObject Result = Service.InvokeMethod("ChangeStartMode", inParams, null);

            return(int.Parse(Result.GetPropertyValue("ReturnValue").ToString()));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Start a Service
        /// </summary>
        /// <param name="ServiceName"></param>
        /// <returns>Return Value</returns>
        public int StartService(String ServiceName)
        {
            ManagementObject Service;

            try
            {
                ManagementEventWatcher watcher = new ManagementEventWatcher();
                lock (oWMIProvider)
                {
                    WMIProvider oProv = new WMIProvider(oWMIProvider.mScope.Clone());
                    oProv.mScope.Path.NamespacePath       = @"root\cimv2";
                    oProv.mScope.Options.EnablePrivileges = true;
                    Service = oProv.GetObject("Win32_Service.Name='" + ServiceName + "'");

                    WqlEventQuery query = new WqlEventQuery("__InstanceModificationEvent");
                    query.Condition      = "TargetInstance ISA 'Win32_Service' AND TargetInstance.Name='" + ServiceName + "' AND TargetInstance.State='Running'";
                    query.WithinInterval = new TimeSpan(0, 0, 2);
                    watcher = new ManagementEventWatcher(oProv.mScope, query);
                    watcher.EventArrived += new EventArrivedEventHandler(ServiceEventArrivedHandler);
                }
                //watcher.Options.Timeout = new TimeSpan(0, 0, 15);
                watcher.Start();

                Object result  = Service.InvokeMethod("StartService", null);
                int    iResult = int.Parse(result.ToString());
                if (iResult == 0)
                {
                    MRE.WaitOne(new TimeSpan(0, 0, 30), true);
                }
                watcher.Stop();
                watcher.Dispose();

                Service.Get();


                if (Service["State"].ToString() == "Running")
                {
                    return(iResult);
                }
                else
                {
                    return(iResult);
                }
            }
            catch (Exception ex)
            {
                //0x80080005 wmi stopped
                throw (ex);
            }
        }