using System; using System.Runtime.InteropServices; using System.Text; using Vanara.Extensions; 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); /// /// The ACTCTX_COMPATIBILITY_ELEMENT_TYPE enumeration describes the compatibility element in the application manifest. /// // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ne-winnt-actctx_compatibility_element_type typedef enum { // ACTCTX_COMPATIBILITY_ELEMENT_TYPE_UNKNOWN, ACTCTX_COMPATIBILITY_ELEMENT_TYPE_OS, ACTCTX_COMPATIBILITY_ELEMENT_TYPE_MITIGATION, // ACTCTX_COMPATIBILITY_ELEMENT_TYPE_MAXVERSIONTESTED } ; [PInvokeData("winnt.h", MSDNShortId = "3a3c99e5-9a73-4688-8192-baee0078c17c")] public enum ACTCTX_COMPATIBILITY_ELEMENT_TYPE { /// ACTCTX_COMPATIBILITY_ELEMENT_TYPE_UNKNOWN, /// ACTCTX_COMPATIBILITY_ELEMENT_TYPE_OS, /// ACTCTX_COMPATIBILITY_ELEMENT_TYPE_MITIGATION, /// ACTCTX_COMPATIBILITY_ELEMENT_TYPE_MAXVERSIONTESTED, } /// The ACTCTX_REQUESTED_RUN_LEVEL enumeration describes the requested run level of the activation context. // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ne-winnt-actctx_requested_run_level typedef enum { // ACTCTX_RUN_LEVEL_UNSPECIFIED, ACTCTX_RUN_LEVEL_AS_INVOKER, ACTCTX_RUN_LEVEL_HIGHEST_AVAILABLE, ACTCTX_RUN_LEVEL_REQUIRE_ADMIN, // ACTCTX_RUN_LEVEL_NUMBERS } ; [PInvokeData("winnt.h", MSDNShortId = "3bf75a4d-a209-43e4-9fe2-f7da1602fc6c")] public enum ACTCTX_REQUESTED_RUN_LEVEL { /// The application manifest does not specify a requested run level for the application. ACTCTX_RUN_LEVEL_UNSPECIFIED, /// The application manifest requests the least privilege level to run the application. ACTCTX_RUN_LEVEL_AS_INVOKER, /// The application manifest requests the highest privilege level to run the application. ACTCTX_RUN_LEVEL_HIGHEST_AVAILABLE, /// The application manifest requests the administrator privilege level to run the application. ACTCTX_RUN_LEVEL_REQUIRE_ADMIN, /// Total number of possible run levels. ACTCTX_RUN_LEVEL_NUMBERS, } /// 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, /// ACTIVATION_CONTEXT_SECTION_WINRT_ACTIVATABLE_CLASSES = 12 } /// 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. [CorrespondingType(typeof(ACTIVATION_CONTEXT_BASIC_INFORMATION))] 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. /// [CorrespondingType(typeof(ACTIVATION_CONTEXT_DETAILED_INFORMATION))] 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. /// [CorrespondingType(typeof(ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION))] AssemblyDetailedInformationInActivationContext = 3, /// /// Information about a file in one of the assemblies in Activation Context. The pvSubInstance parameter must point to an /// 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. /// [CorrespondingType(typeof(ASSEMBLY_FILE_DETAILED_INFORMATION))] 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. /// [CorrespondingType(typeof(ACTIVATION_CONTEXT_RUN_LEVEL_INFORMATION))] 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. /// [CorrespondingType(typeof(ACTIVATION_CONTEXT_COMPATIBILITY_INFORMATION))] [CorrespondingType(typeof(ACTIVATION_CONTEXT_COMPATIBILITY_INFORMATION_UNMGD))] CompatibilityInformationInActivationContext = 6, /// The activation context manifest resource name ActivationContextManifestResourceName = 7, } /// [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 pActCtx); /// 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 ulCookie); /// /// 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, [Optional] IntPtr lpExtensionGuid, ACTCTX_SECTION 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, [Optional] IntPtr 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 lphActCtx); /// /// 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([Optional] uint dwFlags, [In] HACTCTX hActCtx, [Optional] 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 /// /// /// ActivationContextBasicInformation 1 /// Not available. /// /// /// ActivationContextDetailedInformation 2 /// /// 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. /// /// /// /// AssemblyDetailedInformationInActivationContext 3 /// /// 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. /// /// /// /// FileInformationInAssemblyOfAssemblyInActivationContext 4 /// /// 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. /// /// /// /// RunlevelInformationInActivationContext 5 /// /// 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. /// /// /// /// CompatibilityInformationInActivationContext 6 /// /// 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. /// /// /// /// /// The parameter cbBuffer specifies the size in bytes of the buffer pointed to by pvBuffer. If pvBuffer is NULL, then /// cbBuffer must be 0. The parameter pcbWrittenOrRequired can only be NULL if pvBuffer is NULL. If /// pcbWrittenOrRequired is non- NULL on return, it is filled with the number of bytes required to store the returned /// information. When the information data returned is larger than the provided buffer, QueryActCtxW returns /// ERROR_INSUFFICIENT_BUFFER and no data is written to the buffer pointed to by pvBuffer. /// /// The following example shows the method of calling first with a small buffer and then recalling if the buffer is too small. /// // https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-queryactctxw // BOOL QueryActCtxW( DWORD dwFlags, HANDLE hActCtx, PVOID pvSubInstance, ULONG ulInfoClass, PVOID pvBuffer, SIZE_T cbBuffer, SIZE_T *pcbWrittenOrRequired ); [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)] [PInvokeData("winbase.h", MSDNShortId = "7d45f63f-0baf-4236-b245-d36f9eb32e8c")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool QueryActCtxW(QueryActCtxFlag dwFlags, [In] HACTCTX hActCtx, [In, Optional] IntPtr pvSubInstance, ACTIVATION_CONTEXT_INFO_CLASS ulInfoClass, IntPtr pvBuffer, SizeT cbBuffer, out SizeT pcbWrittenOrRequired); /// The QueryActCtxW function queries the activation context. /// The type of the requested return value. /// /// 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. /// /// This parameter can have only the values shown in the following table. /// /// /// Option /// Meaning /// /// /// ActivationContextBasicInformation 1 /// Not available. /// /// /// ActivationContextDetailedInformation 2 /// /// 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. /// /// /// /// AssemblyDetailedInformationInActivationContext 3 /// /// 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. /// /// /// /// FileInformationInAssemblyOfAssemblyInActivationContext 4 /// /// 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. /// /// /// /// RunlevelInformationInActivationContext 5 /// /// 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. /// /// /// /// CompatibilityInformationInActivationContext 6 /// /// 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. /// /// /// /// /// /// /// 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. /// /// /// /// /// The returned information. /// public static T QueryActCtxW(QueryActCtxFlag dwFlags, [In] HACTCTX hActCtx, ACTIVATION_CONTEXT_INFO_CLASS ulInfoClass, [In] object pvSubInstance = null) where T : struct { if (!CorrespondingTypeAttribute.CanGet(ulInfoClass, typeof(T))) throw new ArgumentException(); if (!QueryActCtxW(dwFlags, hActCtx, pvSubInstance is null ? IntPtr.Zero : new PinnedObject(pvSubInstance), ulInfoClass, default, 0, out var req) && req == 0) Win32Error.ThrowLastError(); using (var mem = new SafeCoTaskMemHandle(req)) { if (!QueryActCtxW(dwFlags, hActCtx, pvSubInstance is null ? IntPtr.Zero : new PinnedObject(pvSubInstance), ulInfoClass, (IntPtr)mem, mem.Size, out req)) Win32Error.ThrowLastError(); if (typeof(T) == typeof(ACTIVATION_CONTEXT_COMPATIBILITY_INFORMATION)) return (T)(object)new ACTIVATION_CONTEXT_COMPATIBILITY_INFORMATION((IntPtr)mem); return mem.ToStructure(); } } /// 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, [Optional] IntPtr pvParameter, [Optional] uint dwPingInterval, [Optional] 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; } /// The ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION structure is used by the QueryActCtxW function. /// /// If QueryActCtxW is called with the AssemblyDetailedInformationInActivationContext option, and the function succeeds, the /// information in the returned buffer is in the form of the ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION structure. /// // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-activation_context_assembly_detailed_information typedef struct // _ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION { DWORD ulFlags; DWORD ulEncodedAssemblyIdentityLength; DWORD // ulManifestPathType; DWORD ulManifestPathLength; LARGE_INTEGER liManifestLastWriteTime; DWORD ulPolicyPathType; DWORD // ulPolicyPathLength; LARGE_INTEGER liPolicyLastWriteTime; DWORD ulMetadataSatelliteRosterIndex; DWORD ulManifestVersionMajor; DWORD // ulManifestVersionMinor; DWORD ulPolicyVersionMajor; DWORD ulPolicyVersionMinor; DWORD ulAssemblyDirectoryNameLength; PCWSTR // lpAssemblyEncodedAssemblyIdentity; PCWSTR lpAssemblyManifestPath; PCWSTR lpAssemblyPolicyPath; PCWSTR lpAssemblyDirectoryName; // DWORD ulFileCount; } ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION, *PACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION; [PInvokeData("winnt.h", MSDNShortId = "b093cc6a-55ea-49bf-904d-2b43517f9b02")] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION { /// This value is always 0. public uint ulFlags; /// Length of the encoded assembly identity in bytes. public uint ulEncodedAssemblyIdentityLength; /// This value always a constant. public uint ulManifestPathType; /// Length of the assembly manifest path in bytes. public uint ulManifestPathLength; /// The last time the manifest was written. This is in the form of a FILETIME data structure. public int liManifestLastWriteTime; /// This value always a constant. public uint ulPolicyPathType; /// Length of the publisher policy path in bytes. public uint ulPolicyPathLength; /// The last time the policy was written. This is in the form of a FILETIME data structure. public int liPolicyLastWriteTime; /// Metadata satellite roster index. public uint ulMetadataSatelliteRosterIndex; /// Major version of the assembly queried by QueryActCtxW. For more information, see Assembly Versions. public uint ulManifestVersionMajor; /// Minor version of the assembly queried by QueryActCtxW. For more information, see Assembly Versions. public uint ulManifestVersionMinor; /// Major version of any policy, if one exists. public uint ulPolicyVersionMajor; /// Minor version of any policy, if one exists. public uint ulPolicyVersionMinor; /// Length of the assembly directory name in bytes. public uint ulAssemblyDirectoryNameLength; /// Pointer to a null-terminated string that contains a textually-encoded format of the assembly's identity. [MarshalAs(UnmanagedType.LPWStr)] public string lpAssemblyEncodedAssemblyIdentity; /// Pointer to a null-terminated string that indicates the original path to this assembly's manifest. [MarshalAs(UnmanagedType.LPWStr)] public string lpAssemblyManifestPath; /// /// Pointer to a null-terminated string that indicates the path of whatever policy assembly was used to determine that this /// version of the assembly should be loaded. If this member is null, no policy was used to decide to load this version. /// [MarshalAs(UnmanagedType.LPWStr)] public string lpAssemblyPolicyPath; /// Pointer to a null-terminated string that indicates the folder from which this assembly was loaded. [MarshalAs(UnmanagedType.LPWStr)] public string lpAssemblyDirectoryName; /// public uint ulFileCount; } /// Undocumented. [StructLayout(LayoutKind.Sequential)] public struct ACTIVATION_CONTEXT_BASIC_INFORMATION { /// Undocumented. public HACTCTX hActCtx; /// Undocumented. public uint dwFlags; } /// The ACTIVATION_CONTEXT_COMPATIBILITY_INFORMATION_UNMGD structure is used by the QueryActCtxW function. /// /// The following example requires Windows Server 2008 R2 or Windows 7 and shows the method to retrieve information about the /// compatibility context. /// // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-activation_context_compatibility_information typedef struct // _ACTIVATION_CONTEXT_COMPATIBILITY_INFORMATION { DWORD ElementCount; COMPATIBILITY_CONTEXT_ELEMENT Elements[]; } // ACTIVATION_CONTEXT_COMPATIBILITY_INFORMATION, *PACTIVATION_CONTEXT_COMPATIBILITY_INFORMATION; [PInvokeData("winnt.h", MSDNShortId = "d8c1ef4a-8e64-45bd-a185-b4af7932a0d2")] [StructLayout(LayoutKind.Sequential)] public struct ACTIVATION_CONTEXT_COMPATIBILITY_INFORMATION_UNMGD { /// The number of compatibility elements defined in the application manifest. public uint ElementCount; /// /// This is an array of COMPATIBILITY_CONTEXT_ELEMENT structures. Each structure describes one compatibility element in the /// application manifest. /// public IntPtr Elements; } /// /// The ACTIVATION_CONTEXT_COMPATIBILITY_INFORMATION structure is the managed equivalent of the /// ACTIVATION_CONTEXT_COMPATIBILITY_INFORMATION_UNMGD structure used by the QueryActCtxW function. /// public struct ACTIVATION_CONTEXT_COMPATIBILITY_INFORMATION { /// /// This is an array of COMPATIBILITY_CONTEXT_ELEMENT structures. Each structure describes one compatibility element in the /// application manifest. /// public COMPATIBILITY_CONTEXT_ELEMENT[] Elements; /// Initializes a new instance of the struct. /// The unmanaged pointer to this info. internal ACTIVATION_CONTEXT_COMPATIBILITY_INFORMATION(IntPtr mem) { var c = Marshal.ReadInt32(mem, 0); Elements = mem.Offset(4).ToArray(c); } } /// The ACTIVATION_CONTEXT_DETAILED_INFORMATION structure is used by the QueryActCtxW function. /// /// If QueryActCtxW is called with the ActivationContextDetailedInformation option, and the function succeeds, the information in the /// returned buffer is in the form of the ACTIVATION_CONTEXT_DETAILED_INFORMATION structure. The following is an example of a /// structure used to hold detailed information about the activation context and a call from QueryActCtxW. /// // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-activation_context_detailed_information typedef struct // _ACTIVATION_CONTEXT_DETAILED_INFORMATION { DWORD dwFlags; DWORD ulFormatVersion; DWORD ulAssemblyCount; DWORD // ulRootManifestPathType; DWORD ulRootManifestPathChars; DWORD ulRootConfigurationPathType; DWORD ulRootConfigurationPathChars; // DWORD ulAppDirPathType; DWORD ulAppDirPathChars; PCWSTR lpRootManifestPath; PCWSTR lpRootConfigurationPath; PCWSTR lpAppDirPath; } // ACTIVATION_CONTEXT_DETAILED_INFORMATION, *PACTIVATION_CONTEXT_DETAILED_INFORMATION; [PInvokeData("winnt.h", MSDNShortId = "58e4acfe-d5c8-45ae-bf32-469229ffc836")] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct ACTIVATION_CONTEXT_DETAILED_INFORMATION { /// This value is always 0. public uint dwFlags; /// /// This value specifies the format of the returned information. On Windows XP and Windows Server 2003 this member is always 1. /// public uint ulFormatVersion; /// Number of assemblies in the activation context. public uint ulAssemblyCount; /// /// Specifies the kind of path from which this assembly's manifest was loaded. /// This member is always one of the following constants: /// public uint ulRootManifestPathType; /// Number of characters in the manifest path. public uint ulRootManifestPathChars; /// /// Specifies the kind of path from which this assembly's application configuration manifest was loaded. /// This member is always one of the following constants: /// public uint ulRootConfigurationPathType; /// Number of characters in any application configuration file path. public uint ulRootConfigurationPathChars; /// /// Specifies the kind of path from which this application manifest was loaded. /// This member is always one of the following constants: /// public uint ulAppDirPathType; /// Number of characters in the application directory. public uint ulAppDirPathChars; /// Path of the application manifest. [MarshalAs(UnmanagedType.LPWStr)] public string lpRootManifestPath; /// Path of the configuration file. [MarshalAs(UnmanagedType.LPWStr)] public string lpRootConfigurationPath; /// Path of the application directory. [MarshalAs(UnmanagedType.LPWStr)] public string lpAppDirPath; } /// The ACTIVATION_CONTEXT_QUERY_INDEX structure is used by QueryActCtxW function. /// /// Calling the QueryActCtxW function with the FileInformationInAssemblyOfAssemblyInActivationContext option requires that the /// pvSubInstance parameter point to an ACTIVATION_CONTEXT_QUERY_INDEX structure. See the sample for /// ASSEMBLY_FILE_DETAILED_INFORMATION for an example of its use. /// // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-activation_context_query_index typedef struct // _ACTIVATION_CONTEXT_QUERY_INDEX { DWORD ulAssemblyIndex; DWORD ulFileIndexInAssembly; } ACTIVATION_CONTEXT_QUERY_INDEX, *PACTIVATION_CONTEXT_QUERY_INDEX; [PInvokeData("winnt.h", MSDNShortId = "eb15895c-07c9-4b68-83ef-2f2b8e3b271c")] [StructLayout(LayoutKind.Sequential)] public struct ACTIVATION_CONTEXT_QUERY_INDEX { /// One-based index of the assembly whose file table is to be queried. public uint ulAssemblyIndex; /// Zero-based index of the file in the above assembly to be queried. public uint ulFileIndexInAssembly; /// Initializes a new instance of the struct. /// One-based index of the assembly whose file table is to be queried. /// Zero-based index of the file in the above assembly to be queried. public ACTIVATION_CONTEXT_QUERY_INDEX(uint asmIndex, uint fileIndex) { ulAssemblyIndex = asmIndex; ulFileIndexInAssembly = fileIndex; } } /// The ACTIVATION_CONTEXT_RUN_LEVEL_INFORMATION structure is used by the QueryActCtxW function. // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-activation_context_run_level_information typedef struct // _ACTIVATION_CONTEXT_RUN_LEVEL_INFORMATION { DWORD ulFlags; ACTCTX_REQUESTED_RUN_LEVEL RunLevel; DWORD UiAccess; } // ACTIVATION_CONTEXT_RUN_LEVEL_INFORMATION, *PACTIVATION_CONTEXT_RUN_LEVEL_INFORMATION; [PInvokeData("winnt.h", MSDNShortId = "1c4e7333-6982-4d58-ab2a-d1993c59d0ef")] [StructLayout(LayoutKind.Sequential)] public struct ACTIVATION_CONTEXT_RUN_LEVEL_INFORMATION { /// This parameter is reserved for future use. This parameter currently returns 0. public uint ulFlags; /// A ACTCTX_REQUESTED_RUN_LEVEL enumeration value that gives the requested run level of the activation context. public ACTCTX_REQUESTED_RUN_LEVEL RunLevel; /// /// This parameter returns zero if the uiAccess attribute in the application manifest is false. This parameter returns a /// non-zero value if the uiAccess attribute in the manifest is true. True means that UI accessibility applications /// require access higher privileges. /// public uint UiAccess; } /// The ASSEMBLY_FILE_DETAILED_INFORMATION structure is used by the QueryActCtxW function. /// /// If QueryActCtxW is called with the FileInformationInAssemblyOfAssemblyInActivationContext option, and the function succeeds, the /// information in the returned buffer is in form of the ASSEMBLY_FILE_DETAILED_INFORMATION structure. The following is an /// example of a structure used to hold detailed information about the activation context and a call from QueryActCtxW. /// // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-_assembly_file_detailed_information typedef struct // _ASSEMBLY_FILE_DETAILED_INFORMATION { DWORD ulFlags; DWORD ulFilenameLength; DWORD ulPathLength; PCWSTR lpFileName; PCWSTR // lpFilePath; } ASSEMBLY_FILE_DETAILED_INFORMATION, *PASSEMBLY_FILE_DETAILED_INFORMATION; [PInvokeData("winnt.h", MSDNShortId = "7f1e5155-a6c1-4b6a-be47-37fab337186c")] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct ASSEMBLY_FILE_DETAILED_INFORMATION { /// This value is always 0. public uint ulFlags; /// /// Length in bytes of the file name pointed to by lpFileName. The count does not include the terminating null character. /// public uint ulFilenameLength; /// /// Length in bytes of the path string pointed to by lpFilePath The count does not include the terminating null character. /// public uint ulPathLength; /// Null-terminated string that specifies the name of the file. [MarshalAs(UnmanagedType.LPWStr)] public string lpFileName; /// Null-terminated string that specifies the path to the file named in lpFileName. [MarshalAs(UnmanagedType.LPWStr)] public string lpFilePath; } /// /// The COMPATIBILITY_CONTEXT_ELEMENT structure is used by the QueryActCtxW function as part of the /// ACTIVATION_CONTEXT_COMPATIBILITY_INFORMATION structure. /// // https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-_compatibility_context_element typedef struct // _COMPATIBILITY_CONTEXT_ELEMENT { GUID Id; ACTCTX_COMPATIBILITY_ELEMENT_TYPE Type; } COMPATIBILITY_CONTEXT_ELEMENT, *PCOMPATIBILITY_CONTEXT_ELEMENT; [PInvokeData("winnt.h", MSDNShortId = "3e654f44-43f6-4282-b277-14ed6e25abf2")] [StructLayout(LayoutKind.Sequential)] public struct COMPATIBILITY_CONTEXT_ELEMENT { /// /// This is a GUID that specifies a version of Windows. /// /// /// Value /// Meaning /// /// /// {e2011457-1546-43c5-a5fe-008deee3d3f0} /// Windows Vista /// /// /// {35138b9a-5d96-4fbd-8e2d-a2440225f93a} /// Windows 7 /// /// /// public Guid Id; /// /// A value of the ACTCTX_COMPATIBILITY_ELEMENT_TYPE enumeration that describes the compatibility elements in the application manifest. /// public ACTCTX_COMPATIBILITY_ELEMENT_TYPE Type; } /// 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 : SafeHANDLE { /// 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); } /// Represents a NULL value. public static HACTCTX Null => new SafeHACTCTX(IntPtr.Zero, 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; } } } }