/// <summary>
        /// 停止驱动程序服务
        /// </summary>
        /// <param name="svcName"></param>
        /// <returns></returns>
        public bool StopDriver(String svcName)
        {
            IntPtr scManagerHandle;
            IntPtr schDriverService;

            DriverEntity.SERVICE_STATUS serviceStatus;

            //打开服务控制台管理器
            scManagerHandle = DriverDAL.OpenSCManager(null, null, DriverEntity.SC_MANAGER_CREATE_SERVICE);
            if (null == scManagerHandle || IntPtr.Zero == scManagerHandle)
            {
                return false;
            }

            //打开服务
            schDriverService = DriverDAL.OpenService(scManagerHandle, svcName, DriverEntity.SERVICE_ALL_ACCESS);
            if (null == schDriverService || IntPtr.Zero == schDriverService)
            {
                DriverDAL.CloseServiceHandle(scManagerHandle);
                return false;
            }

            serviceStatus = new DriverEntity.SERVICE_STATUS();

            //停止服务
            if (false == DriverDAL.ControlService(schDriverService, DriverEntity.SERVICE_CONTROL_STOP, ref serviceStatus))
            {
                DriverDAL.CloseServiceHandle(schDriverService);
                DriverDAL.CloseServiceHandle(scManagerHandle);

                return false;
            }
            else
            {
                DriverDAL.CloseServiceHandle(schDriverService);
                DriverDAL.CloseServiceHandle(scManagerHandle);

                return true;
            }
        }
Example #2
0
        /// <summary>
        /// 停止驱动程序服务
        /// </summary>
        /// <param name="svcName"></param>
        /// <returns></returns>
        public bool StopDriver(String svcName)
        {
            IntPtr scManagerHandle;
            IntPtr schDriverService;

            DriverEntity.SERVICE_STATUS serviceStatus;

            //打开服务控制台管理器
            scManagerHandle = DriverDAL.OpenSCManager(null, null, DriverEntity.SC_MANAGER_CREATE_SERVICE);
            if (null == scManagerHandle || IntPtr.Zero == scManagerHandle)
            {
                return(false);
            }

            //打开服务
            schDriverService = DriverDAL.OpenService(scManagerHandle, svcName, DriverEntity.SERVICE_ALL_ACCESS);
            if (null == schDriverService || IntPtr.Zero == schDriverService)
            {
                DriverDAL.CloseServiceHandle(scManagerHandle);
                return(false);
            }

            serviceStatus = new DriverEntity.SERVICE_STATUS();

            //停止服务
            if (false == DriverDAL.ControlService(schDriverService, DriverEntity.SERVICE_CONTROL_STOP, ref serviceStatus))
            {
                DriverDAL.CloseServiceHandle(schDriverService);
                DriverDAL.CloseServiceHandle(scManagerHandle);

                return(false);
            }
            else
            {
                DriverDAL.CloseServiceHandle(schDriverService);
                DriverDAL.CloseServiceHandle(scManagerHandle);

                return(true);
            }
        }
 public static extern bool ControlService(
     IntPtr hService,
     UInt32 dwControl,
     ref DriverEntity.SERVICE_STATUS lpServiceStatus
     );