Files
mirror-processhacker/trunk/ProcessHacker/Components/RestartRecoveryLib/RestartRecoveryInterop.cs
T
dmex04 be8ab499fa fixed Hresult comment, Added support for Windows RegisterForRestart and RegisterForRecovery
git-svn-id: svn://svn.code.sf.net/p/processhacker/code@2099 21ef857c-d57f-4fe0-8362-d861dc6d29cd
2009-10-08 02:39:40 +00:00

127 lines
3.9 KiB
C#

using System.Runtime.InteropServices;
using ProcessHacker.Native.Api;
using System.Security;
using System;
namespace ProcessHackerRestartRecovery
{
[SuppressUnmanagedCodeSecurity]
internal static class AppRestartRecoveryNativeMethods
{
#region Application Restart and Recovery Definitions
internal delegate UInt32 InternalRecoveryCallback(IntPtr state);
internal static InternalRecoveryCallback internalCallback;
static AppRestartRecoveryNativeMethods()
{
internalCallback = new InternalRecoveryCallback(InternalRecoveryHandler);
}
private static UInt32 InternalRecoveryHandler(IntPtr parameter)
{
bool cancelled = false;
ApplicationRecoveryInProgress(out cancelled);
GCHandle handle = GCHandle.FromIntPtr(parameter);
RecoveryData data = handle.Target as RecoveryData;
data.Invoke();
handle.Free();
return 0;
}
[DllImport("kernel32.dll")]
internal static extern void ApplicationRecoveryFinished(
[MarshalAs(UnmanagedType.Bool)]
bool success
);
[DllImport("kernel32.dll")]
[PreserveSig]
internal static extern HResult ApplicationRecoveryInProgress(
[Out, MarshalAs(UnmanagedType.Bool)]
out bool canceled
);
[DllImport("kernel32.dll")]
[PreserveSig]
internal static extern HResult GetApplicationRecoveryCallback(
IntPtr processHandle,
out RecoveryCallback recoveryCallback,
out object state,
out uint pingInterval,
out uint flags
);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
[PreserveSig]
internal static extern HResult RegisterApplicationRecoveryCallback(
InternalRecoveryCallback callback, IntPtr param,
uint pingInterval,
uint flags //Unused
);
[DllImport("kernel32.dll")]
[PreserveSig]
internal static extern HResult RegisterApplicationRestart(
[MarshalAs(UnmanagedType.BStr)]
string commandLineArgs,
RestartRestrictions flags
);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[PreserveSig]
internal static extern HResult GetApplicationRestartSettings(
IntPtr process,
IntPtr commandLine,
ref uint size,
out RestartRestrictions flags
);
[DllImport("kernel32.dll")]
[PreserveSig]
internal static extern HResult UnregisterApplicationRecoveryCallback();
[DllImport("kernel32.dll")]
[PreserveSig]
internal static extern HResult UnregisterApplicationRestart();
#endregion
}
/// <summary>
/// Specifies the conditions when Windows Error Reporting
/// should not restart an application that has registered
/// for automatic restart.
/// </summary>
[Flags]
public enum RestartRestrictions
{
/// <summary>
/// Always restart the application.
/// </summary>
None = 0,
/// <summary>
/// Do not restart when the application has crashed.
/// </summary>
NotOnCrash = 1,
/// <summary>
/// Do not restart when the application is hung.
/// </summary>
NotOnHang = 2,
/// <summary>
/// Do not restart when the application is terminated
/// due to a system update.
/// </summary>
NotOnPatch = 4,
/// <summary>
/// Do not restart when the application is terminated
/// because of a system reboot.
/// </summary>
NotOnReboot = 8
}
}