Ejemplo n.º 1
0
        private unsafe string? GetServiceDisplayName(SafeServiceHandle? scmHandle, string serviceName)
        {
            var builder = new ValueStringBuilder(4096);
            int bufLen;
            while (true)
            {
                bufLen = builder.Capacity;
                fixed (char* c = builder)
                {
                    if (Interop.Advapi32.GetServiceDisplayName(scmHandle, serviceName, c, ref bufLen))
                        break;
                }

                int lastError = Marshal.GetLastWin32Error();
                if (lastError == Interop.Errors.ERROR_SERVICE_DOES_NOT_EXIST)
                {
                    return null;
                }
                else if (lastError != Interop.Errors.ERROR_INSUFFICIENT_BUFFER)
                {
                    throw new InvalidOperationException(SR.Format(SR.NoService, serviceName, _machineName), new Win32Exception(lastError));
                }

                builder.EnsureCapacity(bufLen + 1); // Does not include null
            }

            builder.Length = bufLen;
            return builder.ToString();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Closes the handle to the service manager, but does not
        /// mark the class as disposed.
        /// </summary>
        /// <remarks>
        /// Violates design guidelines by not matching Dispose() -- matches .NET Framework
        /// </remarks>
        public void Close()
        {
            if (_serviceManagerHandle != null)
            {
                _serviceManagerHandle.Dispose();
                _serviceManagerHandle = null;
            }

            _statusGenerated = false;
            _startTypeInitialized = false;
            _type = Interop.Advapi32.ServiceTypeOptions.SERVICE_TYPE_ALL;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Closes the handle to the service manager, but does not
        /// mark the class as disposed.
        /// </summary>
        /// <remarks>
        /// Violates design guidelines by not matching Dispose() -- matches .NET Framework
        /// </remarks>
        public void Close()
        {
            if (_serviceManagerHandle != null)
            {
                _serviceManagerHandle.Dispose();
                _serviceManagerHandle = null;
            }

            _statusGenerated      = false;
            _startTypeInitialized = false;
            _type = AllServiceTypes;
        }
Ejemplo n.º 4
0
        private void GetDataBaseHandleWithConnectAccess()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            // get a handle to SCM with connect access and store it in serviceManagerHandle field.
            if (_serviceManagerHandle == null)
            {
                _serviceManagerHandle = GetDataBaseHandleWithAccess(_machineName, Interop.Advapi32.ServiceControllerOptions.SC_MANAGER_CONNECT);
            }
        }
Ejemplo n.º 5
0
        private static SafeServiceHandle GetDataBaseHandleWithAccess(string machineName, int serviceControlManagerAccess)
        {
            SafeServiceHandle?databaseHandle = null;

            if (machineName.Equals(DefaultMachineName) || machineName.Length == 0)
            {
                databaseHandle = new SafeServiceHandle(Interop.Advapi32.OpenSCManager(null, null, serviceControlManagerAccess));
            }
            else
            {
                databaseHandle = new SafeServiceHandle(Interop.Advapi32.OpenSCManager(machineName, null, serviceControlManagerAccess));
            }

            if (databaseHandle.IsInvalid)
            {
                Exception inner = new Win32Exception(Marshal.GetLastWin32Error());
                throw new InvalidOperationException(SR.Format(SR.OpenSC, machineName), inner);
            }

            return(databaseHandle);
        }
 internal static extern unsafe bool GetServiceKeyName(SafeServiceHandle?SCMHandle, string displayName, char *KeyName, ref int KeyNameLength);
 internal static extern IntPtr OpenService(SafeServiceHandle?databaseHandle, string serviceName, int access);
 internal static unsafe partial bool GetServiceDisplayName(SafeServiceHandle?SCMHandle, string serviceName, char *displayName, ref int displayNameLength);