示例#1
0
        /// <summary>
        /// Install a Windows service.
        /// </summary>
        /// <param name="machineName">
        /// The name of the target computer. If the value is null or an empty
        /// string, the function connects to the SCM on the local machine.
        /// </param>
        /// <param name="serviceName">
        /// The name of the service to install. The maximum string length is 256
        /// characters.
        /// </param>
        /// <param name="displayName">
        /// The display name to be used by user interface programs to identify the
        /// service. This string has a maximum length of 256 chars.
        /// </param>
        /// <param name="dwStartType">
        /// The service start options. The parameter can be one of the following
        /// values: SERVICE_AUTO_START, SERVICE_BOOT_START, SERVICE_DEMAND_START,
        /// SERVICE_DISABLED, SERVICE_SYSTEM_START.
        /// </param>
        /// <param name="binaryPath">
        /// The full path to the service binary file.
        /// </param>
        /// <param name="serviceStartName">
        /// The name of the account under which the service should run. If this
        /// parameter is NULL, CreateService uses the LocalSystem account. The
        /// parameter can also be "NT AUTHORITY\LocalService",
        /// "NT AUTHORITY\NetworkService", or in the form of "DomainName\UserName",
        /// or ".\UserName".
        /// </param>
        /// <param name="password">
        /// The password to the account name specified by the serviceStartName
        /// parameter. Specify an empty string if the account has no password or if
        /// the service runs in the LocalService, NetworkService, or LocalSystem
        /// account.
        /// </param>
        /// <returns></returns>
        public static void InstallService(string machineName, string serviceName,
                                          string displayName, ServiceStart dwStartType, string binaryPath,
                                          string serviceStartName, string password)
        {
            SafeServiceHandle schSCManager = null;
            SafeServiceHandle schService   = null;

            try
            {
                // Get a handle to the SCM database.
                schSCManager = NativeMethods.OpenSCManager(
                    machineName,
                    null,
                    ServiceControlAccessRights.SC_MANAGER_CREATE_SERVICE);
                if (schSCManager.IsInvalid)
                {
                    throw new Win32Exception();
                }

                // Create the service.
                schService = NativeMethods.CreateService(
                    schSCManager,
                    serviceName,
                    displayName,
                    ServiceAccessRights.SERVICE_ALL_ACCESS,
                    ServiceType.SERVICE_WIN32_OWN_PROCESS,
                    dwStartType,
                    ServiceError.SERVICE_ERROR_NORMAL,
                    binaryPath,
                    null,
                    null,
                    null,
                    serviceStartName,
                    password);
                if (schService.IsInvalid)
                {
                    throw new Win32Exception();
                }
            }
            finally
            {
                if (schSCManager != null)
                {
                    schSCManager.Close();
                }
                if (schService != null)
                {
                    schService.Close();
                }
            }
        }
 public static extern SafeServiceHandle CreateService(
     SafeServiceHandle hSCManager,
     string lpServiceName,
     string lpDisplayName,
     ServiceAccessRights dwDesiredAccess,
     ServiceType dwServiceType,
     ServiceStart dwStartType,
     ServiceError dwErrorControl,
     string lpBinaryPathName,
     string lpLoadOrderGroup,
     string lpdwTagId,
     string lpDependencies,
     string lpServiceStartName,
     string lpPassword);
 public static extern bool DeleteService(SafeServiceHandle hService);
 public static extern SafeServiceHandle OpenService(
     SafeServiceHandle hSCManager,
     string lpServiceName,
     ServiceAccessRights dwDesiredAccess);