diff --git a/PInvoke/Gdi32/WinGdi.FontText.cs b/PInvoke/Gdi32/WinGdi.FontText.cs index 37d09a80..e576e67d 100644 --- a/PInvoke/Gdi32/WinGdi.FontText.cs +++ b/PInvoke/Gdi32/WinGdi.FontText.cs @@ -4841,8 +4841,9 @@ namespace Vanara.PInvoke // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-glyphset typedef struct tagGLYPHSET { DWORD cbThis; DWORD // flAccel; DWORD cGlyphsSupported; DWORD cRanges; WCRANGE ranges[1]; } GLYPHSET, *PGLYPHSET, *LPGLYPHSET; [PInvokeData("wingdi.h", MSDNShortId = "b8ac8d3f-b062-491c-966f-02f3d4c11419")] + [VanaraMarshaler(typeof(SafeAnysizeStructMarshaler), nameof(cRanges))] [StructLayout(LayoutKind.Sequential)] - public struct GLYPHSET : IVanaraMarshaler + public struct GLYPHSET { /// The size, in bytes, of this structure. public uint cbThis; @@ -4871,10 +4872,6 @@ namespace Vanara.PInvoke /// Array of Unicode ranges that are supported in the font. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] public WCRANGE[] ranges; - - SizeT IVanaraMarshaler.GetNativeSize() => 20; - SafeAllocatedMemoryHandle IVanaraMarshaler.MarshalManagedToNative(object managedObject) => new SafeAnysizeStruct((GLYPHSET)managedObject, nameof(cRanges)); - object IVanaraMarshaler.MarshalNativeToManaged(IntPtr p, SizeT s) => p != IntPtr.Zero ? new SafeAnysizeStruct(p, s, nameof(cRanges)).Value : default; } /// The KERNINGPAIR structure defines a kerning pair. diff --git a/PInvoke/Kernel32/WinIOCtl.Structs.cs b/PInvoke/Kernel32/WinIOCtl.Structs.cs index de8eac99..a8546441 100644 --- a/PInvoke/Kernel32/WinIOCtl.Structs.cs +++ b/PInvoke/Kernel32/WinIOCtl.Structs.cs @@ -1041,8 +1041,9 @@ namespace Vanara.PInvoke // _DRIVE_LAYOUT_INFORMATION { DWORD PartitionCount; DWORD Signature; PARTITION_INFORMATION PartitionEntry[1]; } // DRIVE_LAYOUT_INFORMATION, *PDRIVE_LAYOUT_INFORMATION; [PInvokeData("winioctl.h", MSDNShortId = "e67ccaa7-a735-4695-8385-28f57b41821c")] + [VanaraMarshaler(typeof(SafeAnysizeStructMarshaler), nameof(PartitionCount))] [StructLayout(LayoutKind.Sequential)] - public struct DRIVE_LAYOUT_INFORMATION : IVanaraMarshaler + public struct DRIVE_LAYOUT_INFORMATION { /// /// The number of partitions on a drive. @@ -1059,10 +1060,6 @@ namespace Vanara.PInvoke /// A variable-sized array of PARTITION_INFORMATION structures, one structure for each partition on a drive. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] public PARTITION_INFORMATION[] PartitionEntry; - - SizeT IVanaraMarshaler.GetNativeSize() => Marshal.SizeOf(GetType()); - SafeAllocatedMemoryHandle IVanaraMarshaler.MarshalManagedToNative(object obj) => new SafeAnysizeStruct((DRIVE_LAYOUT_INFORMATION)obj, nameof(PartitionCount)); - object IVanaraMarshaler.MarshalNativeToManaged(IntPtr p, SizeT s) => p != IntPtr.Zero ? new SafeAnysizeStruct(p, s, nameof(PartitionCount)).Value : default; } /// Contains extended information about a drive's partitions. @@ -1070,8 +1067,9 @@ namespace Vanara.PInvoke // _DRIVE_LAYOUT_INFORMATION_EX { DWORD PartitionStyle; DWORD PartitionCount; union { DRIVE_LAYOUT_INFORMATION_MBR Mbr; // DRIVE_LAYOUT_INFORMATION_GPT Gpt; } DUMMYUNIONNAME; PARTITION_INFORMATION_EX PartitionEntry[1]; } DRIVE_LAYOUT_INFORMATION_EX, *PDRIVE_LAYOUT_INFORMATION_EX; [PInvokeData("winioctl.h", MSDNShortId = "381c87a8-fe40-4251-a4df-dddc9e2a126d")] + [VanaraMarshaler(typeof(SafeAnysizeStructMarshaler), nameof(PartitionCount))] [StructLayout(LayoutKind.Explicit)] - public struct DRIVE_LAYOUT_INFORMATION_EX : IVanaraMarshaler + public struct DRIVE_LAYOUT_INFORMATION_EX { /// /// The style of the partitions on the drive enumerated by the PARTITION_STYLE enumeration. @@ -1122,10 +1120,6 @@ namespace Vanara.PInvoke [FieldOffset(48)] [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] public PARTITION_INFORMATION_EX[] PartitionEntry; - - SizeT IVanaraMarshaler.GetNativeSize() => Marshal.SizeOf(GetType()); - SafeAllocatedMemoryHandle IVanaraMarshaler.MarshalManagedToNative(object obj) => new SafeAnysizeStruct((DRIVE_LAYOUT_INFORMATION_EX)obj, nameof(PartitionCount)); - object IVanaraMarshaler.MarshalNativeToManaged(IntPtr p, SizeT s) => p != IntPtr.Zero ? new SafeAnysizeStruct(p, s, nameof(PartitionCount)).Value : default; } /// Contains information about a drive's GUID partition table (GPT) partitions. @@ -3883,8 +3877,9 @@ namespace Vanara.PInvoke // Header; FILE_ID_128 FileReferenceNumber; FILE_ID_128 ParentFileReferenceNumber; USN Usn; DWORD Reason; DWORD SourceInfo; DWORD // RemainingExtents; WORD NumberOfExtents; WORD ExtentSize; USN_RECORD_EXTENT Extents[1]; } USN_RECORD_V4, *PUSN_RECORD_V4; [PInvokeData("winioctl.h", MSDNShortId = "2636D1A1-6FD1-4F84-954C-499DCCE6E390")] + [VanaraMarshaler(typeof(SafeAnysizeStructMarshaler), nameof(NumberOfExtents))] [StructLayout(LayoutKind.Sequential, Size = 80)] - public struct USN_RECORD_V4 : IVanaraMarshaler + public struct USN_RECORD_V4 { /// /// A USN_RECORD_COMMON_HEADER structure that describes the record length, major version, and minor version for the record. @@ -4104,10 +4099,6 @@ namespace Vanara.PInvoke /// An array of USN_RECORD_EXTENT structures that represent the extents in the USN_RECORD_V4 entry. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] public USN_RECORD_EXTENT[] Extents; - - SizeT IVanaraMarshaler.GetNativeSize() => Marshal.SizeOf(GetType()); - SafeAllocatedMemoryHandle IVanaraMarshaler.MarshalManagedToNative(object obj) => new SafeAnysizeStruct((USN_RECORD_V4)obj, nameof(NumberOfExtents)); - object IVanaraMarshaler.MarshalNativeToManaged(IntPtr p, SizeT s) => p != IntPtr.Zero ? new SafeAnysizeStruct(p, s, nameof(NumberOfExtents)).Value : default; } /// diff --git a/PInvoke/Pdh/Pdh.cs b/PInvoke/Pdh/Pdh.cs index 24ed25a1..852e5191 100644 --- a/PInvoke/Pdh/Pdh.cs +++ b/PInvoke/Pdh/Pdh.cs @@ -5089,8 +5089,9 @@ namespace Vanara.PInvoke // https://docs.microsoft.com/en-us/windows/win32/api/pdh/ns-pdh-pdh_raw_log_record typedef struct _PDH_RAW_LOG_RECORD { DWORD // dwStructureSize; DWORD dwRecordType; DWORD dwItems; UCHAR RawBytes[1]; } PDH_RAW_LOG_RECORD, *PPDH_RAW_LOG_RECORD; [PInvokeData("pdh.h", MSDNShortId = "ae96515f-ea3f-4578-a249-fb8f41cdfa69")] + [VanaraMarshaler(typeof(SafeAnysizeStructMarshaler), nameof(dwItems))] [StructLayout(LayoutKind.Sequential)] - public struct PDH_RAW_LOG_RECORD : IVanaraMarshaler + public struct PDH_RAW_LOG_RECORD { /// /// Size of this structure, in bytes. The size includes this structure and the RawBytes appended to the end of this structure. @@ -5134,10 +5135,6 @@ namespace Vanara.PInvoke /// Binary record. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] public byte[] RawBytes; - - SizeT IVanaraMarshaler.GetNativeSize() => Marshal.SizeOf(GetType()); - SafeAllocatedMemoryHandle IVanaraMarshaler.MarshalManagedToNative(object obj) => new SafeAnysizeStruct((PDH_RAW_LOG_RECORD)obj, nameof(dwItems)); - object IVanaraMarshaler.MarshalNativeToManaged(IntPtr p, SizeT s) => p != IntPtr.Zero ? new SafeAnysizeStruct(p, s, nameof(dwItems)).Value : default; } /// diff --git a/PInvoke/Security/AdvApi32/SecurityBaseApi.cs b/PInvoke/Security/AdvApi32/SecurityBaseApi.cs index 061a88fe..6c880084 100644 --- a/PInvoke/Security/AdvApi32/SecurityBaseApi.cs +++ b/PInvoke/Security/AdvApi32/SecurityBaseApi.cs @@ -6313,11 +6313,11 @@ namespace Vanara.PInvoke /// an identify level impersonation token. If the current token is not an application container but is an identity level token, /// you should return AccessDenied. /// - public T GetInfo(TOKEN_INFORMATION_CLASS tokenInfoClass) + public T GetInfo(TOKEN_INFORMATION_CLASS? tokenInfoClass = null) { - if (!CorrespondingTypeAttribute.CanGet(tokenInfoClass, typeof(T))) + if (!CorrespondingTypeAttribute.CanGet(tokenInfoClass, out var ti)) throw new InvalidCastException(); - using var pType = GetInfo(tokenInfoClass); + using var pType = GetInfo(ti); return ((IntPtr)pType).Convert(pType.Size); } @@ -6377,6 +6377,30 @@ namespace Vanara.PInvoke // SetTokenInformation( HANDLE TokenHandle, TOKEN_INFORMATION_CLASS TokenInformationClass, LPVOID TokenInformation, DWORD // TokenInformationLength ); public bool SetInfo(TOKEN_INFORMATION_CLASS TokenInformationClass, T TokenInformation) => SetTokenInformation(this, TokenInformationClass, TokenInformation); + + /// + /// The SetInfo function sets various types of information for a specified access token. The information that this + /// function sets replaces existing information. The calling process must have appropriate access rights to set the information. + /// + /// + /// A value that contains the information set in the access token. The structure of this buffer depends on the type of + /// information specified by the TokenInformationClass parameter. + /// + /// + /// If the function succeeds, the function returns nonzero. + /// If the function fails, it returns zero. To get extended error information, call GetLastError. + /// + /// + /// + /// To set privilege information, an application can call the AdjustTokenPrivileges function. To set a token's groups, an + /// application can call the AdjustTokenGroups function. + /// + /// Token-type information can be set only when an access token is created. + /// + // https://docs.microsoft.com/en-us/windows/desktop/api/securitybaseapi/nf-securitybaseapi-settokeninformation BOOL + // SetTokenInformation( HANDLE TokenHandle, TOKEN_INFORMATION_CLASS TokenInformationClass, LPVOID TokenInformation, DWORD + // TokenInformationLength ); + public bool SetInfo(T TokenInformation) => CorrespondingTypeAttribute.CanSet(out var ti) ? SetTokenInformation(this, ti, TokenInformation) : throw new InvalidCastException("Cannot get this type."); } /// Provides a for that is disposed using . diff --git a/PInvoke/Security/AdvApi32/WinNT.cs b/PInvoke/Security/AdvApi32/WinNT.cs index d10705b7..82fcaa0f 100644 --- a/PInvoke/Security/AdvApi32/WinNT.cs +++ b/PInvoke/Security/AdvApi32/WinNT.cs @@ -3786,8 +3786,9 @@ namespace Vanara.PInvoke } /// The TOKEN_GROUPS structure contains information about the group security identifiers (SIDs) in an access token. + [VanaraMarshaler(typeof(SafeAnysizeStructMarshaler), nameof(GroupCount))] [StructLayout(LayoutKind.Sequential)] - public struct TOKEN_GROUPS : IVanaraMarshaler + public struct TOKEN_GROUPS { /// Specifies the number of groups in the access token. public uint GroupCount; @@ -3819,10 +3820,6 @@ namespace Vanara.PInvoke { Groups[0] = new SID_AND_ATTRIBUTES(sid, attributes); } - - SizeT IVanaraMarshaler.GetNativeSize() => Marshal.SizeOf(GetType()); - SafeAllocatedMemoryHandle IVanaraMarshaler.MarshalManagedToNative(object obj) => new SafeAnysizeStruct((TOKEN_GROUPS)obj, nameof(GroupCount)); - object IVanaraMarshaler.MarshalNativeToManaged(IntPtr p, SizeT s) => p != IntPtr.Zero ? new SafeAnysizeStruct(p, s, nameof(GroupCount)).Value : default; } /// @@ -3973,7 +3970,8 @@ namespace Vanara.PInvoke // PrivilegeCount; LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY]; } TOKEN_PRIVILEGES, *PTOKEN_PRIVILEGES; [PInvokeData("winnt.h", MSDNShortId = "c9016511-740f-44f3-92ed-17cc518c6612")] [StructLayout(LayoutKind.Sequential)] - public struct TOKEN_PRIVILEGES : IVanaraMarshaler + [VanaraMarshaler(typeof(SafeAnysizeStructMarshaler), nameof(PrivilegeCount))] + public struct TOKEN_PRIVILEGES { /// This must be set to the number of entries in the Privileges array. public uint PrivilegeCount; @@ -4034,10 +4032,6 @@ namespace Vanara.PInvoke PrivilegeCount = (uint)(values?.Length ?? 0); Privileges = (LUID_AND_ATTRIBUTES[])values?.Clone() ?? new LUID_AND_ATTRIBUTES[0]; } - - SizeT IVanaraMarshaler.GetNativeSize() => Marshal.SizeOf(GetType()); - SafeAllocatedMemoryHandle IVanaraMarshaler.MarshalManagedToNative(object obj) => new SafeAnysizeStruct((TOKEN_PRIVILEGES)obj, nameof(PrivilegeCount)); - object IVanaraMarshaler.MarshalNativeToManaged(IntPtr p, SizeT s) => p != IntPtr.Zero ? new SafeAnysizeStruct(p, s, nameof(PrivilegeCount)).Value : default; } /// The TOKEN_SOURCE structure identifies the source of an access token. diff --git a/PInvoke/Shared/Wdm.cs b/PInvoke/Shared/Wdm.cs index a06c5090..88f485a6 100644 --- a/PInvoke/Shared/Wdm.cs +++ b/PInvoke/Shared/Wdm.cs @@ -956,8 +956,9 @@ namespace Vanara.PInvoke // _CM_PARTIAL_RESOURCE_LIST { USHORT Version; USHORT Revision; ULONG Count; CM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptors[1]; } // CM_PARTIAL_RESOURCE_LIST, *PCM_PARTIAL_RESOURCE_LIST; [PInvokeData("wdm.h", MSDNShortId = "f16b26f5-1f32-4c2e-83ec-0a0f79a4be85")] + [VanaraMarshaler(typeof(SafeAnysizeStructMarshaler), nameof(Count))] [StructLayout(LayoutKind.Sequential)] - public struct CM_PARTIAL_RESOURCE_LIST : IVanaraMarshaler + public struct CM_PARTIAL_RESOURCE_LIST { /// The version number of this structure. This value should be 1. public ushort Version; @@ -971,10 +972,6 @@ namespace Vanara.PInvoke /// The first element in an array of one or more CM_PARTIAL_RESOURCE_DESCRIPTOR structures. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] public CM_PARTIAL_RESOURCE_DESCRIPTOR[] PartialDescriptors; - - SizeT IVanaraMarshaler.GetNativeSize() => Marshal.SizeOf(GetType()); - SafeAllocatedMemoryHandle IVanaraMarshaler.MarshalManagedToNative(object obj) => new SafeAnysizeStruct((CM_PARTIAL_RESOURCE_LIST)obj, nameof(Count)); - object IVanaraMarshaler.MarshalNativeToManaged(IntPtr p, SizeT s) => p != IntPtr.Zero ? new SafeAnysizeStruct(p, s, nameof(Count)).Value : default; } /// The CM_RESOURCE_LIST structure specifies all of the system hardware resources assigned to a device. @@ -1017,8 +1014,9 @@ namespace Vanara.PInvoke // https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/wdm/ns-wdm-_cm_resource_list typedef struct _CM_RESOURCE_LIST { // ULONG Count; CM_FULL_RESOURCE_DESCRIPTOR List[1]; } CM_RESOURCE_LIST, *PCM_RESOURCE_LIST; [PInvokeData("wdm.h", MSDNShortId = "01f31255-a4f7-4a16-9238-a7391bb850d1")] + [VanaraMarshaler(typeof(SafeAnysizeStructMarshaler), nameof(Count))] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] - public struct CM_RESOURCE_LIST : IVanaraMarshaler + public struct CM_RESOURCE_LIST { /// /// The number of full resource descriptors that are specified by this CM_RESOURCE_LIST structure. The List member is @@ -1034,9 +1032,5 @@ namespace Vanara.PInvoke /// [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] public CM_FULL_RESOURCE_DESCRIPTOR[] List; - - SizeT IVanaraMarshaler.GetNativeSize() => Marshal.SizeOf(GetType()); - SafeAllocatedMemoryHandle IVanaraMarshaler.MarshalManagedToNative(object obj) => new SafeAnysizeStruct((CM_RESOURCE_LIST)obj, nameof(Count)); - object IVanaraMarshaler.MarshalNativeToManaged(IntPtr p, SizeT s) => p != IntPtr.Zero ? new SafeAnysizeStruct(p, s, nameof(Count)).Value : default; } } \ No newline at end of file diff --git a/UnitTests/PInvoke/Security/AdvApi32/SecurityBaseApiTests.cs b/UnitTests/PInvoke/Security/AdvApi32/SecurityBaseApiTests.cs index 63502c24..88cc98e3 100644 --- a/UnitTests/PInvoke/Security/AdvApi32/SecurityBaseApiTests.cs +++ b/UnitTests/PInvoke/Security/AdvApi32/SecurityBaseApiTests.cs @@ -534,7 +534,7 @@ namespace Vanara.PInvoke.Tests { Assert.That(t, Is.Not.Null); - var p = t.GetInfo(TOKEN_INFORMATION_CLASS.TokenPrivileges); + var p = t.GetInfo(); Assert.That(p, Is.Not.Null); Assert.That(p.PrivilegeCount, Is.GreaterThan(0)); TestContext.WriteLine("Privs: " + string.Join("; ", p.Privileges.Select(i => i.ToString())));