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 bool Do_LogonSCManager() { int iRet = ServiceManagerWindowsWrapper.ApiOpnSCManager(); if (iRet == 0) { return(true); } return(false); }
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); }
private void GetWindowsServiceInfo() { foreach (ManagementObject mo in ServiceManagerWindowsWrapper.GetServiceCollection(serviceName)) { bool canPause = (bool)mo["AcceptPause"]; string status = mo["State"].ToString(); txtDescription.Text = mo["Description"].ToString(); txtPathToExecute.Text = mo["PathName"].ToString(); lblServiceStatusValue.Text = mo["State"].ToString(); lblServiceNameValue.Text = mo["Name"].ToString(); txtDisplayName.Text = mo["DisplayName"].ToString(); if (string.Equals(status, "Started")) { //Service is started btnStop.Enabled = true; if (canPause) { btnPause.Enabled = true; } btnStart.Enabled = btnResume.Enabled = !btnStop.Enabled; } else if (string.Equals(status, "Stopped")) { btnStart.Enabled = true; btnPause.Enabled = btnResume.Enabled = btnStop.Enabled = !btnStart.Enabled; } else if (string.Equals(status, "Running")) { btnStop.Enabled = true; if (canPause) { btnPause.Enabled = true; } btnStart.Enabled = btnResume.Enabled = !btnStop.Enabled; } else if (string.Equals(status, "Paused")) { btnStop.Enabled = btnResume.Enabled = true; btnStart.Enabled = btnPause.Enabled = !btnStop.Enabled; } int compareResult = string.Compare(mo["StartMode"].ToString(), "Disabled"); if (compareResult < 0) { cmbStartupType.SelectedIndex = 0; } else if (compareResult > 0) { cmbStartupType.SelectedIndex = 1; } else { cmbStartupType.SelectedIndex = 2; //Disable all buttons when service is disabled btnStart.Enabled = btnStop.Enabled = btnPause.Enabled = btnResume.Enabled = false; } } }