using System; using System.Runtime.ConstrainedExecution; using System.Runtime.InteropServices; using System.Text; using Vanara.InteropServices; namespace Vanara.PInvoke { public static partial class Kernel32 { /// Flags that indicate how the values included in are to be used. [Flags] [PInvokeData("Winbase.h")] public enum ActCtxFlags { /// No values are set. ACTCTX_FLAG_NONE = 0x00000000, /// The value is set. ACTCTX_FLAG_PROCESSOR_ARCHITECTURE_VALID = 0x00000001, /// The value is set. ACTCTX_FLAG_LANGID_VALID = 0x00000002, /// The value is set. ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID = 0x00000004, /// The value is set. ACTCTX_FLAG_RESOURCE_NAME_VALID = 0x00000008, /// Set the activation context to be the process default when calling . ACTCTX_FLAG_SET_PROCESS_DEFAULT = 0x00000010, /// The value is set. ACTCTX_FLAG_APPLICATION_NAME_VALID = 0x00000020, /// Use when calling to identify that contains an assembly identifier. ACTCTX_FLAG_SOURCE_IS_ASSEMBLYREF = 0x00000040, /// The value is set. ACTCTX_FLAG_HMODULE_VALID = 0x00000080 } /// [Flags] [PInvokeData("Winbase.h", MSDNShortId = "aa373344")] public enum ApplicationRestartFlags { /// Do not restart the process if it terminates due to an unhandled exception. RESTART_NO_CRASH = 1, /// Do not restart the process if it terminates due to the application not responding. RESTART_NO_HANG = 2, /// Do not restart the process if it terminates due to the installation of an update. RESTART_NO_PATCH = 4, /// Do not restart the process if the computer is restarted as the result of an update. RESTART_NO_REBOOT = 8, } [PInvokeData("Winbase.h")] public enum DeactivateActCtxFlag { /// /// If this value is set and the cookie specified in the ulCookie parameter is in the top frame of the activation stack, the activation context is /// popped from the stack and thereby deactivated. /// /// If this value is set and the cookie specified in the ulCookie parameter is not in the top frame of the activation stack, this function searches /// down the stack for the cookie. /// /// If the cookie is found, a STATUS_SXS_EARLY_DEACTIVATION exception is thrown. /// If the cookie is not found, a STATUS_SXS_INVALID_DEACTIVATION exception is thrown. /// This value should be specified in most cases. /// None = 0, /// /// If this value is set and the cookie specified in the ulCookie parameter is in the top frame of the activation stack, the function returns an /// ERROR_INVALID_PARAMETER error code. Call GetLastError to obtain this code. /// If this value is set and the cookie is not on the activation stack, a STATUS_SXS_INVALID_DEACTIVATION exception will be thrown. /// /// If this value is set and the cookie is in a lower frame of the activation stack, all of the frames down to and including the frame the cookie is /// in is popped from the stack. /// /// DEACTIVATE_ACTCTX_FLAG_FORCE_EARLY_DEACTIVATION = 1 } /// /// Application-defined callback function used to save data and application state information in the event the application encounters an unhandled /// exception or becomes unresponsive. /// /// Context information specified when you called the RegisterApplicationRecoveryCallback function to register for recovery. /// The return value is not used and should be 0. [UnmanagedFunctionPointer(CallingConvention.Winapi)] [PInvokeData("Winbase.h", MSDNShortId = "aa373202")] public delegate uint ApplicationRecoveryCallback(IntPtr pvParameter); /// /// The ActivateActCtx function activates the specified activation context. It does this by pushing the specified activation context to the top of the /// activation stack. The specified activation context is thus associated with the current thread and any appropriate side-by-side API functions. /// /// Handle to an ACTCTX structure that contains information on the activation context that is to be made active. /// Pointer to a ULONG_PTR that functions as a cookie, uniquely identifying a specific, activated activation context. /// If the function succeeds, it returns TRUE. Otherwise, it returns FALSE. This function sets errors that can be retrieved by calling GetLastError. [DllImport(Lib.Kernel32, ExactSpelling = true, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] [PInvokeData("Winbase.h", MSDNShortId = "aa374151")] public static extern bool ActivateActCtx(ActCtxSafeHandle hActCtx, out IntPtr lpCookie); /// Indicates that the calling application has completed its data recovery. /// Specify TRUE to indicate that the data was successfully recovered; otherwise, FALSE. [DllImport(Lib.Kernel32, ExactSpelling = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa373328")] public static extern void ApplicationRecoveryFinished([MarshalAs(UnmanagedType.Bool)] bool bSuccess); /// Indicates that the calling application is continuing to recover data. /// Indicates whether the user has canceled the recovery process. Set by WER if the user clicks the Cancel button. /// This function returns S_OK on success or one of the following error codes. [DllImport(Lib.Kernel32, ExactSpelling = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa373329")] public static extern HRESULT ApplicationRecoveryInProgress([Out, MarshalAs(UnmanagedType.Bool)] out bool pbCanceled); /// The CreateActCtx function creates an activation context. /// Pointer to an ACTCTX structure that contains information about the activation context to be created. /// If the function succeeds, it returns a handle to the returned activation context. Otherwise, it returns INVALID_HANDLE_VALUE. [DllImport(Lib.Kernel32, CharSet = CharSet.Auto, SetLastError = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa375125")] public static extern ActCtxSafeHandle CreateActCtx(ref ACTCTX actctx); /// The DeactivateActCtx function deactivates the activation context corresponding to the specified cookie. /// Flags that indicate how the deactivation is to occur. /// /// The ULONG_PTR that was passed into the call to ActivateActCtx. This value is used as a cookie to identify a specific activated activation context. /// /// If the function succeeds, it returns TRUE. Otherwise, it returns FALSE. This function sets errors that can be retrieved by calling GetLastError. [DllImport(Lib.Kernel32, ExactSpelling = true, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] [PInvokeData("Winbase.h", MSDNShortId = "aa375140")] public static extern bool DeactivateActCtx(DeactivateActCtxFlag dwFlags, IntPtr lpCookie); /// /// Retrieves a pointer to the callback routine registered for the specified process. The address returned is in the virtual address space of the process. /// /// A handle to the process. This handle must have the PROCESS_VM_READ access right. /// A pointer to the recovery callback function. For more information, see ApplicationRecoveryCallback. /// A pointer to the callback parameter. /// The recovery ping interval, in 100-nanosecond intervals. /// Reserved for future use. /// This function returns S_OK on success. [DllImport(Lib.Kernel32, ExactSpelling = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa373343")] public static extern HRESULT GetApplicationRecoveryCallback(IntPtr hProcess, out ApplicationRecoveryCallback pRecoveryCallback, out IntPtr ppvParameter, out uint pdwPingInterval, out int pdwFlags); /// Retrieves the restart information registered for the specified process. /// A handle to the process. This handle must have the PROCESS_VM_READ access right. /// /// A pointer to a buffer that receives the restart command line specified by the application when it called the RegisterApplicationRestart function. The /// maximum size of the command line, in characters, is RESTART_MAX_CMD_LINE. Can be NULL if pcchSize is zero. /// /// /// On input, specifies the size of the pwzCommandLine buffer, in characters. /// /// If the buffer is not large enough to receive the command line, the function fails with HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER) and sets this /// parameter to the required buffer size, in characters. /// /// On output, specifies the size of the buffer that was used. /// /// To determine the required buffer size, set pwzCommandLine to NULL and this parameter to zero. The size includes one for the null-terminator /// character. Note that the function returns S_OK, not HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER) in this case. /// /// /// /// A pointer to a variable that receives the flags specified by the application when it called the RegisterApplicationRestart function. /// /// This function returns S_OK on success [DllImport(Lib.Kernel32, ExactSpelling = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa373344")] public static extern HRESULT GetApplicationRestartSettings(IntPtr hProcess, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pwzCommandline, ref uint pcchSize, out ApplicationRestartFlags pdwFlags); /// The GetCurrentActCtx function returns the handle to the active activation context of the calling thread. /// Pointer to the returned ACTCTX structure that contains information on the active activation context. /// If the function succeeds, it returns TRUE. Otherwise, it returns FALSE. This function sets errors that can be retrieved by calling GetLastError. [DllImport(Lib.Kernel32, ExactSpelling = true, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] [PInvokeData("Winbase.h", MSDNShortId = "aa375152")] public static extern bool GetCurrentActCtx(out ActCtxSafeHandle handle); /// Registers the active instance of an application for recovery. /// A pointer to the recovery callback function. For more information, see ApplicationRecoveryCallback. /// A pointer to a variable to be passed to the callback function. Can be NULL. /// /// The recovery ping interval, in milliseconds. By default, the interval is 5 seconds (RECOVERY_DEFAULT_PING_INTERVAL). The maximum interval is 5 /// minutes. If you specify zero, the default interval is used. /// /// You must call the ApplicationRecoveryInProgress function within the specified interval to indicate to ARR that you are still actively recovering; /// otherwise, WER terminates recovery. Typically, you perform recovery in a loop with each iteration lasting no longer than the ping interval. Each /// iteration performs a block of recovery work followed by a call to ApplicationRecoveryInProgress. Since you also use ApplicationRecoveryInProgress to /// determine if the user wants to cancel recovery, you should consider a smaller interval, so you do not perform a lot of work unnecessarily. /// /// /// Reserved for future use. Set to zero. /// This function returns S_OK on success [DllImport(Lib.Kernel32, ExactSpelling = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa373345")] public static extern HRESULT RegisterApplicationRecoveryCallback(ApplicationRecoveryCallback pRecoveryCallback, IntPtr pvParameter, uint dwPingInterval, uint dwFlags); /// Registers the active instance of an application for restart. /// /// A string that specifies the command-line arguments for the application when it is restarted. The maximum size of the command line that you can /// specify is RESTART_MAX_CMD_LINE characters. Do not include the name of the executable in the command line; this function adds it for you. /// /// If this parameter is NULL or an empty string, the previously registered command line is removed. If the argument contains spaces, use quotes around /// the argument. /// /// /// Options /// This function returns S_OK on success [DllImport(Lib.Kernel32, ExactSpelling = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa373347")] public static extern HRESULT RegisterApplicationRestart([MarshalAs(UnmanagedType.BStr)] string pwzCommandline, ApplicationRestartFlags dwFlags); /// The ReleaseActCtx function decrements the reference count of the specified activation context. /// /// Handle to the ACTCTX structure that contains information on the activation context for which the reference count is to be decremented. /// [DllImport(Lib.Kernel32, ExactSpelling = true, SetLastError = true)] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] [PInvokeData("Winbase.h", MSDNShortId = "aa375713")] public static extern void ReleaseActCtx(IntPtr hActCtx); /// Removes the active instance of an application from the recovery list. /// You do not need to call this function before exiting. You need to remove the registration only if you choose to not recover data. /// This function returns S_OK on success [DllImport(Lib.Kernel32, ExactSpelling = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa373348")] public static extern HRESULT UnregisterApplicationRecoveryCallback(); /// Removes the active instance of an application from the restart list. /// /// You do not need to call this function before exiting. You need to remove the registration only if you choose to not restart the application. For /// example, you could remove the registration if your application entered a corrupted state where a future restart would also fail. You must call this /// function before the application fails abnormally. /// /// This function returns S_OK on success [DllImport(Lib.Kernel32, ExactSpelling = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa373349")] public static extern HRESULT UnregisterApplicationRestart(); /// The ACTCTX structure is used by the CreateActCtx function to create the activation context. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] [PInvokeData("Winbase.h", MSDNShortId = "aa374149")] public struct ACTCTX { /// The size, in bytes, of this structure. This is used to determine the version of this structure. public int cbSize; /// /// Flags that indicate how the values included in this structure are to be used. Set any undefined bits in dwFlags to 0. If any undefined bits are /// not set to 0, the call to CreateActCtx that creates the activation context fails and returns an invalid parameter error code. /// public ActCtxFlags dwFlags; /// /// Null-terminated string specifying the path of the manifest file or PE image to be used to create the activation context. If this path refers to /// an EXE or DLL file, the lpResourceName member is required. /// [MarshalAs(UnmanagedType.LPTStr)] public string lpSource; /// Identifies the type of processor used. Specifies the system's processor architecture. public ProcessorArchitecture wProcessorArchitecture; /// /// Specifies the language manifest that should be used. The default is the current user's current UI language. /// If the requested language cannot be found, an approximation is searched for using the following order: /// /// /// The current user's specific language. For example, for US English (1033). /// /// /// The current user's primary language. For example, for English (9). /// /// /// The current system's specific language. /// /// /// The current system's primary language. /// /// /// A nonspecific worldwide language. Language neutral (0). /// /// /// public ushort wLangId; /// /// The base directory in which to perform private assembly probing if assemblies in the activation context are not present in the system-wide store. /// [MarshalAs(UnmanagedType.LPTStr)] public string lpAssemblyDirectory; /// /// Pointer to a null-terminated string that contains the resource name to be loaded from the PE specified in hModule or lpSource. If the resource /// name is an integer, set this member using MAKEINTRESOURCE. This member is required if lpSource refers to an EXE or DLL. /// [MarshalAs(UnmanagedType.LPTStr)] public string lpResourceName; /// /// The name of the current application. If the value of this member is set to null, the name of the executable that launched the current process is used. /// [MarshalAs(UnmanagedType.LPTStr)] public string lpApplicationName; /// /// Use this member rather than lpSource if you have already loaded a DLL and wish to use it to create activation contexts rather than using a path /// in lpSource. See lpResourceName for the rules of looking up resources in this module. /// public IntPtr hModule; /// Initializes a new instance of the struct. /// The source. public ACTCTX(string source) : this() { cbSize = Marshal.SizeOf(typeof(ACTCTX)); lpSource = source; } /// The empty public static ACTCTX Empty = new ACTCTX { cbSize = Marshal.SizeOf(typeof(ACTCTX)) }; } /// A safe handle for an Activation Context. /// [PInvokeData("Winbase.h")] public class ActCtxSafeHandle : GenericSafeHandle { /// Initializes a new instance of the class. public ActCtxSafeHandle() : base(ReleaseHandle) { } /// Initializes a new instance of the class. /// The h act CTX. /// if set to true [owns handle]. public ActCtxSafeHandle(IntPtr hActCtx, bool ownsHandle) : base(hActCtx, ReleaseHandle, ownsHandle) { } private static bool ReleaseHandle(IntPtr handle) { ReleaseActCtx(handle); return true; } } } }