From 36c538f872c25a0062213019a278185ac49e0529 Mon Sep 17 00:00:00 2001 From: David Hall Date: Wed, 26 Jun 2019 17:48:17 -0600 Subject: [PATCH] Added SECURITY_CAPABILITIES and UMS_CREATE_THREAD_ATTRIBUTES --- PInvoke/Kernel32/WinNT.cs | 213 +++++++++++++++++++++++++++++----------------- 1 file changed, 134 insertions(+), 79 deletions(-) diff --git a/PInvoke/Kernel32/WinNT.cs b/PInvoke/Kernel32/WinNT.cs index 6ebb2276..1c1df309 100644 --- a/PInvoke/Kernel32/WinNT.cs +++ b/PInvoke/Kernel32/WinNT.cs @@ -51,8 +51,8 @@ namespace Vanara.PInvoke /// /// To unregister the callback function, use the RemoveSecureMemoryCacheCallback function. /// - // https://docs.microsoft.com/en-us/windows/desktop/api/winnt/nc-winnt-psecure_memory_cache_callback - // BOOLEAN PsecureMemoryCacheCallback( PVOID Addr, SIZE_T Range ) {...} + // https://docs.microsoft.com/en-us/windows/desktop/api/winnt/nc-winnt-psecure_memory_cache_callback BOOLEAN + // PsecureMemoryCacheCallback( PVOID Addr, SIZE_T Range ) {...} [UnmanagedFunctionPointer(CallingConvention.Winapi)] [PInvokeData("winnt.h", MSDNShortId = "abde4b6f-7cd8-4a4b-9b00-f035b2c29054")] [return: MarshalAs(UnmanagedType.U1)] @@ -161,83 +161,6 @@ namespace Vanara.PInvoke [UnmanagedFunctionPointer(CallingConvention.Winapi)] public delegate void RtlUmsSchedulerEntryPoint(RTL_UMS_SCHEDULER_REASON Reason, IntPtr ActivationPayload, IntPtr SchedulerParam); - /// Used by thread context functions. - [PInvokeData("winnt.h")] - public static class CONTEXT_FLAG - { - private static readonly uint systemContext; - - static CONTEXT_FLAG() - { - GetNativeSystemInfo(out var info); - switch (info.wProcessorArchitecture) - { - case ProcessorArchitecture.PROCESSOR_ARCHITECTURE_INTEL: - systemContext = CONTEXT_i386; - break; - case ProcessorArchitecture.PROCESSOR_ARCHITECTURE_ARM: - systemContext = CONTEXT_ARM; - break; - case ProcessorArchitecture.PROCESSOR_ARCHITECTURE_AMD64: - systemContext = CONTEXT_AMD64; - break; - default: - throw new InvalidOperationException("Processor context not recognized."); - } - } - - /// Undocumented. - public const uint CONTEXT_AMD64 = 0x00100000; - - /// Undocumented. - public const uint CONTEXT_ARM = 0x00200000; - - /// Undocumented. - public const uint CONTEXT_i386 = 0x00010000; - - /// Undocumented. - public static uint CONTEXT_CONTROL => systemContext | 0x00000001; - - /// Undocumented. - public static uint CONTEXT_INTEGER => systemContext | 0x00000002; - - /// Undocumented. - public static uint CONTEXT_SEGMENTS => systemContext | 0x00000004; - - /// Undocumented. - public static uint CONTEXT_FLOATING_POINT => systemContext | 0x00000008; - - /// Undocumented. - public static uint CONTEXT_DEBUG_REGISTERS => systemContext | 0x00000010; - - /// Undocumented. - public static uint CONTEXT_EXTENDED_REGISTERS => systemContext | 0x00000020; - - /// Undocumented. - public static uint CONTEXT_FULL => CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_FLOATING_POINT; - - /// Undocumented. - public static uint CONTEXT_ALL => CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS | CONTEXT_FLOATING_POINT | CONTEXT_DEBUG_REGISTERS; - - /// Undocumented. - public static uint CONTEXT_XSTATE => systemContext | 0x00000040; - - /// Undocumented. - public const uint CONTEXT_KERNEL_DEBUGGER = 0x04000000; - - /// Undocumented. - public const uint CONTEXT_EXCEPTION_ACTIVE = 0x08000000; - - /// Undocumented. - public const uint CONTEXT_SERVICE_ACTIVE = 0x10000000; - - /// Undocumented. - public const uint CONTEXT_EXCEPTION_REQUEST = 0x40000000; - - /// Undocumented. - public const uint CONTEXT_EXCEPTION_REPORTING = 0x80000000; - } - /// The flags that control the enforcement of the minimum and maximum working set sizes. [PInvokeData("winnt.h")] [Flags] @@ -319,16 +242,22 @@ namespace Vanara.PInvoke { /// Query the section object for information about the section. Drivers should set this flag. SECTION_QUERY = 0x0001, + /// Write views of the section. SECTION_MAP_WRITE = 0x0002, + /// Read views of the section. SECTION_MAP_READ = 0x0004, + /// Execute views of the section. SECTION_MAP_EXECUTE = 0x0008, + /// Dynamically extend the size of the section. SECTION_EXTEND_SIZE = 0x0010, + /// Undocumented. SECTION_MAP_EXECUTE_EXPLICIT = 0x0020, + /// All of the previous flags combined with STANDARD_RIGHTS_REQUIRED. SECTION_ALL_ACCESS = ACCESS_MASK.STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_WRITE | SECTION_MAP_READ | SECTION_MAP_EXECUTE | SECTION_EXTEND_SIZE, } @@ -462,6 +391,52 @@ namespace Vanara.PInvoke public HARDWARE_COUNTER_DATA[] HwCounters; } + /// The SECURITY_CAPABILITIES structure defines the security capabilities of the app container. + // https://docs.microsoft.com/en-us/windows/desktop/api/winnt/ns-winnt-_security_capabilities typedef struct _SECURITY_CAPABILITIES { + // #if ... PISID AppContainerSid; #if ... PSID_AND_ATTRIBUTES Capabilities; #else PSID AppContainerSid; #endif #else + // PSID_AND_ATTRIBUTES Capabilities; #endif DWORD CapabilityCount; DWORD Reserved; } SECURITY_CAPABILITIES, *PSECURITY_CAPABILITIES, *LPSECURITY_CAPABILITIES; + [PInvokeData("winnt.h", MSDNShortId = "1A865519-E042-4871-886C-9AA64D71CCE4")] + [StructLayout(LayoutKind.Sequential)] + public struct SECURITY_CAPABILITIES + { + /// The SID of the app container. + public PSID AppContainerSid; + + /// The specific capabilities. + public IntPtr Capabilities; + + /// The number of the capabilities. + public uint CapabilityCount; + + /// This member is reserved. Do not use it. + public uint Reserved; + } + + /// + /// Specifies attributes for a user-mode scheduling (UMS) worker thread. + /// This structure is used with the UpdateProcThreadAttribute function. + /// + // https://docs.microsoft.com/en-us/windows/desktop/api/winnt/ns-winnt-ums_create_thread_attributes typedef struct + // _UMS_CREATE_THREAD_ATTRIBUTES { DWORD UmsVersion; PVOID UmsContext; PVOID UmsCompletionList; } UMS_CREATE_THREAD_ATTRIBUTES, *PUMS_CREATE_THREAD_ATTRIBUTES; + [PInvokeData("winnt.h", MSDNShortId = "5d3e1721-c439-49bb-9cb6-8386fa8aaf50")] + [StructLayout(LayoutKind.Sequential)] + public struct UMS_CREATE_THREAD_ATTRIBUTES + { + /// The UMS version for which the application was built. This parameter must be UMS_VERSION. + public uint UmsVersion; + + /// + /// A pointer to a UMS thread context for the worker thread to be created. This pointer is provided by the CreateUmsThreadContext function. + /// + public IntPtr UmsContext; + + /// + /// A pointer to a UMS completion list. This pointer is provided by the CreateUmsCompletionList function. The newly created + /// worker thread is queued to the specified completion list. + /// + public IntPtr UmsCompletionList; + } + /// /// Represents a context frame on WOW64. Refer to the header file WinNT.h for the definition of this structure. /// @@ -710,5 +685,85 @@ namespace Vanara.PInvoke /// public bool Granularity { get => GetBit(Flags, 15); set => SetBit(ref Flags, 15, value); } } + + /// Used by thread context functions. + [PInvokeData("winnt.h")] + public static class CONTEXT_FLAG + { + /// Undocumented. + public const uint CONTEXT_AMD64 = 0x00100000; + + /// Undocumented. + public const uint CONTEXT_ARM = 0x00200000; + + /// Undocumented. + public const uint CONTEXT_EXCEPTION_ACTIVE = 0x08000000; + + /// Undocumented. + public const uint CONTEXT_EXCEPTION_REPORTING = 0x80000000; + + /// Undocumented. + public const uint CONTEXT_EXCEPTION_REQUEST = 0x40000000; + + /// Undocumented. + public const uint CONTEXT_i386 = 0x00010000; + + /// Undocumented. + public const uint CONTEXT_KERNEL_DEBUGGER = 0x04000000; + + /// Undocumented. + public const uint CONTEXT_SERVICE_ACTIVE = 0x10000000; + + private static readonly uint systemContext; + + static CONTEXT_FLAG() + { + GetNativeSystemInfo(out var info); + switch (info.wProcessorArchitecture) + { + case ProcessorArchitecture.PROCESSOR_ARCHITECTURE_INTEL: + systemContext = CONTEXT_i386; + break; + + case ProcessorArchitecture.PROCESSOR_ARCHITECTURE_ARM: + systemContext = CONTEXT_ARM; + break; + + case ProcessorArchitecture.PROCESSOR_ARCHITECTURE_AMD64: + systemContext = CONTEXT_AMD64; + break; + + default: + throw new InvalidOperationException("Processor context not recognized."); + } + } + + /// Undocumented. + public static uint CONTEXT_ALL => CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS | CONTEXT_FLOATING_POINT | CONTEXT_DEBUG_REGISTERS; + + /// Undocumented. + public static uint CONTEXT_CONTROL => systemContext | 0x00000001; + + /// Undocumented. + public static uint CONTEXT_DEBUG_REGISTERS => systemContext | 0x00000010; + + /// Undocumented. + public static uint CONTEXT_EXTENDED_REGISTERS => systemContext | 0x00000020; + + /// Undocumented. + public static uint CONTEXT_FLOATING_POINT => systemContext | 0x00000008; + + /// Undocumented. + public static uint CONTEXT_FULL => CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_FLOATING_POINT; + + /// Undocumented. + public static uint CONTEXT_INTEGER => systemContext | 0x00000002; + + /// Undocumented. + public static uint CONTEXT_SEGMENTS => systemContext | 0x00000004; + + /// Undocumented. + public static uint CONTEXT_XSTATE => systemContext | 0x00000040; + } } } \ No newline at end of file