示例#1
0
        public void StartStopService()
        {
            using (var scm = AdvApi32.OpenSCManager(null, null, ScManagerAccessTypes.SC_MANAGER_CONNECT))
            {
                AssertHandleIsValid(scm);

                var access = ServiceAccessTypes.SERVICE_START | ServiceAccessTypes.SERVICE_STOP | ServiceAccessTypes.SERVICE_QUERY_STATUS;

                //opens print spooler service
                using (var service = AdvApi32.OpenService(scm, "Spooler", access))
                {
                    AssertHandleIsValid(service);

                    //query service status
                    var status = AdvApi32.QueryServiceStatusEx <SERVICE_STATUS_PROCESS>(service, SC_STATUS_TYPE.SC_STATUS_PROCESS_INFO);

                    if (status.dwCurrentState == ServiceState.SERVICE_RUNNING)
                    {
                        var ret4 = AdvApi32.StopService(service, out var _);
                        if (!ret4)
                        {
                            Win32Error.ThrowLastError();
                        }

                        WaitForServiceStatus(service, ServiceState.SERVICE_STOPPED);

                        var ret6 = AdvApi32.StartService(service);
                        if (!ret6)
                        {
                            Win32Error.ThrowLastError();
                        }
                    }
                    else
                    {
                        var ret4 = AdvApi32.StartService(service);
                        if (!ret4)
                        {
                            Win32Error.ThrowLastError();
                        }

                        WaitForServiceStatus(service, ServiceState.SERVICE_RUNNING);

                        var ret6 = AdvApi32.StopService(service, out var _);
                        if (!ret6)
                        {
                            Win32Error.ThrowLastError();
                        }
                    }
                }
            }
        }
示例#2
0
        public bool Install(string path, out string errorMessage)
        {
            IntPtr manager = AdvApi32.OpenSCManager(null, null, AdvApi32.SC_MANAGER_ACCESS_MASK.SC_MANAGER_ALL_ACCESS);

            if (manager == IntPtr.Zero)
            {
                errorMessage = "OpenSCManager returned zero.";
                return(false);
            }

            IntPtr service = AdvApi32.CreateService(manager,
                                                    _id,
                                                    _id,
                                                    AdvApi32.SERVICE_ACCESS_MASK.SERVICE_ALL_ACCESS,
                                                    AdvApi32.SERVICE_TYPE.SERVICE_KERNEL_DRIVER,
                                                    AdvApi32.SERVICE_START.SERVICE_DEMAND_START,
                                                    AdvApi32.SERVICE_ERROR.SERVICE_ERROR_NORMAL,
                                                    path,
                                                    null,
                                                    null,
                                                    null,
                                                    null,
                                                    null);

            if (service == IntPtr.Zero)
            {
                if (Marshal.GetHRForLastWin32Error() == Kernel32.ERROR_SERVICE_EXISTS)
                {
                    errorMessage = "Service already exists";
                    return(false);
                }

                errorMessage = "CreateService returned the error: " + Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()).Message;
                AdvApi32.CloseServiceHandle(manager);
                return(false);
            }

            if (!AdvApi32.StartService(service, 0, null))
            {
                if (Marshal.GetHRForLastWin32Error() != Kernel32.ERROR_SERVICE_ALREADY_RUNNING)
                {
                    errorMessage = "StartService returned the error: " + Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()).Message;
                    AdvApi32.CloseServiceHandle(service);
                    AdvApi32.CloseServiceHandle(manager);
                    return(false);
                }
            }

            AdvApi32.CloseServiceHandle(service);
            AdvApi32.CloseServiceHandle(manager);

#if NETFRAMEWORK
            try
            {
                // restrict the driver access to system (SY) and builtin admins (BA)
                // TODO: replace with a call to IoCreateDeviceSecure in the driver
                FileSecurity fileSecurity = File.GetAccessControl(@"\\.\" + _id);
                fileSecurity.SetSecurityDescriptorSddlForm("O:BAG:SYD:(A;;FA;;;SY)(A;;FA;;;BA)");
                File.SetAccessControl(@"\\.\" + _id, fileSecurity);
            }
            catch
            { }
#endif
            errorMessage = null;
            return(true);
        }