2using System.Collections.Generic;
3using System.Runtime.InteropServices;
8 [StructLayout(LayoutKind.Sequential)]
9 internal struct ServiceFailureActionsInfo
11 [MarshalAs(UnmanagedType.U4)]
private uint dwResetPeriod;
12 [MarshalAs(UnmanagedType.LPStr)]
private string lpRebootMsg;
13 [MarshalAs(UnmanagedType.LPStr)]
private string lpCommand;
14 [MarshalAs(UnmanagedType.U4)]
private int cActions;
15 private IntPtr lpsaActions;
17 public TimeSpan ResetPeriod => TimeSpan.FromSeconds(dwResetPeriod);
19 public string RebootMsg => lpRebootMsg;
21 public string Command => lpCommand;
23 public int CountActions => cActions;
25 public ScAction[] Actions
29 int size = Marshal.SizeOf<ScAction>();
30 ScAction[] mangagedArray =
new ScAction[cActions];
32 for (
int i = 0; i < cActions; i++)
34 IntPtr ins =
new IntPtr(lpsaActions.ToInt64() + i * size);
35 mangagedArray[i] = Marshal.PtrToStructure<ScAction>(ins);
45 internal static ServiceFailureActionsInfo Default =
46 new ServiceFailureActionsInfo { dwResetPeriod = 0, lpRebootMsg =
null, lpCommand =
null, cActions = 0, lpsaActions = IntPtr.Zero };
51 internal ServiceFailureActionsInfo(TimeSpan resetPeriod,
string rebootMessage,
string restartCommand, IReadOnlyCollection<ScAction> actions)
53 dwResetPeriod = resetPeriod == TimeSpan.MaxValue ? uint.MaxValue : (uint)Math.Round(resetPeriod.TotalSeconds);
54 lpRebootMsg = rebootMessage;
55 lpCommand = restartCommand;
56 cActions = actions?.Count ?? 0;
58 if (!(actions is
null))
60 lpsaActions = Marshal.AllocHGlobal(Marshal.SizeOf<ScAction>() * cActions);
62 if (lpsaActions == IntPtr.Zero)
63 throw new Exception(
string.Format(
"Unable to allocate memory for service action, error was: 0x{0:X}", Marshal.GetLastWin32Error()));
65 IntPtr nextAction = lpsaActions;
67 foreach (ScAction action
in actions)
69 Marshal.StructureToPtr(action, nextAction, fDeleteOld:
false);
70 nextAction = (IntPtr)(nextAction.ToInt64() + Marshal.SizeOf<ScAction>());
74 lpsaActions = IntPtr.Zero;