Converted structures from using IVanaraMarshaler to using SafeAnysizeStructMarshaler attribute

pull/83/head
David Hall 2019-11-16 15:51:16 -07:00
parent 9d02afc6e1
commit 9771eadebc
7 changed files with 46 additions and 49 deletions

View File

@ -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<GLYPHSET>), nameof(cRanges))]
[StructLayout(LayoutKind.Sequential)]
public struct GLYPHSET : IVanaraMarshaler
public struct GLYPHSET
{
/// <summary>The size, in bytes, of this structure.</summary>
public uint cbThis;
@ -4871,10 +4872,6 @@ namespace Vanara.PInvoke
/// <summary>Array of Unicode ranges that are supported in the font.</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public WCRANGE[] ranges;
SizeT IVanaraMarshaler.GetNativeSize() => 20;
SafeAllocatedMemoryHandle IVanaraMarshaler.MarshalManagedToNative(object managedObject) => new SafeAnysizeStruct<GLYPHSET>((GLYPHSET)managedObject, nameof(cRanges));
object IVanaraMarshaler.MarshalNativeToManaged(IntPtr p, SizeT s) => p != IntPtr.Zero ? new SafeAnysizeStruct<GLYPHSET>(p, s, nameof(cRanges)).Value : default;
}
/// <summary>The <c>KERNINGPAIR</c> structure defines a kerning pair.</summary>

View File

@ -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<DRIVE_LAYOUT_INFORMATION>), nameof(PartitionCount))]
[StructLayout(LayoutKind.Sequential)]
public struct DRIVE_LAYOUT_INFORMATION : IVanaraMarshaler
public struct DRIVE_LAYOUT_INFORMATION
{
/// <summary>
/// <para>The number of partitions on a drive.</para>
@ -1059,10 +1060,6 @@ namespace Vanara.PInvoke
/// <summary>A variable-sized array of PARTITION_INFORMATION structures, one structure for each partition on a drive.</summary>
[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>((DRIVE_LAYOUT_INFORMATION)obj, nameof(PartitionCount));
object IVanaraMarshaler.MarshalNativeToManaged(IntPtr p, SizeT s) => p != IntPtr.Zero ? new SafeAnysizeStruct<DRIVE_LAYOUT_INFORMATION>(p, s, nameof(PartitionCount)).Value : default;
}
/// <summary>Contains extended information about a drive's partitions.</summary>
@ -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<DRIVE_LAYOUT_INFORMATION_EX>), nameof(PartitionCount))]
[StructLayout(LayoutKind.Explicit)]
public struct DRIVE_LAYOUT_INFORMATION_EX : IVanaraMarshaler
public struct DRIVE_LAYOUT_INFORMATION_EX
{
/// <summary>
/// <para>The style of the partitions on the drive enumerated by the PARTITION_STYLE enumeration.</para>
@ -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>((DRIVE_LAYOUT_INFORMATION_EX)obj, nameof(PartitionCount));
object IVanaraMarshaler.MarshalNativeToManaged(IntPtr p, SizeT s) => p != IntPtr.Zero ? new SafeAnysizeStruct<DRIVE_LAYOUT_INFORMATION_EX>(p, s, nameof(PartitionCount)).Value : default;
}
/// <summary>Contains information about a drive's GUID partition table (GPT) partitions.</summary>
@ -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<USN_RECORD_V4>), nameof(NumberOfExtents))]
[StructLayout(LayoutKind.Sequential, Size = 80)]
public struct USN_RECORD_V4 : IVanaraMarshaler
public struct USN_RECORD_V4
{
/// <summary>
/// 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
/// <summary>An array of USN_RECORD_EXTENT structures that represent the extents in the <c>USN_RECORD_V4</c> entry.</summary>
[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>((USN_RECORD_V4)obj, nameof(NumberOfExtents));
object IVanaraMarshaler.MarshalNativeToManaged(IntPtr p, SizeT s) => p != IntPtr.Zero ? new SafeAnysizeStruct<USN_RECORD_V4>(p, s, nameof(NumberOfExtents)).Value : default;
}
/// <summary>

View File

@ -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<PDH_RAW_LOG_RECORD>), nameof(dwItems))]
[StructLayout(LayoutKind.Sequential)]
public struct PDH_RAW_LOG_RECORD : IVanaraMarshaler
public struct PDH_RAW_LOG_RECORD
{
/// <summary>
/// Size of this structure, in bytes. The size includes this structure and the <c>RawBytes</c> appended to the end of this structure.
@ -5134,10 +5135,6 @@ namespace Vanara.PInvoke
/// <summary>Binary record.</summary>
[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>((PDH_RAW_LOG_RECORD)obj, nameof(dwItems));
object IVanaraMarshaler.MarshalNativeToManaged(IntPtr p, SizeT s) => p != IntPtr.Zero ? new SafeAnysizeStruct<PDH_RAW_LOG_RECORD>(p, s, nameof(dwItems)).Value : default;
}
/// <summary>

View File

@ -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.
/// </param>
public T GetInfo<T>(TOKEN_INFORMATION_CLASS tokenInfoClass)
public T GetInfo<T>(TOKEN_INFORMATION_CLASS? tokenInfoClass = null)
{
if (!CorrespondingTypeAttribute.CanGet(tokenInfoClass, typeof(T)))
if (!CorrespondingTypeAttribute.CanGet<T, TOKEN_INFORMATION_CLASS>(tokenInfoClass, out var ti))
throw new InvalidCastException();
using var pType = GetInfo(tokenInfoClass);
using var pType = GetInfo(ti);
return ((IntPtr)pType).Convert<T>(pType.Size);
}
@ -6377,6 +6377,30 @@ namespace Vanara.PInvoke
// SetTokenInformation( HANDLE TokenHandle, TOKEN_INFORMATION_CLASS TokenInformationClass, LPVOID TokenInformation, DWORD
// TokenInformationLength );
public bool SetInfo<T>(TOKEN_INFORMATION_CLASS TokenInformationClass, T TokenInformation) => SetTokenInformation(this, TokenInformationClass, TokenInformation);
/// <summary>
/// The <c>SetInfo</c> 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.
/// </summary>
/// <param name="TokenInformation">
/// 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.
/// </param>
/// <returns>
/// <para>If the function succeeds, the function returns nonzero.</para>
/// <para>If the function fails, it returns zero. To get extended error information, call GetLastError.</para>
/// </returns>
/// <remarks>
/// <para>
/// To set privilege information, an application can call the AdjustTokenPrivileges function. To set a token's groups, an
/// application can call the AdjustTokenGroups function.
/// </para>
/// <para>Token-type information can be set only when an access token is created.</para>
/// </remarks>
// 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>(T TokenInformation) => CorrespondingTypeAttribute.CanSet<T, TOKEN_INFORMATION_CLASS>(out var ti) ? SetTokenInformation(this, ti, TokenInformation) : throw new InvalidCastException("Cannot get this type.");
}
/// <summary>Provides a <see cref="SafeHandle"/> for <see cref="SECURITY_DESCRIPTOR"/> that is disposed using <see cref="DestroyPrivateObjectSecurity"/>.</summary>

View File

@ -3786,8 +3786,9 @@ namespace Vanara.PInvoke
}
/// <summary>The TOKEN_GROUPS structure contains information about the group security identifiers (SIDs) in an access token.</summary>
[VanaraMarshaler(typeof(SafeAnysizeStructMarshaler<TOKEN_GROUPS>), nameof(GroupCount))]
[StructLayout(LayoutKind.Sequential)]
public struct TOKEN_GROUPS : IVanaraMarshaler
public struct TOKEN_GROUPS
{
/// <summary>Specifies the number of groups in the access token.</summary>
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>((TOKEN_GROUPS)obj, nameof(GroupCount));
object IVanaraMarshaler.MarshalNativeToManaged(IntPtr p, SizeT s) => p != IntPtr.Zero ? new SafeAnysizeStruct<TOKEN_GROUPS>(p, s, nameof(GroupCount)).Value : default;
}
/// <summary>
@ -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<TOKEN_PRIVILEGES>), nameof(PrivilegeCount))]
public struct TOKEN_PRIVILEGES
{
/// <summary>This must be set to the number of entries in the <c>Privileges</c> array.</summary>
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>((TOKEN_PRIVILEGES)obj, nameof(PrivilegeCount));
object IVanaraMarshaler.MarshalNativeToManaged(IntPtr p, SizeT s) => p != IntPtr.Zero ? new SafeAnysizeStruct<TOKEN_PRIVILEGES>(p, s, nameof(PrivilegeCount)).Value : default;
}
/// <summary>The TOKEN_SOURCE structure identifies the source of an access token.</summary>

View File

@ -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<CM_PARTIAL_RESOURCE_LIST>), nameof(Count))]
[StructLayout(LayoutKind.Sequential)]
public struct CM_PARTIAL_RESOURCE_LIST : IVanaraMarshaler
public struct CM_PARTIAL_RESOURCE_LIST
{
/// <summary>The version number of this structure. This value should be 1.</summary>
public ushort Version;
@ -971,10 +972,6 @@ namespace Vanara.PInvoke
/// <summary>The first element in an array of one or more CM_PARTIAL_RESOURCE_DESCRIPTOR structures.</summary>
[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>((CM_PARTIAL_RESOURCE_LIST)obj, nameof(Count));
object IVanaraMarshaler.MarshalNativeToManaged(IntPtr p, SizeT s) => p != IntPtr.Zero ? new SafeAnysizeStruct<CM_PARTIAL_RESOURCE_LIST>(p, s, nameof(Count)).Value : default;
}
/// <summary>The <c>CM_RESOURCE_LIST</c> structure specifies all of the system hardware resources assigned to a device.</summary>
@ -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<CM_RESOURCE_LIST>), nameof(Count))]
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct CM_RESOURCE_LIST : IVanaraMarshaler
public struct CM_RESOURCE_LIST
{
/// <summary>
/// The number of full resource descriptors that are specified by this <c>CM_RESOURCE_LIST</c> structure. The <c>List</c> member is
@ -1034,9 +1032,5 @@ namespace Vanara.PInvoke
/// </summary>
[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>((CM_RESOURCE_LIST)obj, nameof(Count));
object IVanaraMarshaler.MarshalNativeToManaged(IntPtr p, SizeT s) => p != IntPtr.Zero ? new SafeAnysizeStruct<CM_RESOURCE_LIST>(p, s, nameof(Count)).Value : default;
}
}

View File

@ -534,7 +534,7 @@ namespace Vanara.PInvoke.Tests
{
Assert.That(t, Is.Not.Null);
var p = t.GetInfo<TOKEN_PRIVILEGES>(TOKEN_INFORMATION_CLASS.TokenPrivileges);
var p = t.GetInfo<TOKEN_PRIVILEGES>();
Assert.That(p, Is.Not.Null);
Assert.That(p.PrivilegeCount, Is.GreaterThan(0));
TestContext.WriteLine("Privs: " + string.Join("; ", p.Privileges.Select(i => i.ToString())));