Пример #1
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);
        }
Пример #2
0
 internal static extern bool QueryServiceConfig(SafeServiceHandle service, SafeGlobalMemoryHandle pServiceConfig, uint bufSize, out uint bytesNeeded);