From 73792290f634d9db3bc19927e366881cfc1071a6 Mon Sep 17 00:00:00 2001 From: David Hall Date: Mon, 8 Apr 2019 19:44:08 -0600 Subject: [PATCH] Broke down and finally used an unsafe struct for this. --- PInvoke/Shared/WinNT/OBJECT_TYPE_LIST.cs | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/PInvoke/Shared/WinNT/OBJECT_TYPE_LIST.cs b/PInvoke/Shared/WinNT/OBJECT_TYPE_LIST.cs index 6e7a039c..e04c7069 100644 --- a/PInvoke/Shared/WinNT/OBJECT_TYPE_LIST.cs +++ b/PInvoke/Shared/WinNT/OBJECT_TYPE_LIST.cs @@ -28,7 +28,7 @@ namespace Vanara.PInvoke // typedef struct _OBJECT_TYPE_LIST { WORD Level; WORD Sbz; GUID *ObjectType; } OBJECT_TYPE_LIST, *POBJECT_TYPE_LIST; [PInvokeData("winnt.h", MSDNShortId = "c729ff1a-65f3-4f6f-84dd-5700aead75ce")] [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 2)] - public partial class OBJECT_TYPE_LIST : IDisposable + public unsafe struct OBJECT_TYPE_LIST { /// /// Specifies the level of the object type in the hierarchy of an object and its subobjects. Level zero indicates the object itself. @@ -41,39 +41,19 @@ namespace Vanara.PInvoke public ushort Sbz; /// A pointer to the GUID for the object or subobject. - public IntPtr guidObjectType; + public Guid* guidObjectType; /// Initializes a new instance of the struct. /// The level of the object type in the hierarchy of an object and its subobjects. /// The object or subobject identifier. - public OBJECT_TYPE_LIST(ObjectTypeListLevel level, Guid objType) + public OBJECT_TYPE_LIST(ObjectTypeListLevel level, Guid objType = default) { Sbz = 0; this.level = level; - ObjectId = objType; + guidObjectType = objType == default ? null : &objType; } /// Represents an object that is itself. - public static readonly OBJECT_TYPE_LIST Self = new OBJECT_TYPE_LIST(0, Guid.Empty); - - /// The GUID for the object or subobject - public Guid ObjectId - { - get => guidObjectType == IntPtr.Zero ? Guid.Empty : (Guid)Marshal.PtrToStructure(guidObjectType, typeof(Guid)); - set - { - ((IDisposable)this).Dispose(); - guidObjectType = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(Guid))); - Marshal.StructureToPtr(value, guidObjectType, true); - } - } - - /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. - void IDisposable.Dispose() - { - if (guidObjectType != IntPtr.Zero) - Marshal.FreeCoTaskMem(guidObjectType); - guidObjectType = IntPtr.Zero; - } + public static readonly OBJECT_TYPE_LIST Self = new OBJECT_TYPE_LIST(); } } \ No newline at end of file