Example #1
0
        public static ServiceManagerApi.SERVICE_FAILURE_ACTIONS ApiQueryServiceConfig2(
            string sServicename)
        {
            UInt32 dwBytesNeeded;

            //UInt32 INFINITE = 0xFFFFFFFF;
            ServiceManagerApi.SERVICE_FAILURE_ACTIONS failureActions = new ServiceManagerApi.SERVICE_FAILURE_ACTIONS();
            try
            {
                ApiOpenService(sServicename, ServiceManagerInteropWindows.SERVICE_QUERY_CONFIG);

                Logger.Log(string.Format("ServiceManagerWindowsWrapper.ApiQueryServiceConfig2(sHostname={0})", sServicename), Logger.ServiceManagerLoglevel);

                bool bSuccess = ServiceManagerInteropWindows.QueryServiceConfig2(phService,
                                                                                 ServiceManagerInteropWindows.SERVICE_CONFIG_FAILURE_ACTIONS,
                                                                                 IntPtr.Zero, 0, out dwBytesNeeded);
                if (!bSuccess)
                {
                    Logger.Log("ServiceManagerWindowsWrapper.ApiOpenService returns false", Logger.ServiceManagerLoglevel);
                }

                IntPtr ptr = Marshal.AllocHGlobal((int)dwBytesNeeded);
                bSuccess = ServiceManagerInteropWindows.QueryServiceConfig2(phService,
                                                                            ServiceManagerInteropWindows.SERVICE_CONFIG_FAILURE_ACTIONS,
                                                                            ptr, dwBytesNeeded, out dwBytesNeeded);

                Marshal.PtrToStructure(ptr, failureActions);

                ApiCloseServiceHandle(phService);

                Logger.Log("ServiceManagerWindowsWrapper.ApiOpnSCManager is Success", Logger.ServiceManagerLoglevel);
            }
            catch (Exception ex)
            {
                Logger.LogException("ServiceManagerWindowsWrapper.ApiOpnSCManager()", ex);
            }

            return(failureActions);
        }
Example #2
0
        public static bool ApiChangeServiceConfig2(string sServicename,
                                                   ServiceManagerApi.SERVICE_FAILURE_ACTIONS failureActions)
        {
            try
            {
                ApiOpenService(sServicename, ServiceManagerInteropWindows.SERVICE_CHANGE_CONFIG);

                IntPtr lpInfo = Marshal.AllocHGlobal(Marshal.SizeOf(failureActions));
                if (lpInfo == IntPtr.Zero)
                {
                    throw new Exception(String.Format("Unable to allocate memory, error was: 0x{0:X}", Marshal.GetLastWin32Error()));
                }

                Marshal.StructureToPtr(failureActions, lpInfo, false);

                if (!ServiceManagerInteropWindows.ChangeServiceConfig2(phService, (int)ServiceManagerInteropWindows.SERVICE_CONFIG_FAILURE_ACTIONS, lpInfo))
                {
                    Marshal.FreeHGlobal(lpInfo);
                    Logger.Log(String.Format("Error setting service config, error was: 0x{0:X}", Marshal.GetLastWin32Error()));
                    return(false);
                }

                Marshal.FreeHGlobal(lpInfo);

                Logger.Log("MyService: Service modification completed");

                ApiCloseServiceHandle(phService);
            }
            catch (Exception ex)
            {
                Logger.LogException("ApiChangeServiceConfig2()", ex);
                return(false);
            }

            return(true);
        }
        public void SetData()
        {
            cbFirstfailure.Items.AddRange(sFailureMsgs);
            cbSecondFailure.Items.AddRange(sFailureMsgs);
            cbSubsquentFailure.Items.AddRange(sFailureMsgs);

            cbFirstfailure.SelectedIndexChanged -= new EventHandler(this.cbFirstfailure_SelectedIndexChanged);
            cbSecondFailure.SelectedIndexChanged -= new EventHandler(this.cbFirstfailure_SelectedIndexChanged);
            cbSubsquentFailure.SelectedIndexChanged -= new EventHandler(this.cbFirstfailure_SelectedIndexChanged);

            cbFirstfailure.SelectedItem = cbSecondFailure.SelectedItem = cbSubsquentFailure.SelectedItem = sFailureMsgs[0];

            failureActions = ServiceManagerWindowsWrapper.ApiQueryServiceConfig2(serviceName);
            ServiceManagerApi.SC_ACTION[] actions = new ServiceManagerApi.SC_ACTION[failureActions.cActions];
            int offset = 0;
            for (int i = 0; i < failureActions.cActions; i++)
            {
                ServiceManagerApi.SC_ACTION action = new ServiceManagerApi.SC_ACTION();
                action.Type = Marshal.ReadInt32(failureActions.lpsaActions, offset);
                offset += sizeof(Int32);
                action.Delay = (Int32)Marshal.ReadInt32(failureActions.lpsaActions, offset);
                offset += sizeof(Int32);
                actions[i] = action;

                switch (action.Type)
                {
                    case (int)ServiceManagerApi.SC_ACTION_TYPE.SC_ACTION_NONE:
                        txtDays.Text = action.Delay.ToString();
                        if (i == 0)
                            cbFirstfailure.SelectedItem = 0;
                        else if (i == 1)
                            cbSecondFailure.SelectedItem = 0;
                        else
                            cbSubsquentFailure.SelectedItem = 0;
                        break;

                    case (int)ServiceManagerApi.SC_ACTION_TYPE.SC_ACTION_REBOOT:
                        txtMinutes.Text = action.Delay.ToString();
                        if (i == 0)
                            cbFirstfailure.SelectedItem = 3;
                        else if (i == 1)
                            cbSecondFailure.SelectedItem = 3;
                        else
                            cbSubsquentFailure.SelectedItem = 3;
                        break;

                    case (int)ServiceManagerApi.SC_ACTION_TYPE.SC_ACTION_RESTART:
                        btnBrowse.Enabled = true;
                        if (i == 0)
                            cbFirstfailure.SelectedItem = 1;
                        else if (i == 1)
                            cbSecondFailure.SelectedItem = 1;
                        else
                            cbSubsquentFailure.SelectedItem = 1;
                        break;

                    case (int)ServiceManagerApi.SC_ACTION_TYPE.SC_ACTION_RUN_COMMAND:
                        txtProgram.Text = failureActions.lpCommand;
                        if (i == 0)
                            cbFirstfailure.SelectedItem = 2;
                        else if (i == 1)
                            cbSecondFailure.SelectedItem = 2;
                        else
                            cbSubsquentFailure.SelectedItem = 2;
                        break;

                    default:
                        break;
                }
            }
            if (failureActions.cActions != 0)
                SetControlStates(sFailureMsgs[0]);

            cbFirstfailure.SelectedIndexChanged += new EventHandler(this.cbFirstfailure_SelectedIndexChanged);
            cbSecondFailure.SelectedIndexChanged += new EventHandler(this.cbFirstfailure_SelectedIndexChanged);
            cbSubsquentFailure.SelectedIndexChanged += new EventHandler(this.cbFirstfailure_SelectedIndexChanged);
        }
        public bool OnApply()
        {
            Logger.Log("In ServiceRecoveryPage: OnApply()");

            ServiceManagerApi.SERVICE_FAILURE_ACTIONS failureActionUpdate = new ServiceManagerApi.SERVICE_FAILURE_ACTIONS();
            failureActions.cActions = 2;
            IntPtr lpsaActions = IntPtr.Zero;

            ServiceManagerApi.SC_ACTION action = new ServiceManagerApi.SC_ACTION();
            if (txtDays.Enabled)
            {
                action.Type = (int)ServiceManagerApi.SC_ACTION_TYPE.SC_ACTION_NONE;
                action.Delay = (int)TimeSpan.FromMinutes(int.Parse(txtDays.Text.Trim())).TotalMilliseconds;

                lpsaActions = Marshal.AllocHGlobal(Marshal.SizeOf(action) * 2);
                if (lpsaActions == IntPtr.Zero)
                {
                    Logger.Log(String.Format("Unable to allocate memory for service action, error was: 0x{0:X}", Marshal.GetLastWin32Error()));
                }
                Marshal.StructureToPtr(action, lpsaActions, false);
                if (txtMinutes.Enabled)
                {
                    action = new ServiceManagerApi.SC_ACTION();
                    action.Type = (int)ServiceManagerApi.SC_ACTION_TYPE.SC_ACTION_RESTART;
                    action.Delay = (int)TimeSpan.FromMinutes(int.Parse(txtMinutes.Text.Trim())).TotalMilliseconds;

                    IntPtr nextAction = (IntPtr)(lpsaActions.ToInt64() + Marshal.SizeOf(action));
                    Marshal.StructureToPtr(action, nextAction, false);
                }
                failureActions.lpsaActions = lpsaActions;
            }

            if (txtProgram.Enabled)
                failureActionUpdate.lpCommand = txtProgram.Text.Trim() + " " + txtCommandlines.Text.Trim();

            if (btnBrowse.Enabled)
            {
                failureActionUpdate.lpRebootMsg = failureActions.lpRebootMsg;
                failureActionUpdate.dwResetPeriod = failureActions.dwResetPeriod;
            }

            if (ServiceManagerWindowsWrapper.ApiChangeServiceConfig2(
                                            serviceName, failureActionUpdate))
            {
                Logger.Log("ServiceRecoveryPage:OnApply():ApiChangeServiceConfig2()");
                //return false;
            }

            if (lpsaActions != IntPtr.Zero)
                Marshal.FreeHGlobal(lpsaActions);

            return true;
        }
        public static ServiceManagerApi.SERVICE_FAILURE_ACTIONS ApiQueryServiceConfig2(
                                                                string sServicename)
        {
            UInt32 dwBytesNeeded;
            //UInt32 INFINITE = 0xFFFFFFFF;
            ServiceManagerApi.SERVICE_FAILURE_ACTIONS failureActions = new ServiceManagerApi.SERVICE_FAILURE_ACTIONS();
            try
            {
                ApiOpenService(sServicename, ServiceManagerInteropWindows.SERVICE_QUERY_CONFIG);

                Logger.Log(string.Format("ServiceManagerWindowsWrapper.ApiQueryServiceConfig2(sHostname={0})", sServicename), Logger.ServiceManagerLoglevel);

                bool bSuccess = ServiceManagerInteropWindows.QueryServiceConfig2(phService,
                                                    ServiceManagerInteropWindows.SERVICE_CONFIG_FAILURE_ACTIONS,
                                                    IntPtr.Zero, 0, out dwBytesNeeded);
                if (!bSuccess)
                {
                    Logger.Log("ServiceManagerWindowsWrapper.ApiOpenService returns false", Logger.ServiceManagerLoglevel);
                }

                IntPtr ptr = Marshal.AllocHGlobal((int)dwBytesNeeded);
                bSuccess = ServiceManagerInteropWindows.QueryServiceConfig2(phService,
                                                  ServiceManagerInteropWindows.SERVICE_CONFIG_FAILURE_ACTIONS,
                                                  ptr, dwBytesNeeded, out dwBytesNeeded);

                Marshal.PtrToStructure(ptr, failureActions);

                ApiCloseServiceHandle(phService);

                Logger.Log("ServiceManagerWindowsWrapper.ApiOpnSCManager is Success", Logger.ServiceManagerLoglevel);
            }
            catch (Exception ex)
            {
                Logger.LogException("ServiceManagerWindowsWrapper.ApiOpnSCManager()", ex);
            }

            return failureActions;
        }