public SERVICE_FAILURE_ACTIONS(ServiceFailureActions sfa) { if (sfa.ResetPeriod.TotalSeconds > 0) { dwResetPeriod = Convert.ToUInt32(sfa.ResetPeriod.TotalSeconds); } else { dwResetPeriod = 0; } if (sfa.RebootMessage.Length > 0) { rebootMsg = Marshal.StringToHGlobalUni(sfa.RebootMessage); } else { rebootMsg = IntPtr.Zero; } if (sfa.Command.Length > 0) { command = Marshal.StringToHGlobalUni(sfa.Command); } else { command = IntPtr.Zero; } if (sfa.HasActionsDefined) { numActions = (uint)sfa.Actions.Length; //Allocate space for the unmanaged array SC_ACTION nativeAction; int sizeSCACTION = Marshal.SizeOf(typeof(SC_ACTION)); actions = Marshal.AllocHGlobal(sizeSCACTION * sfa.Actions.Length); //Nowe we need to copy the elements to the new array (but we have to track where we place it) IntPtr pElement = actions; for (int nIdx = 0; nIdx < numActions; ++nIdx) { //Copy the action data nativeAction = new SC_ACTION(sfa.Actions[nIdx]); //The destination is actual calculated from the base of our pointer pElement = (IntPtr)(actions.ToInt64() + (sizeSCACTION * nIdx)); Marshal.StructureToPtr(nativeAction, pElement, false); } ; } else { numActions = 0; actions = IntPtr.Zero; }; }
private void QueryFailureActions(SafeHandle handle) { uint sizeNeeded; uint sizeBuffer = 4096; IntPtr pBuffer = IntPtr.Zero; try { while (true) { //Allocate buffer pBuffer = Marshal.AllocHGlobal((int)sizeBuffer); //Get the description first as it is the easiest if (NativeMethods.QueryServiceConfig2(handle, NativeMethods.SERVICE_CONFIG_INFOLEVEL.FAILURE_ACTIONS, pBuffer, sizeBuffer, out sizeNeeded)) { break; } //If we failed for any reason other than insufficient buffer space int error = Marshal.GetLastWin32Error(); if (error != NativeMethods.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(error); } //Prepare to allocate a new buffer Marshal.FreeHGlobal(pBuffer); sizeBuffer = sizeNeeded; } ; //Build the failure list var failure = (NativeMethods.SERVICE_FAILURE_ACTIONS)Marshal.PtrToStructure(pBuffer, typeof(NativeMethods.SERVICE_FAILURE_ACTIONS)); FailureActions = new ServiceFailureActions(failure); } finally { //Clean up if (pBuffer != IntPtr.Zero) { Marshal.FreeHGlobal(pBuffer); } }; }
/// <summary>Initializes an instance of the <see cref="ServiceInstaller2"/> class.</summary> public ServiceInstaller2() { FailureActions = new ServiceFailureActions(); }