示例#1
0
        internal static void SetServiceStartType(string serviceName, ServiceNativeMethods.SERVICE_STARTTYPE type)
        {
            //open service control manager
            using (SafeServiceHandle scManager = ServiceNativeMethods.OpenSCManager(null, null, ServiceNativeMethods.SC_MANAGER_ACCESS.GENERIC_READ))
            {
                int errorCode = Marshal.GetLastWin32Error();
                if (scManager.IsInvalid)
                {
                    throw new System.ComponentModel.Win32Exception(errorCode);
                }

                // open service
                using (SafeServiceHandle service = ServiceNativeMethods.OpenService(scManager, serviceName, ServiceNativeMethods.SERVICE_ACCESS.GENERIC_WRITE))
                {
                    errorCode = Marshal.GetLastWin32Error();
                    if (service.IsInvalid)
                    {
                        throw new System.ComponentModel.Win32Exception(errorCode);
                    }

                    // change start type
                    if (!ServiceNativeMethods.ChangeServiceConfig(service, ServiceNativeMethods.SERVICE_TYPE.SERVICE_NO_CHANGE, type,
                                                                  ServiceNativeMethods.SERVICE_ERRORCONTROL.SERVICE_NO_CHANGE, null, null, IntPtr.Zero, null, null, null, null))
                    {
                        errorCode = Marshal.GetLastWin32Error();
                        throw new System.ComponentModel.Win32Exception(errorCode);
                    }
                }
            }
        }
示例#2
0
        internal static ServiceNativeMethods.SERVICE_STARTTYPE GetServiceStartType(string serviceName, string server = null)
        {
            ServiceNativeMethods.QUERY_SERVICE_CONFIG config = new ServiceNativeMethods.QUERY_SERVICE_CONFIG();

            //open service control manager
            using (SafeServiceHandle scManager = ServiceNativeMethods.OpenSCManager(server, null, ServiceNativeMethods.SC_MANAGER_ACCESS.GENERIC_READ))
            {
                int errorCode = Marshal.GetLastWin32Error();
                if (scManager.IsInvalid)
                {
                    throw new System.ComponentModel.Win32Exception(errorCode);
                }

                // open service
                using (SafeServiceHandle service = ServiceNativeMethods.OpenService(scManager, serviceName, ServiceNativeMethods.SERVICE_ACCESS.GENERIC_READ))
                {
                    errorCode = Marshal.GetLastWin32Error();
                    if (service.IsInvalid)
                    {
                        throw new System.ComponentModel.Win32Exception(errorCode);
                    }

                    //query size of buffer needed for configuration information
                    uint bufSize = 0;
                    uint bytesNeeded;
                    using (SafeGlobalMemoryHandle invalidHandle = SafeGlobalMemoryHandle.InvalidHandle)
                    {
                        if (!ServiceNativeMethods.QueryServiceConfig(service, invalidHandle, bufSize, out bytesNeeded))
                        {
                            errorCode = Marshal.GetLastWin32Error();
                            if (errorCode != ServiceNativeMethods.ERROR_INSUFFICIENT_BUFFER)
                            {
                                throw new System.ComponentModel.Win32Exception(errorCode);
                            }
                        }
                    }

                    //allocate buffer and query configurations
                    using (SafeGlobalMemoryHandle buffer = new SafeGlobalMemoryHandle((int)bytesNeeded))
                    {
                        bufSize = bytesNeeded;
                        if (!ServiceNativeMethods.QueryServiceConfig(service, buffer, bufSize, out bytesNeeded))
                        {
                            errorCode = Marshal.GetLastWin32Error();
                            throw new System.ComponentModel.Win32Exception(errorCode);
                        }

                        config = (ServiceNativeMethods.QUERY_SERVICE_CONFIG)buffer.ToStructure(typeof(ServiceNativeMethods.QUERY_SERVICE_CONFIG));
                    }
                }
            }

            return(config.startType);
        }
示例#3
0
 protected override bool ReleaseHandle()
 {
     return(ServiceNativeMethods.CloseServiceHandle(this.handle));
 }