/// <summary>
        /// Create a ServiceHandle instance
        /// </summary>
        /// <param name="serviceName">The disired service name</param>
        /// <param name="desiredAccess">The disired access rights</param>
        /// <returns>ServiceHandle instance</returns>
        public ServiceHandle OpenService(string serviceName, Advapi32.ServiceAccessRights desiredAccess)
        {
            ServiceHandle serviceHandle = Advapi32.OpenService
                                          (
                this,
                serviceName,
                desiredAccess
                                          );

            if (serviceHandle.IsInvalid)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            return(serviceHandle);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new service and return his handle
        /// </summary>
        /// <param name="serviceDefinition">The services definitions</param>
        /// <returns>A ServiceHandle instance</returns>
        public ServiceHandle CreateService(IWin32ServiceDefinition serviceDefinition)
        {
            IntPtr passwordHandle = IntPtr.Zero;

            if (!ValidServiceName(serviceDefinition.ServiceName))
            {
                throw new ArgumentException("The given service name is not a valid name.");
            }

            //Create the service type
            var serviceType = Advapi32.ServiceType.Win32OwnProcess; //DM only supports Win32OwnProcess

            if (Equals(serviceDefinition.Credentials, ServiceCredentials.LocalSystem) && serviceDefinition.CanInteractWithDesktop)
            {
                serviceType |= Advapi32.ServiceType.InteractivProcess;
            }

            //The credentials can't be null
            if (serviceDefinition.Credentials == null)
            {
                throw new ArgumentNullException(nameof(serviceDefinition.Credentials));
            }

            try
            {
                //Only call marshal if a password is set (SecureString != null), otherwise leave IntPtr.Zero
                if (serviceDefinition.Credentials.Password != null)
                {
                    passwordHandle = Marshal.SecureStringToGlobalAllocUnicode(serviceDefinition.Credentials.Password);
                }

                ServiceHandle serviceHandle = Advapi32.CreateService
                                              (
                    this,
                    serviceDefinition.ServiceName,
                    serviceDefinition.DisplayName,
                    Advapi32.ServiceAccessRights.AllAccess,
                    serviceType,
                    serviceDefinition.StartType,
                    serviceDefinition.ErrorControl,
                    DmServiceExe,
                    serviceDefinition.LoadOrderGroup,
                    tagId: 0, //Tags are only evaluated for driver services that have SERVICE_BOOT_START or SERVICE_SYSTEM_START start types.
                    Advapi32.ConvertDependenciesArraysToDoubleNullTerminatedString(serviceDefinition.DependOnService, serviceDefinition.DependOnGroup),
                    serviceDefinition.Credentials.Username,
                    passwordHandle
                                              );

                if (serviceHandle.IsInvalid)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                return(serviceHandle);
            }
            finally
            {
                if (passwordHandle != IntPtr.Zero)
                {
                    Marshal.ZeroFreeGlobalAllocUnicode(passwordHandle);
                }
            }
        }