/// <summary>
        /// Look up the Failure action information for a service
        /// </summary>
        /// <param name="serviceName">The service to look up</param>
        /// <returns>Service Failure Action information</returns>
        public static ServiceInformation GetSericeInformation(string serviceName)
        {
            UInt32 dwBytesNeeded;

            // Get a valid service handle
            IntPtr serviceHandle = LookupServiceHandle(serviceName, scope.Query);

            // Determine the buffer size needed
            bool sucess = QueryServiceConfig2(serviceHandle, SERVICE_CONFIG_FAILURE_ACTIONS, IntPtr.Zero, 0, out dwBytesNeeded);

            IntPtr ptr = Marshal.AllocHGlobal((int)dwBytesNeeded);

            sucess = QueryServiceConfig2(serviceHandle, SERVICE_CONFIG_FAILURE_ACTIONS, ptr, dwBytesNeeded, out dwBytesNeeded);

            if (false == sucess)
            {
                throw new System.Runtime.InteropServices.ExternalException(string.Format("Cannot find  SERVICE_FAILURE_ACTIONS struct. Last Error: {0}.", Marshal.GetLastWin32Error()));
            }

            SERVICE_FAILURE_ACTIONS failureActions = new SERVICE_FAILURE_ACTIONS();

            Marshal.PtrToStructure(ptr, failureActions);

            ServiceInformation serviceConfigInformation = new ServiceInformation((UInt32)failureActions.dwResetPeriod, failureActions.lpRebootMsg, failureActions.lpCommand, failureActions.cActions);

            int offset = 0;

            for (int i = 0; i < failureActions.cActions; i++)
            {
                ServiceFailureAction action = new ServiceFailureAction();
                action.Type  = (ServiceFailureActionType)Marshal.ReadInt32(failureActions.lpsaActions, offset);
                offset      += sizeof(Int32);
                action.Delay = (UInt32)Marshal.ReadInt32(failureActions.lpsaActions, offset);
                offset      += sizeof(Int32);
                serviceConfigInformation.Actions[i] = action;
            }

            // clean up
            Marshal.FreeHGlobal(ptr);
            if (serviceHandle != IntPtr.Zero)
            {
                CloseServiceHandle(serviceHandle);
            }

            return(serviceConfigInformation);
        }
        /// <summary>
        /// verify a service has the expected failure action details
        /// </summary>
        /// <param name="serviceName">service name</param>
        /// <param name="resetPeriodInDays">the reset period in days</param>
        /// <param name="failureActions">an ordered list of the expected failure action types</param>
        public static void VerifyServiceInformation(string serviceName, int resetPeriodInDays, ServiceFailureActionType[] failureActions)
        {
            ServiceInformation service = GetSericeInformation(serviceName);
            string             message = string.Empty;
            bool failed = false;

            if (resetPeriodInDays != service.ResetPeriodInDays)
            {
                failed   = true;
                message += string.Format("Reset Period is incoorect. Actual: {0}, Expected: {1}.\r\n", service.ResetPeriodInDays, resetPeriodInDays);
            }

            for (int i = 0; i < failureActions.Length; i++)
            {
                if (failureActions[i] != service.Actions[i].Type)
                {
                    failed   = true;
                    message += string.Format("FailureAction {0} is incorect. Actual: {1}, Expected: {2}. \r\n", i, service.Actions[i].Type.ToString(), failureActions[i].ToString());
                }
            }

            Assert.IsFalse(failed, message);
        }
Example #3
0
        /// <summary>
        /// Look up the Failure action information for a service
        /// </summary>
        /// <param name="serviceName">The service to look up</param>
        /// <returns>Service Failure Action information</returns>
        public static ServiceInformation GetSericeInformation(string serviceName)
        {
            UInt32 dwBytesNeeded;

            // Get a valid service handle
            IntPtr serviceHandle = LookupServiceHandle(serviceName, scope.Query);

            // Determine the buffer size needed
            bool sucess = QueryServiceConfig2(serviceHandle, SERVICE_CONFIG_FAILURE_ACTIONS, IntPtr.Zero, 0, out dwBytesNeeded);

            IntPtr ptr = Marshal.AllocHGlobal((int)dwBytesNeeded);
            sucess = QueryServiceConfig2(serviceHandle, SERVICE_CONFIG_FAILURE_ACTIONS, ptr, dwBytesNeeded, out dwBytesNeeded);

            if (false == sucess)
            {
                throw new System.Runtime.InteropServices.ExternalException(string.Format("Cannot find  SERVICE_FAILURE_ACTIONS struct. Last Error: {0}.", Marshal.GetLastWin32Error()));
            }

            SERVICE_FAILURE_ACTIONS failureActions = new SERVICE_FAILURE_ACTIONS();
            Marshal.PtrToStructure(ptr, failureActions);

            ServiceInformation serviceConfigInformation = new ServiceInformation((UInt32)failureActions.dwResetPeriod, failureActions.lpRebootMsg, failureActions.lpCommand, failureActions.cActions);

            int offset = 0;
            for (int i = 0; i < failureActions.cActions; i++)
            {
                ServiceFailureAction action = new ServiceFailureAction();
                action.Type = (ServiceFailureActionType)Marshal.ReadInt32(failureActions.lpsaActions, offset);
                offset += sizeof(Int32);
                action.Delay = (UInt32)Marshal.ReadInt32(failureActions.lpsaActions, offset);
                offset += sizeof(Int32);
                serviceConfigInformation.Actions[i] = action;
            }

            // clean up
            Marshal.FreeHGlobal(ptr);
            if (serviceHandle != IntPtr.Zero)
            {
                CloseServiceHandle(serviceHandle);
            }

            return serviceConfigInformation;
        }