Ejemplo n.º 1
0
        private static void StopServiceExe(string hostname)
        {
            ServiceControlHandle con = NativeMethods.OpenSCManager(hostname, null, NativeMethods.SCM_ACCESS.SC_MANAGER_ALL_ACCESS);
            ServiceControlHandle m_serv = NativeMethods.OpenService(con, GlobalVars.ServiceName, NativeMethods.SERVICE_ACCESS.SERVICE_ALL_ACCESS);
            NativeMethods.SERVICE_STATUS stat = new NativeMethods.SERVICE_STATUS();
            bool res = NativeMethods.ControlService(m_serv, NativeMethods.SERVICE_CONTROL.STOP, ref stat);
            Console.WriteLine("[*] Stop service result: " + res);
            // TODO: Code to halt execution until the service has finally stopped, to continue another task afterwards.
            while (!res)
            {
                res = NativeMethods.QueryServiceStatus(m_serv, ref stat);
                if (stat.dwCurrentState == NativeMethods.SERVICE_STATE.SERVICE_STOPPED)
                {
                    break;
                }
                if (!res)
                {
                    int error = Marshal.GetLastWin32Error();
                    if (error == 0x6)
                    {
                        Console.WriteLine("[*] StopServiceExe ERROR_INVALID_HANDLE");
                        break;
                    }
                    if (error == 0x5)
                    {
                        Console.WriteLine("[*] StopServiceExe ERROR_ACCESS_DENIED");
                        break;
                    }

                }
                Console.WriteLine("[*] Waiting for it to stop ...");
                Thread.Sleep(5);
            }

        }
Ejemplo n.º 2
0
        private void UpdateServiceProgress(string serviceName)
        {
            int progress        = 0;
            var maxProgressList = new List <int>()
            {
                119, 120, 744, 745, 746
            };
            int  maxProgressIndex = 0;
            int  maxProgress      = 747;
            bool tripped          = false;

            progressBar1.Maximum = maxProgress;
            try
            {
                IntPtr smHandle = NativeMethods.OpenSCManager(null, null, NativeMethods.ServiceAccess.ENUMERATE_SERVICE);
                if (smHandle != IntPtr.Zero)
                {
                    IntPtr svHandle = NativeMethods.OpenService(smHandle, serviceName, NativeMethods.ServiceAccess.ENUMERATE_SERVICE);
                    if (svHandle != IntPtr.Zero)
                    {
                        var servStat = new NativeMethods.SERVICE_STATUS();
                        while (servStat.dwCurrentState != NativeMethods.ServiceState.Running)
                        {
                            if (NativeMethods.QueryServiceStatus(svHandle, servStat))
                            {
                                progress += servStat.dwCheckPoint;
                                if (progress > maxProgress)
                                {
                                    maxProgressIndex++;
                                    if (maxProgressIndex < maxProgressList.Count)
                                    {
                                        maxProgress          = maxProgressList[maxProgressIndex];
                                        progressBar1.Maximum = maxProgress;
                                    }
                                    else
                                    {
                                        tripped = true;
                                    }
                                }


                                lblProgress.Text   = progress.ToString();
                                progressBar1.Value = progress;
                            }
                        }

                        NativeMethods.CloseServiceHandle(svHandle);
                    }
                    NativeMethods.CloseServiceHandle(smHandle);
                }
            }
            catch (System.Exception)
            {
            }
            if (tripped)
            {
                maxProgressList.Add(progress);
                Console.WriteLine("max progress {0}", progress);
            }
        }
Ejemplo n.º 3
0
        public static Exception ControlService(string serviceName, int controlCode)
        {
            Exception result = null;

            using (ServiceController serviceController = new ServiceController(serviceName))
            {
                using (SafeHandle serviceHandle = serviceController.ServiceHandle)
                {
                    NativeMethods.SERVICE_STATUS service_STATUS = default(NativeMethods.SERVICE_STATUS);
                    if (!NativeMethods.ControlService(serviceHandle, controlCode, ref service_STATUS))
                    {
                        int    lastWin32Error = Marshal.GetLastWin32Error();
                        string message        = string.Format("ControlService({0}, {1}) failed with error: {2}", serviceName, controlCode, lastWin32Error);
                        ServiceOperations.Tracer.TraceError(0L, message);
                        result = new Win32Exception(lastWin32Error, message);
                    }
                }
            }
            return(result);
        }