diff --git a/PInvoke/Gdi32/WinGdi.FontText.cs b/PInvoke/Gdi32/WinGdi.FontText.cs index 58b43f45..37d09a80 100644 --- a/PInvoke/Gdi32/WinGdi.FontText.cs +++ b/PInvoke/Gdi32/WinGdi.FontText.cs @@ -4842,7 +4842,7 @@ namespace Vanara.PInvoke // flAccel; DWORD cGlyphsSupported; DWORD cRanges; WCRANGE ranges[1]; } GLYPHSET, *PGLYPHSET, *LPGLYPHSET; [PInvokeData("wingdi.h", MSDNShortId = "b8ac8d3f-b062-491c-966f-02f3d4c11419")] [StructLayout(LayoutKind.Sequential)] - public struct GLYPHSET : IMarshalDirective + public struct GLYPHSET : IVanaraMarshaler { /// The size, in bytes, of this structure. public uint cbThis; @@ -4872,8 +4872,9 @@ namespace Vanara.PInvoke [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] public WCRANGE[] ranges; - MarshalDirectiveActivator IMarshalDirective.GetActivator() => (p, s) => p != IntPtr.Zero ? new SafeAnysizeStruct(p, s, nameof(cRanges)).Value : default; - SafeAllocatedMemoryHandle IMarshalDirective.ToNative() => new SafeAnysizeStruct(this, nameof(cRanges)); + 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 7040e946..de8eac99 100644 --- a/PInvoke/Kernel32/WinIOCtl.Structs.cs +++ b/PInvoke/Kernel32/WinIOCtl.Structs.cs @@ -1042,7 +1042,7 @@ namespace Vanara.PInvoke // DRIVE_LAYOUT_INFORMATION, *PDRIVE_LAYOUT_INFORMATION; [PInvokeData("winioctl.h", MSDNShortId = "e67ccaa7-a735-4695-8385-28f57b41821c")] [StructLayout(LayoutKind.Sequential)] - public struct DRIVE_LAYOUT_INFORMATION : IMarshalDirective + public struct DRIVE_LAYOUT_INFORMATION : IVanaraMarshaler { /// /// The number of partitions on a drive. @@ -1060,8 +1060,9 @@ namespace Vanara.PInvoke [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] public PARTITION_INFORMATION[] PartitionEntry; - MarshalDirectiveActivator IMarshalDirective.GetActivator() => (p, s) => p != IntPtr.Zero ? new SafeAnysizeStruct(p, s, nameof(PartitionCount)).Value : default; - SafeAllocatedMemoryHandle IMarshalDirective.ToNative() => new SafeAnysizeStruct(this, nameof(PartitionCount)); + 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,7 +1071,7 @@ namespace Vanara.PInvoke // 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")] [StructLayout(LayoutKind.Explicit)] - public struct DRIVE_LAYOUT_INFORMATION_EX : IMarshalDirective + public struct DRIVE_LAYOUT_INFORMATION_EX : IVanaraMarshaler { /// /// The style of the partitions on the drive enumerated by the PARTITION_STYLE enumeration. @@ -1122,8 +1123,9 @@ namespace Vanara.PInvoke [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] public PARTITION_INFORMATION_EX[] PartitionEntry; - MarshalDirectiveActivator IMarshalDirective.GetActivator() => (p, s) => p != IntPtr.Zero ? new SafeAnysizeStruct(p, s, nameof(PartitionCount)).Value : default; - SafeAllocatedMemoryHandle IMarshalDirective.ToNative() => new SafeAnysizeStruct(this, nameof(PartitionCount)); + 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. @@ -3882,7 +3884,7 @@ namespace Vanara.PInvoke // 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")] [StructLayout(LayoutKind.Sequential, Size = 80)] - public struct USN_RECORD_V4 : IMarshalDirective + public struct USN_RECORD_V4 : IVanaraMarshaler { /// /// A USN_RECORD_COMMON_HEADER structure that describes the record length, major version, and minor version for the record. @@ -4103,8 +4105,9 @@ namespace Vanara.PInvoke [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] public USN_RECORD_EXTENT[] Extents; - MarshalDirectiveActivator IMarshalDirective.GetActivator() => (p, s) => p != IntPtr.Zero ? new SafeAnysizeStruct(p, s, nameof(NumberOfExtents)).Value : default; - SafeAllocatedMemoryHandle IMarshalDirective.ToNative() => new SafeAnysizeStruct(this, nameof(NumberOfExtents)); + 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 68921746..24ed25a1 100644 --- a/PInvoke/Pdh/Pdh.cs +++ b/PInvoke/Pdh/Pdh.cs @@ -5090,7 +5090,7 @@ namespace Vanara.PInvoke // 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")] [StructLayout(LayoutKind.Sequential)] - public struct PDH_RAW_LOG_RECORD : IMarshalDirective + public struct PDH_RAW_LOG_RECORD : IVanaraMarshaler { /// /// Size of this structure, in bytes. The size includes this structure and the RawBytes appended to the end of this structure. @@ -5135,8 +5135,9 @@ namespace Vanara.PInvoke [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] public byte[] RawBytes; - MarshalDirectiveActivator IMarshalDirective.GetActivator() => (p, s) => p != IntPtr.Zero ? new SafeAnysizeStruct(p, s, nameof(dwItems)).Value : default; - SafeAllocatedMemoryHandle IMarshalDirective.ToNative() => new SafeAnysizeStruct(this, nameof(dwItems)); + 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/NTSecApi.cs b/PInvoke/Security/AdvApi32/NTSecApi.cs index a229d422..f8b2fdbc 100644 --- a/PInvoke/Security/AdvApi32/NTSecApi.cs +++ b/PInvoke/Security/AdvApi32/NTSecApi.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Runtime.InteropServices; using Vanara.InteropServices; using FILETIME = System.Runtime.InteropServices.ComTypes.FILETIME; @@ -48,6 +49,36 @@ namespace Vanara.PInvoke DOMAIN_NO_LM_OWF_CHANGE = 0x00000040 } + /// Auditing options for an audit event type. + [PInvokeData("ntsecapi.h")] + [Flags] + public enum POLICY_AUDIT_EVENT_OPTIONS : uint + { + /// + /// Do not change auditing options for the specified event type. + /// This value is valid for the AuditSetSystemPolicy and AuditQuerySystemPolicy functions. + /// + POLICY_AUDIT_EVENT_UNCHANGED = 0x00000000, + + /// + /// Audit successful occurrences of the specified event type. + /// This value is valid for the AuditSetSystemPolicy and AuditQuerySystemPolicy functions. + /// + POLICY_AUDIT_EVENT_SUCCESS = 0x00000001, + + /// + /// Audit failed attempts to cause the specified event type. + /// This value is valid for the AuditSetSystemPolicy and AuditQuerySystemPolicy functions. + /// + POLICY_AUDIT_EVENT_FAILURE = 0x00000002, + + /// + /// Do not audit the specified event type. + /// This value is valid for the AuditSetSystemPolicy and AuditQuerySystemPolicy functions. + /// + POLICY_AUDIT_EVENT_NONE = 0x00000004, + } + /// The POLICY_DOMAIN_INFORMATION_CLASS enumeration defines the type of policy domain information. // https://docs.microsoft.com/en-us/windows/desktop/api/ntsecapi/ne-ntsecapi-_policy_domain_information_class typedef enum // _POLICY_DOMAIN_INFORMATION_CLASS { PolicyDomainQualityOfServiceInformation, PolicyDomainEfsInformation, @@ -515,7 +546,7 @@ namespace Vanara.PInvoke // } POLICY_AUDIT_EVENTS_INFO, *PPOLICY_AUDIT_EVENTS_INFO; [PInvokeData("ntsecapi.h", MSDNShortId = "3442e5e5-78cf-4bda-ba11-0f51ee40df16")] [StructLayout(LayoutKind.Sequential)] - public struct POLICY_AUDIT_EVENTS_INFO + public struct POLICY_AUDIT_EVENTS_INFO : IVanaraMarshaler { /// /// Indicates whether auditing is enabled. @@ -528,8 +559,7 @@ namespace Vanara.PInvoke /// auditing options as specified in the EventAuditingOptions member even when AuditingMode is FALSE. /// /// - [MarshalAs(UnmanagedType.U1)] - public bool AuditingMode; + public BOOLEAN AuditingMode; /// /// @@ -572,14 +602,44 @@ namespace Vanara.PInvoke /// /// /// - public IntPtr EventAuditingOptions; + public POLICY_AUDIT_EVENT_OPTIONS[] EventAuditingOptions; /// /// Specifies the number of elements in the EventAuditingOptions array. For set operations, if this value is less than the /// number of audit event types supported by the system, the system does not change the auditing options for event types with /// indexes equal to or higher than the value specified in MaximumAuditEventCount. /// - public uint MaximumAuditEventCount; + public int MaximumAuditEventCount => EventAuditingOptions?.Length ?? 0; + + SizeT IVanaraMarshaler.GetNativeSize() => 8 + IntPtr.Size; + + SafeAllocatedMemoryHandle IVanaraMarshaler.MarshalManagedToNative(object obj) + { + var i = (POLICY_AUDIT_EVENTS_INFO)obj; + var mem = new SafeHGlobalHandle(64); + using (var ret = new NativeMemoryStream(mem)) + { + ret.Write(i.AuditingMode); + ret.Position = 4; + ret.Write(i.EventAuditingOptions, true); + ret.Write(i.MaximumAuditEventCount); + } + return mem; + } + + object IVanaraMarshaler.MarshalNativeToManaged(IntPtr ptr, SizeT size) + { + using var str = new NativeMemoryStream(ptr, size); + var mode = str.Read(); + str.Position = 4 + IntPtr.Size; + var cnt = str.Read(); + str.Position = 4; + return new POLICY_AUDIT_EVENTS_INFO + { + AuditingMode = mode, + EventAuditingOptions = str.ReadArray(cnt, true).ToArray() + }; + } } /// diff --git a/PInvoke/Security/AdvApi32/WinNT.cs b/PInvoke/Security/AdvApi32/WinNT.cs index a3098bb2..d10705b7 100644 --- a/PInvoke/Security/AdvApi32/WinNT.cs +++ b/PInvoke/Security/AdvApi32/WinNT.cs @@ -3787,7 +3787,7 @@ namespace Vanara.PInvoke /// The TOKEN_GROUPS structure contains information about the group security identifiers (SIDs) in an access token. [StructLayout(LayoutKind.Sequential)] - public struct TOKEN_GROUPS : IMarshalDirective + public struct TOKEN_GROUPS : IVanaraMarshaler { /// Specifies the number of groups in the access token. public uint GroupCount; @@ -3820,8 +3820,9 @@ namespace Vanara.PInvoke Groups[0] = new SID_AND_ATTRIBUTES(sid, attributes); } - MarshalDirectiveActivator IMarshalDirective.GetActivator() => (p, s) => p != IntPtr.Zero ? new SafeAnysizeStruct(p, s, nameof(GroupCount)).Value : default; - SafeAllocatedMemoryHandle IMarshalDirective.ToNative() => new SafeAnysizeStruct(this); + 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; } /// @@ -3972,7 +3973,7 @@ 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 : IMarshalDirective + public struct TOKEN_PRIVILEGES : IVanaraMarshaler { /// This must be set to the number of entries in the Privileges array. public uint PrivilegeCount; @@ -4034,8 +4035,9 @@ namespace Vanara.PInvoke Privileges = (LUID_AND_ATTRIBUTES[])values?.Clone() ?? new LUID_AND_ATTRIBUTES[0]; } - MarshalDirectiveActivator IMarshalDirective.GetActivator() => (p, s) => p != IntPtr.Zero ? new SafeAnysizeStruct(p, s, nameof(PrivilegeCount)).Value : default; - SafeAllocatedMemoryHandle IMarshalDirective.ToNative() => new SafeAnysizeStruct(this); + 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 c67ccf04..a06c5090 100644 --- a/PInvoke/Shared/Wdm.cs +++ b/PInvoke/Shared/Wdm.cs @@ -957,7 +957,7 @@ namespace Vanara.PInvoke // CM_PARTIAL_RESOURCE_LIST, *PCM_PARTIAL_RESOURCE_LIST; [PInvokeData("wdm.h", MSDNShortId = "f16b26f5-1f32-4c2e-83ec-0a0f79a4be85")] [StructLayout(LayoutKind.Sequential)] - public struct CM_PARTIAL_RESOURCE_LIST : IMarshalDirective + public struct CM_PARTIAL_RESOURCE_LIST : IVanaraMarshaler { /// The version number of this structure. This value should be 1. public ushort Version; @@ -972,8 +972,9 @@ namespace Vanara.PInvoke [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] public CM_PARTIAL_RESOURCE_DESCRIPTOR[] PartialDescriptors; - MarshalDirectiveActivator IMarshalDirective.GetActivator() => (p, s) => p != IntPtr.Zero ? new SafeAnysizeStruct(p, s, nameof(Count)).Value : default; - SafeAllocatedMemoryHandle IMarshalDirective.ToNative() => new SafeAnysizeStruct(this, nameof(Count)); + 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,7 +1018,7 @@ namespace Vanara.PInvoke // ULONG Count; CM_FULL_RESOURCE_DESCRIPTOR List[1]; } CM_RESOURCE_LIST, *PCM_RESOURCE_LIST; [PInvokeData("wdm.h", MSDNShortId = "01f31255-a4f7-4a16-9238-a7391bb850d1")] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] - public struct CM_RESOURCE_LIST : IMarshalDirective + public struct CM_RESOURCE_LIST : IVanaraMarshaler { /// /// The number of full resource descriptors that are specified by this CM_RESOURCE_LIST structure. The List member is @@ -1034,7 +1035,8 @@ namespace Vanara.PInvoke [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] public CM_FULL_RESOURCE_DESCRIPTOR[] List; - MarshalDirectiveActivator IMarshalDirective.GetActivator() => (p, s) => p != IntPtr.Zero ? new SafeAnysizeStruct(p, s, nameof(Count)).Value : default; - SafeAllocatedMemoryHandle IMarshalDirective.ToNative() => new SafeAnysizeStruct(this, nameof(Count)); + 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