using System; using System.Runtime.InteropServices; using System.Text; using Vanara.InteropServices; namespace Vanara.PInvoke { public static partial class Kernel32 { /// /// 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); /// Flags that determine in which section FindActCtxSectionString searches. public enum ACTCTX_SECTION { ACTIVATION_CONTEXT_SECTION_ASSEMBLY_INFORMATION = 1, ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION = 2, ACTIVATION_CONTEXT_SECTION_WINDOW_CLASS_REDIRECTION = 3, ACTIVATION_CONTEXT_SECTION_COM_SERVER_REDIRECTION = 4, ACTIVATION_CONTEXT_SECTION_COM_INTERFACE_REDIRECTION = 5, ACTIVATION_CONTEXT_SECTION_COM_TYPE_LIBRARY_REDIRECTION = 6, ACTIVATION_CONTEXT_SECTION_COM_PROGID_REDIRECTION = 7, ACTIVATION_CONTEXT_SECTION_GLOBAL_OBJECT_RENAME_TABLE = 8, ACTIVATION_CONTEXT_SECTION_CLR_SURROGATES = 9, ACTIVATION_CONTEXT_SECTION_APPLICATION_SETTINGS = 10, ACTIVATION_CONTEXT_SECTION_COMPATIBILITY_INFO = 11 } /// 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 } /// public enum ACTIVATION_CONTEXT_INFO_CLASS { /// Not available. ActivationContextBasicInformation = 1, /// /// If QueryActCtxW is called with this option and the function succeeds, the returned buffer contains detailed information about the activation /// context. This information is in the form of the ACTIVATION_CONTEXT_DETAILED_INFORMATION structure. /// ActivationContextDetailedInformation = 2, /// /// If QueryActCtxW is called with this option and the function succeeds, the buffer contains information about the assembly that has the index /// specified in pvSubInstance. This information is in the form of the ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION structure. /// AssemblyDetailedInformationInActivationContext = 3, /// /// Information about a file in one of the assemblies in Activation Context. The pvSubInstance parameter must point to an /// ACTIVATION_CONTEXT_QUERY_INDEX structure. If QueryActCtxW is called with this option and the function succeeds, the returned buffer contains /// information for a file in the assembly. This information is in the form of the ASSEMBLY_FILE_DETAILED_INFORMATION structure. /// FileInformationInAssemblyOfAssemblyInActivationContext = 4, /// /// If QueryActCtxW is called with this option and the function succeeds, the buffer contains information about requested run level of the activation /// context. This information is in the form of the ACTIVATION_CONTEXT_RUN_LEVEL_INFORMATION structure.Windows Server 2003 and Windows XP: This value /// is not available. /// RunlevelInformationInActivationContext = 5, /// /// If QueryActCtxW is called with this option and the function succeeds, the buffer contains information about requested compatibility context. This /// information is in the form of the ACTIVATION_CONTEXT_COMPATIBILITY_INFORMATION structure.Windows Server 2008 and earlier, and Windows Vista and /// earlier: This value is not available. This option is available beginning with Windows Server 2008 R2 and Windows 7. /// CompatibilityInformationInActivationContext = 6, /// The activation context manifest resource name ActivationContextManifestResourceName = 7, /// The maximum activation context information class MaxActivationContextInfoClass, /// /// If QueryActCtxW is called with this option and the function succeeds, the buffer contains information about the assembly that has the index /// specified in pvSubInstance. This information is in the form of the ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION structure. /// AssemblyDetailedInformationInActivationContxt = 3, /// /// Information about a file in one of the assemblies in Activation Context. The pvSubInstance parameter must point to an /// ACTIVATION_CONTEXT_QUERY_INDEX structure. If QueryActCtxW is called with this option and the function succeeds, the returned buffer contains /// information for a file in the assembly. This information is in the form of the ASSEMBLY_FILE_DETAILED_INFORMATION structure. /// FileInformationInAssemblyOfAssemblyInActivationContxt = 4 } /// [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 } /// Flags that determine how FindActCtxSectionString operates. public enum FIND_ACTCTX_SECTION { /// /// This function returns the activation context handle where the redirection data was found in the hActCtx member of the ACTCTX_SECTION_KEYED_DATA /// structure. The caller must use ReleaseActCtx to release this activation context. /// FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX = 0x00000001, /// Undocumented. FIND_ACTCTX_SECTION_KEY_RETURN_FLAGS = 0x00000002, /// Undocumented. FIND_ACTCTX_SECTION_KEY_RETURN_ASSEMBLY_METADATA = 0x00000004 } /// [PInvokeData("Winbase.h")] public enum QueryActCtxFlag : uint { /// /// QueryActCtxW queries the activation context active on the thread instead of the context specified by hActCtx. This is usually the last activation /// context passed to ActivateActCtx. If ActivateActCtx has not been called, the active activation context can be the activation context used by the /// executable of the current process. In other cases, the operating system determines the active activation context. For example, when the callback /// function to a new thread is called, the active activation context may be the context that was active when you created the thread by calling CreateThread. /// QUERY_ACTCTX_FLAG_USE_ACTIVE_ACTCTX = 0x00000004, /// /// QueryActCtxW interprets hActCtx as an HMODULE data type and queries an activation context that is associated with a DLL or EXE. When a DLL or EXE /// is loaded, the loader checks for a manifest stored in a resource. If the loader finds an RT_MANIFEST resource with a resource identifier set to /// ISOLATIONAWARE_MANIFEST_ RESOURCE_ID, the loader associates the resulting activation context with the DLL or EXE. This is the activation context /// that QueryActCtxW queries when the QUERY_ACTCTX_FLAG_ACTCTX_IS_HMODULE flag has been set. /// QUERY_ACTCTX_FLAG_ACTCTX_IS_HMODULE = 0x00000008, /// /// QueryActCtxW interprets hActCtx as an address within a DLL or EXE and queries an activation context that has been associated with the DLL or EXE. /// This can be any address within the DLL or EXE. For example, the address of any function within a DLL or EXE or the address of any static data, /// such as a constant string. When a DLL or EXE is loaded, the loader checks for a manifest stored in a resource in the same way as QUERY_ACTCTX_FLAG_ACTCTX_IS_HMODULE. /// QUERY_ACTCTX_FLAG_ACTCTX_IS_ADDRESS = 0x00000010, /// Undocumented. QUERY_ACTCTX_FLAG_NO_ADDREF = 0x80000000, } /// /// 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. For an example, see Retrieving the Last-Error Code. For a complete /// list of error codes, see System Error Codes. /// /// // BOOL ActivateActCtx( _In_ HANDLE hActCtx, _Out_ ULONG_PTR *lpCookie); https://msdn.microsoft.com/en-us/library/windows/desktop/aa374151(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa374151")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool ActivateActCtx(HACTCTX hActCtx, out IntPtr lpCookie); /// The AddRefActCtx function increments the reference count of the specified activation context. /// /// Handle to an ACTCTX structure that contains information on the activation context for which the reference count is to be incremented. /// /// This function does not return a value. // void AddRefActCtx( _In_ HANDLE hActCtx); https://msdn.microsoft.com/en-us/library/windows/desktop/aa374171(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa374171")] public static extern void AddRefActCtx(HACTCTX hActCtx); /// Indicates that the calling application has completed its data recovery. /// Specify TRUE to indicate that the data was successfully recovered; otherwise, FALSE. /// This function does not return a value. // VOID WINAPI ApplicationRecoveryFinished( _In_ BOOL bSuccess); https://msdn.microsoft.com/en-us/library/windows/desktop/aa373328(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, 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. /// /// /// /// Return code /// Description /// /// /// E_FAIL /// You can call this function only after Windows Error Reporting has called your recovery callback function. /// /// /// E_INVALIDARG /// The pbCancelled cannot be NULL. /// /// /// /// // HRESULT WINAPI ApplicationRecoveryInProgress( _Out_ PBOOL pbCanceled); https://msdn.microsoft.com/en-us/library/windows/desktop/aa373329(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, 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. /// /// This function sets errors that can be retrieved by calling GetLastError. For an example, see Retrieving the Last-Error Code. For a complete /// list of error codes, see System Error Codes. /// /// // HANDLE CreateActCtx( _Inout_ PACTCTX pActCtx); https://msdn.microsoft.com/en-us/library/windows/desktop/aa375125(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)] [PInvokeData("Winbase.h", MSDNShortId = "aa375125")] public static extern SafeHACTCTX CreateActCtx(in ACTCTX actctx); /// The DeactivateActCtx function deactivates the activation context corresponding to the specified cookie. /// /// Flags that indicate how the deactivation is to occur. /// /// /// /// Value /// Meaning /// /// /// 0 /// /// 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. /// /// /// /// DEACTIVATE_ACTCTX_FLAG_FORCE_EARLY_DEACTIVATION /// /// 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. /// /// /// /// /// /// /// 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. For an example, see Retrieving the Last-Error Code. For a complete /// list of error codes, see System Error Codes. /// /// // BOOL DeactivateActCtx( _In_ DWORD dwFlags, _In_ ULONG_PTR ulCookie); https://msdn.microsoft.com/en-us/library/windows/desktop/aa375140(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa375140")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool DeactivateActCtx(DeactivateActCtxFlag dwFlags, IntPtr lpCookie); /// /// The FindActCtxSectionGuid function retrieves information on a specific GUID in the current activation context and returns a /// ACTCTX_SECTION_KEYED_DATA structure. /// /// /// Flags that determine how this function is to operate. Only the following flag is currently defined. /// /// /// /// Value /// Meaning /// /// /// FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX /// /// This function returns the activation context handle where the redirection data was found in the hActCtx member of the ACTCTX_SECTION_KEYED_DATA /// structure. The caller must use ReleaseActCtx to release this activation context. /// /// /// /// /// /// Reserved; must be null. /// /// Identifier of the section of the activation context in which to search for the specified GUID. /// The following are valid GUID section identifiers: /// The following is a valid GUID section identifier beginning with Windows Server 2003 and Windows XP with SP1: /// /// Pointer to a GUID to be used as the search criteria. /// Pointer to an ACTCTX_SECTION_KEYED_DATA structure to be filled out with the requested GUID information. /// /// If the function succeeds, it returns TRUE. Otherwise, it returns FALSE. /// /// This function sets errors that can be retrieved by calling GetLastError. For an example, see Retrieving the Last-Error Code. For a complete /// list of error codes, see System Error Codes. /// /// // BOOL FindActCtxSectionGuid( _In_ DWORD dwFlags, _In_ const GUID *lpExtensionGuid, _In_ ULONG ulSectionId, _In_ const GUID *lpGuidToFind, _Out_ // PACTCTX_SECTION_KEYED_DATA ReturnedData); https://msdn.microsoft.com/en-us/library/windows/desktop/aa375148(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa375148")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool FindActCtxSectionGuid(FIND_ACTCTX_SECTION dwFlags, in Guid lpExtensionGuid, uint ulSectionId, in Guid lpGuidToFind, out ACTCTX_SECTION_KEYED_DATA ReturnedData); /// /// The FindActCtxSectionString function retrieves information on a specific string in the current activation context and returns a /// ACTCTX_SECTION_KEYED_DATA structure. /// /// /// Flags that determine how this function is to operate. Only the following flag is currently defined. /// /// /// /// Value /// Meaning /// /// /// FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX /// /// This function returns the activation context handle where the redirection data was found in the hActCtx member of the ACTCTX_SECTION_KEYED_DATA /// structure. The caller must use ReleaseActCtx to release this activation context. /// /// /// /// /// /// Reserved; must be null. /// /// Identifier of the string section of the activation context in which to search for the specific string. /// The following are valid string section identifiers: /// /// Pointer to a null-terminated string to be used as the search criteria. /// Pointer to an ACTCTX_SECTION_KEYED_DATA structure to be filled out with the requested string information. /// /// If the function succeeds, it returns TRUE. Otherwise, it returns FALSE. /// /// This function sets errors that can be retrieved by calling GetLastError. For an example, see Retrieving the Last-Error Code. For a complete /// list of error codes, see System Error Codes. /// /// // BOOL FindActCtxSectionString( _In_ DWORD dwFlags, _In_ const GUID *lpExtensionGuid, _In_ ULONG ulSectionId, _In_ LPCTSTR lpStringToFind, _Out_ // PACTCTX_SECTION_KEYED_DATA ReturnedData); https://msdn.microsoft.com/en-us/library/windows/desktop/aa375149(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)] [PInvokeData("Winbase.h", MSDNShortId = "aa375149")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool FindActCtxSectionString(FIND_ACTCTX_SECTION dwFlags, in Guid lpExtensionGuid, ACTCTX_SECTION ulSectionId, string lpStringToFind, out ACTCTX_SECTION_KEYED_DATA ReturnedData); /// /// 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 or one of the following error codes. /// /// /// /// Return code /// Description /// /// /// S_FALSE /// The application did not register for recovery. /// /// /// E_INVALIDARG /// One or more parameters are not valid. /// /// /// /// // HRESULT WINAPI GetApplicationRecoveryCallback( _In_ HANDLE hProcess, _Out_ APPLICATION_RECOVERY_CALLBACK *pRecoveryCallback, _Out_ PVOID // *ppvParameter, _Out_ PDWORD pdwPingInterval, _Out_ PDWORD pdwFlags); https://msdn.microsoft.com/en-us/library/windows/desktop/aa373343(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa373343")] public static extern HRESULT GetApplicationRecoveryCallback(HPROCESS 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 or one of the following error codes. /// /// /// /// Return code /// Description /// /// /// E_INVALIDARG /// One or more parameters are not valid. /// /// /// HRESULT_FROM_WIN32(ERROR_NOT_FOUND) /// The application did not register for restart. /// /// /// HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER) /// /// The pwzCommandLine buffer is too small. The function returns the required buffer size in pcchSize. Use the required size to reallocate the buffer. /// /// /// /// /// // HRESULT WINAPI GetApplicationRestartSettings( _In_ HANDLE hProcess, _Out_opt_ PWSTR pwzCommandline, _Inout_ PDWORD pcchSize, _Out_opt_ PDWORD // pdwFlags); https://msdn.microsoft.com/en-us/library/windows/desktop/aa373344(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("Winbase.h", MSDNShortId = "aa373344")] public static extern HRESULT GetApplicationRestartSettings(HPROCESS hProcess, 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. For an example, see Retrieving the Last-Error Code. For a complete /// list of error codes, see System Error Codes. /// /// // BOOL GetCurrentActCtx( _Out_ HANDLE *lphActCtx); https://msdn.microsoft.com/en-us/library/windows/desktop/aa375152(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa375152")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetCurrentActCtx(out HACTCTX handle); /// /// The QueryActCtxSettingsW function specifies the activation context, and the namespace and name of the attribute that is to be queried. /// /// This value must be 0. /// A handle to the activation context that is being queried. /// /// /// A pointer to a string that contains the value "http://schemas.microsoft.com/SMI/2005/WindowsSettings" or NULL. These values are equivalent. /// /// /// Windows 8 and Windows Server 2012: A pointer to a string that contains the value /// "http://schemas.microsoft.com/SMI/2011/WindowsSettings" is also a valid parameter. A NULL is still equivalent to the previous value. /// /// /// The name of the attribute to be queried. /// A pointer to the buffer that receives the query result. /// The size of the buffer in characters that receives the query result. /// /// A pointer to a value which is the number of characters written to the buffer specified by pvBuffer or that is required to hold the query result. /// /// /// If the function succeeds, it returns TRUE. Otherwise, it returns FALSE. /// /// This function sets errors that can be retrieved by calling GetLastError. For an example, see Retrieving the Last-Error Code. For a complete /// list of error codes, see System Error Codes. /// /// // BOOL QueryActCtxSettingsW( _In_opt_ DWORD dwFlags, _In_opt_ HANDLE hActCtx, _In_opt_ PCWSTR settingsNameSpace, _In_ PCWSTR settingName, _Out_ PWSTR // pvBuffer, _In_ SIZE_T dwBuffer, _Out_opt_ SIZE_T *pdwWrittenOrRequired); https://msdn.microsoft.com/en-us/library/windows/desktop/aa375700(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("Winbase.h", MSDNShortId = "aa375700")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool QueryActCtxSettingsW(QueryActCtxFlag dwFlags, [In] HACTCTX hActCtx, string settingsNameSpace, string settingName, StringBuilder pvBuffer, SizeT dwBuffer, out SizeT pdwWrittenOrRequired); /// The QueryActCtxW function queries the activation context. /// /// This parameter should be set to one of the following flag bits. /// /// /// /// Flag /// Meaning /// /// /// QUERY_ACTCTX_FLAG_USE_ACTIVE_ACTCTX /// /// QueryActCtxW queries the activation context active on the thread instead of the context specified by hActCtx. This is usually the last activation /// context passed to ActivateActCtx. If ActivateActCtx has not been called, the active activation context can be the activation context used by the /// executable of the current process. In other cases, the operating system determines the active activation context. For example, when the callback /// function to a new thread is called, the active activation context may be the context that was active when you created the thread by calling CreateThread. /// /// /// /// QUERY_ACTCTX_FLAG_ACTCTX_IS_HMODULE /// /// QueryActCtxW interprets hActCtx as an HMODULE data type and queries an activation context that is associated with a DLL or EXE. When a DLL or EXE is /// loaded, the loader checks for a manifest stored in a resource. If the loader finds an RT_MANIFEST resource with a resource identifier set to /// ISOLATIONAWARE_MANIFEST_ RESOURCE_ID, the loader associates the resulting activation context with the DLL or EXE. This is the activation context that /// QueryActCtxW queries when the QUERY_ACTCTX_FLAG_ACTCTX_IS_HMODULE flag has been set. /// /// /// /// QUERY_ACTCTX_FLAG_ACTCTX_IS_ADDRESS /// /// QueryActCtxW interprets hActCtx as an address within a DLL or EXE and queries an activation context that has been associated with the DLL or EXE. /// This can be any address within the DLL or EXE. For example, the address of any function within a DLL or EXE or the address of any static data, such /// as a constant string. When a DLL or EXE is loaded, the loader checks for a manifest stored in a resource in the same way as QUERY_ACTCTX_FLAG_ACTCTX_IS_HMODULE. /// /// /// /// /// /// Handle to the activation context that is being queried. /// /// /// Index of the assembly, or assembly and file combination, in the activation context. The meaning of the pvSubInstance depends on the option specified /// by the value of the ulInfoClass parameter. This parameter may be null. /// /// /// /// /// ulInfoClass Option /// Meaning /// /// /// AssemblyDetailedInformationInActivationContext /// /// Pointer to a DWORD that specifies the index of the assembly within the activation context. This is the activation context that QueryActCtxW queries. /// /// /// /// FileInformationInAssemblyOfAssemblyInActivationContext /// /// Pointer to an ACTIVATION_CONTEXT_QUERY_INDEX structure. If QueryActCtxW is called with this option and the function succeeds, the returned buffer /// contains information for a file in the assembly. This information is in the form of the ASSEMBLY_FILE_DETAILED_INFORMATION structure. /// /// /// /// /// /// /// This parameter can have only the values shown in the following table. /// /// /// /// Option /// Meaning /// /// /// ActivationContextBasicInformation1 /// Not available. /// /// /// ActivationContextDetailedInformation2 /// /// If QueryActCtxW is called with this option and the function succeeds, the returned buffer contains detailed information about the activation context. /// This information is in the form of the ACTIVATION_CONTEXT_DETAILED_INFORMATION structure. /// /// /// /// AssemblyDetailedInformationInActivationContext3 /// /// If QueryActCtxW is called with this option and the function succeeds, the buffer contains information about the assembly that has the index specified /// in pvSubInstance. This information is in the form of the ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION structure. /// /// /// /// FileInformationInAssemblyOfAssemblyInActivationContext4 /// /// Information about a file in one of the assemblies in Activation Context. The pvSubInstance parameter must point to an ACTIVATION_CONTEXT_QUERY_INDEX /// structure. If QueryActCtxW is called with this option and the function succeeds, the returned buffer contains information for a file in the assembly. /// This information is in the form of the ASSEMBLY_FILE_DETAILED_INFORMATION structure. /// /// /// /// RunlevelInformationInActivationContext5 /// /// If QueryActCtxW is called with this option and the function succeeds, the buffer contains information about requested run level of the activation /// context. This information is in the form of the ACTIVATION_CONTEXT_RUN_LEVEL_INFORMATION structure.Windows Server 2003 and Windows XP: This value is /// not available. /// /// /// /// CompatibilityInformationInActivationContext6 /// /// If QueryActCtxW is called with this option and the function succeeds, the buffer contains information about requested compatibility context. This /// information is in the form of the ACTIVATION_CONTEXT_COMPATIBILITY_INFORMATION structure.Windows Server 2008 and earlier, and Windows Vista and /// earlier: This value is not available. This option is available beginning with Windows Server 2008 R2 and Windows 7. /// /// /// /// /// /// /// Pointer to a buffer that holds the returned information. This parameter is optional. If pvBuffer is null, then cbBuffer must be zero. If the /// size of the buffer pointed to by pvBuffer is too small, QueryActCtxW returns ERROR_INSUFFICIENT_BUFFER and no data is written into the buffer. /// See the Remarks section for the method you can use to determine the required size of the buffer. /// /// Size of the buffer in bytes pointed to by pvBuffer. This parameter is optional. /// /// Number of bytes written or required. The parameter pcbWrittenOrRequired can only be NULL when pvBuffer is NULL. If pcbWrittenOrRequired /// is non- NULL, it is filled with the number of bytes required to store the returned buffer. /// /// /// If the function succeeds, it returns TRUE. Otherwise, it returns FALSE. /// /// This function sets errors that can be retrieved by calling GetLastError. For an example, see Retrieving the Last-Error Code. For a complete /// list of error codes, see System Error Codes. /// /// // BOOL QueryActCtxW( _In_ DWORD dwFlags, _In_ HANDLE hActCtx, _In_opt_ PVOID pvSubInstance, _In_ ULONG ulInfoClass, _Out_ PVOID pvBuffer, _In_opt_ // SIZE_T cbBuffer, _Out_opt_ SIZE_T *pcbWrittenOrRequired); https://msdn.microsoft.com/en-us/library/windows/desktop/aa375704(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa375704")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool QueryActCtxW(QueryActCtxFlag dwFlags, [In] HACTCTX hActCtx, [In] IntPtr pvSubInstance, ACTIVATION_CONTEXT_INFO_CLASS ulInfoClass, IntPtr pvBuffer, SizeT cbBuffer, out SizeT pcbWrittenOrRequired); /// 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 or one of the following error codes. /// /// /// /// Return code /// Description /// /// /// E_FAIL /// Internal error; the registration failed. /// /// /// E_INVALIDARG /// The ping interval cannot be more than five minutes. /// /// /// /// // HRESULT WINAPI RegisterApplicationRecoveryCallback( _In_ APPLICATION_RECOVERY_CALLBACK pRecoveryCallback, _In_opt_ PVOID pvParameter, _In_ DWORD // dwPingInterval, _In_ DWORD dwFlags); https://msdn.microsoft.com/en-us/library/windows/desktop/aa373345(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, 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 pointer to a Unicode 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. /// /// /// /// This parameter can be 0 or one or more of the following values. /// /// /// /// Value /// Meaning /// /// /// RESTART_NO_CRASH1 /// Do not restart the process if it terminates due to an unhandled exception. /// /// /// RESTART_NO_HANG2 /// Do not restart the process if it terminates due to the application not responding. /// /// /// RESTART_NO_PATCH4 /// Do not restart the process if it terminates due to the installation of an update. /// /// /// RESTART_NO_REBOOT8 /// Do not restart the process if the computer is restarted as the result of an update. /// /// /// /// /// /// This function returns S_OK on success or one of the following error codes. /// /// /// /// Return code /// Description /// /// /// E_FAIL /// Internal error. /// /// /// E_INVALIDARG /// The specified command line is too long. /// /// /// /// // HRESULT WINAPI RegisterApplicationRestart( _In_opt_ PCWSTR pwzCommandline, _In_ DWORD dwFlags); https://msdn.microsoft.com/en-us/library/windows/desktop/aa373347(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("Winbase.h", MSDNShortId = "aa373347")] public static extern HRESULT RegisterApplicationRestart(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. /// /// /// This function does not return a value. On successful completion, the activation context reference count is decremented. The recipient of the /// reference-counted object must decrement the reference count when the object is no longer required. /// // void ReleaseActCtx( _In_ HANDLE hActCtx); https://msdn.microsoft.com/en-us/library/windows/desktop/aa375713(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa375713")] public static extern void ReleaseActCtx(HACTCTX hActCtx); /// Removes the active instance of an application from the recovery list. /// /// /// This function returns S_OK on success or one of the following error codes. /// /// /// /// Return code /// Description /// /// /// E_FAIL /// Internal error. /// /// /// /// // HRESULT WINAPI UnregisterApplicationRecoveryCallback(void); https://msdn.microsoft.com/en-us/library/windows/desktop/aa373348(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, 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 ZombifyActCtx function deactivates the specified activation context, but does not deallocate it. /// Handle to the activation context that is to be deactivated. /// /// /// If the function succeeds, it returns TRUE. If a null handle is passed in the hActCtx parameter, NULL_INVALID_PARAMETER will be /// returned. Otherwise, it returns FALSE. /// /// /// This function sets errors that can be retrieved by calling GetLastError. For an example, see Retrieving the Last-Error Code. For a complete /// list of error codes, see System Error Codes. /// /// // BOOL ZombifyActCtx( _In_ HANDLE hActCtx); https://msdn.microsoft.com/en-us/library/windows/desktop/aa376622(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] [PInvokeData("Winbase.h", MSDNShortId = "aa376622")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool ZombifyActCtx(HACTCTX hActCtx); /// 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 HINSTANCE hModule; /// Initializes a new instance of the struct. /// The source. public ACTCTX(string source) : this() { cbSize = Marshal.SizeOf(typeof(ACTCTX)); lpSource = source; } /// Gets an empty instance with only the cbSize parameter initialized. public static ACTCTX Empty => new ACTCTX { cbSize = Marshal.SizeOf(typeof(ACTCTX)) }; } /// /// The ACTCTX_SECTION_KEYED_DATA structure is used by the FindActCtxSectionString and FindActCtxSectionGuid functions to return the /// activation context information along with either the GUID or 32-bit integer-tagged activation context section. /// // typedef struct tagACTCTX_SECTION_KEYED_DATA { ULONG cbSize; ULONG ulDataFormatVersion; PVOID lpData; ULONG ulLength; PVOID lpSectionGlobalData; ULONG // ulSectionGlobalDataLength; PVOID lpSectionBase; ULONG ulSectionTotalLength; HANDLE hActCtx; HANDLE ulAssemblyRosterIndex;} ACTCTX_SECTION_KEYED_DATA, *PACTCTX_SECTION_KEYED_DATA; [StructLayout(LayoutKind.Sequential)] [PInvokeData("Winbase.h", MSDNShortId = "aa374148")] public struct ACTCTX_SECTION_KEYED_DATA { /// /// The size, in bytes, of the activation context keyed data structure. /// public uint cbSize; /// /// /// Number that indicates the format of the data in the section where the key was found. Clients should verify that the data format version is as /// expected rather than trying to interpret the values of unfamiliar data formats. This number is only changed when major non-backward-compatible /// changes to the section data formats need to be made. The current format version is 1. /// /// public uint ulDataFormatVersion; /// /// Pointer to the redirection data found associated with the section identifier and key. /// public IntPtr lpData; /// /// /// Number of bytes in the structure referred to by lpData. Note that the data structures grow over time; do not access members in the /// instance data that extend beyond ulLength. /// /// public uint ulLength; /// /// /// Returned pointer to a section-specific data structure which is global to the activation context section where the key was found. Its /// interpretation depends on the section identifier requested. /// /// public IntPtr lpSectionGlobalData; /// /// Number of bytes in the section global data block referred to by lpSectionGlobalData. /// /// Note that the data structures grow over time and you may receive an old format activation context data block; do not access members in the /// section global data that extend beyond ulSectionGlobalDataLength. /// /// public uint ulSectionGlobalDataLength; /// /// /// Pointer to the base of the section where the key was found. Some instance data contains offsets relative to the section base address, in which /// case this pointer value is used. /// /// public IntPtr lpSectionBase; /// /// /// Number of bytes for the entire section starting at lpSectionBase. May be used to verify that offset/length pairs, which are specified as /// relative to the section base are wholly contained in the section. /// /// public uint ulSectionTotalLength; /// /// /// Handle to the activation context where the key was found. First, the active activation context for the thread is searched, followed by the /// process-default activation context and then the system-compatible-default-activation context. This member indicates which activation context /// contained the section and key requested. This is only returned if the FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX flag is passed. /// /// /// Note that when this is returned, the caller must call ReleaseActCtx() on the activation context handle returned to release system /// resources when all other references to the activation context have been released. /// /// public HACTCTX hActCtx; /// /// /// Cardinal number of the assembly in the activation context that provided the redirection information found. This value can be presented to /// QueryActCtxW for more information about the contributing assembly. /// /// public uint ulAssemblyRosterIndex; } /// Provides a handle to an account context. [StructLayout(LayoutKind.Sequential)] public struct HACTCTX { private IntPtr handle; /// Initializes a new instance of the struct. /// An object that represents the pre-existing handle to use. public HACTCTX(IntPtr preexistingHandle) => handle = preexistingHandle; /// Returns an invalid handle by instantiating a object with . public static HACTCTX NULL => new HACTCTX(IntPtr.Zero); /// Gets a value indicating whether this instance is a null handle. public bool IsNull => handle == IntPtr.Zero; /// Performs an explicit conversion from to . /// The handle. /// The result of the conversion. public static explicit operator IntPtr(HACTCTX h) => h.handle; /// Performs an implicit conversion from to . /// The pointer to a handle. /// The result of the conversion. public static implicit operator HACTCTX(IntPtr h) => new HACTCTX(h); /// Implements the operator !=. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator !=(HACTCTX h1, HACTCTX h2) => !(h1 == h2); /// Implements the operator ==. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator ==(HACTCTX h1, HACTCTX h2) => h1.Equals(h2); /// public override bool Equals(object obj) => obj is HACTCTX h ? handle == h.handle : false; /// public override int GetHashCode() => handle.GetHashCode(); } /// Provides a to an account context that releases a created HACTCTX instance at disposal using ReleaseActCtx. public class SafeHACTCTX : HANDLE { /// Initializes a new instance of the class and assigns an existing handle. /// An object that represents the pre-existing handle to use. /// to reliably release the handle during the finalization phase; otherwise, (not recommended). public SafeHACTCTX(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { } private SafeHACTCTX() : base() { } /// Gets the handle to the active activation context of the calling thread. public static SafeHACTCTX GetCurrent() { if (!GetCurrentActCtx(out var h)) Win32Error.ThrowLastError(); return new SafeHACTCTX((IntPtr)h, false); } /// Performs an implicit conversion from to . /// The safe handle instance. /// The result of the conversion. public static implicit operator HACTCTX(SafeHACTCTX h) => h.handle; /// protected override bool InternalReleaseHandle() { ReleaseActCtx(this); return true; } } } }