Beispiel #1
0
        private Win32ServiceControlHandle CreateService(
            Win32ServiceControlHandle serviceControlManagerHandle,
            string name,
            string executablePath,
            NetworkCredential logOnAccount)
        {
            var serviceHandle = Win32ServiceControl.CreateService(
                serviceControlManagerHandle,
                name,
                name,
                Win32ServiceControl.SERVICE_ACCESS.SERVICE_ALL_ACCESS,
                Win32ServiceControl.SERVICE_TYPES.SERVICE_WIN32_OWN_PROCESS,
                Win32ServiceControl.SERVICE_START_TYPES.SERVICE_AUTO_START,
                Win32ServiceControl.SERVICE_ERROR_CONTROL.SERVICE_ERROR_NORMAL,
                executablePath,
                null,
                IntPtr.Zero,
                null,
                string.Format(@"{0}\{1}", logOnAccount.Domain, logOnAccount.UserName),
                logOnAccount.Password);

            if (serviceHandle.IsInvalid)
            {
                ThrowNewWin32Exception(
                    "Failed to create '{0}' on {1}.",
                    name,
                    this.hostName);
            }

            return(serviceHandle);
        }
Beispiel #2
0
        private void UpdateDescription(
            Win32ServiceControlHandle serviceHandle,
            string description)
        {
            if (string.IsNullOrWhiteSpace(description))
            {
                return;
            }

            var desc = new Win32ServiceControl.SERVICE_DESCRIPTION
            {
                lpDescription = description
            };

            int returnValue = Win32ServiceControl.ChangeServiceDescription(
                serviceHandle,
                Win32ServiceControl.INFO_LEVEL.SERVICE_CONFIG_DESCRIPTION,
                ref desc);

            if (returnValue == 0)
            {
                ThrowNewWin32Exception(
                    "Failed to update the description to '{0}' on {1}.",
                    description,
                    this.hostName);
            }
        }
Beispiel #3
0
        private void DeleteService(Win32ServiceControlHandle serviceHandle)
        {
            int returnValue = Win32ServiceControl.DeleteService(serviceHandle);

            if (returnValue == 0)
            {
                ThrowNewWin32Exception(
                    "Failed to remove service on {0}.",
                    this.hostName);
            }
        }
 public static extern Win32ServiceControlHandle CreateService(
     Win32ServiceControlHandle serviceControlManagerHandle,
     string lpSvcName,
     string lpDisplayName,
     SERVICE_ACCESS dwDesiredAccess,
     SERVICE_TYPES dwServiceType,
     SERVICE_START_TYPES dwStartType,
     SERVICE_ERROR_CONTROL dwErrorControl,
     string lpPathName,
     string lpLoadOrderGroup,
     IntPtr lpdwTagId,
     string lpDependencies,
     string lpServiceStartName,
     string lpPassword);
Beispiel #5
0
        private Win32ServiceControlHandle OpenService(
            Win32ServiceControlHandle serviceControlManagerHandle,
            string name)
        {
            var serviceHandle = Win32ServiceControl.OpenService(
                serviceControlManagerHandle,
                name,
                Win32ServiceControl.SERVICE_ACCESS.SERVICE_ALL_ACCESS);

            if (serviceHandle.IsInvalid)
            {
                ThrowNewWin32Exception(
                    "Failed to get a handle on '{0}' on {1}.",
                    name,
                    this.hostName);
            }

            return(serviceHandle);
        }
Beispiel #6
0
        private void UpdateFailureActions(Win32ServiceControlHandle serviceHandle)
        {
            const int DelayMilliseconds = 60 * 1000;
            const int ActionCount       = 3;
            var       actions           = new int[ActionCount * 2];
            int       i = 0;

            actions[i++] = (int)Win32ServiceControl.ACTION_TYPE.SC_ACTION_RESTART;
            actions[i++] = DelayMilliseconds;
            actions[i++] = (int)Win32ServiceControl.ACTION_TYPE.SC_ACTION_RESTART;
            actions[i++] = DelayMilliseconds;
            actions[i++] = (int)Win32ServiceControl.ACTION_TYPE.SC_ACTION_RESTART;
            actions[i++] = DelayMilliseconds;

            IntPtr buffer = Marshal.AllocHGlobal(ActionCount * 8);

            Marshal.Copy(actions, 0, buffer, ActionCount * 2);

            var failureActions = new Win32ServiceControl.SERVICE_FAILURE_ACTIONS
            {
                cActions      = ActionCount,
                dwResetPeriod = 4,
                lpCommand     = null,
                lpRebootMsg   = null,
                lpsaActions   = new IntPtr(buffer.ToInt32())
            };

            int returnValue = Win32ServiceControl.ChangeServiceFailureActions(
                serviceHandle,
                Win32ServiceControl.INFO_LEVEL.SERVICE_CONFIG_FAILURE_ACTIONS,
                ref failureActions);

            if (returnValue == 0)
            {
                ThrowNewWin32Exception("Failed to update failure actions.");
            }
        }
 public static extern int ChangeServiceFailureActions(
     Win32ServiceControlHandle hService,
     INFO_LEVEL dwInfoLevel,
     [MarshalAs(UnmanagedType.Struct)] ref SERVICE_FAILURE_ACTIONS lpInfo);
 public static extern int ChangeServiceDescription(
     Win32ServiceControlHandle serviceHandle,
     INFO_LEVEL dwInfoLevel,
     [MarshalAs(UnmanagedType.Struct)] ref SERVICE_DESCRIPTION lpInfo);
 public static extern int DeleteService(Win32ServiceControlHandle serviceHandle);
 public static extern Win32ServiceControlHandle OpenService(
     Win32ServiceControlHandle serviceControlManagerHandle,
     string lpSvcName,
     SERVICE_ACCESS dwDesiredAccess);
 public static extern int StartService(
     Win32ServiceControlHandle serviceHandle,
     int dwNumServiceArgs,
     string lpServiceArgVectors);