From badc08c7e59e039bda2fe6abf8d2c53ad5ba8575 Mon Sep 17 00:00:00 2001 From: David Hall Date: Mon, 17 Jun 2019 13:44:46 -0600 Subject: [PATCH] Extended StructureToPtr so that it works with `enum` values. --- Core/Extensions/InteropExtensions.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Core/Extensions/InteropExtensions.cs b/Core/Extensions/InteropExtensions.cs index 64cc3ccf..3b6b0b47 100644 --- a/Core/Extensions/InteropExtensions.cs +++ b/Core/Extensions/InteropExtensions.cs @@ -306,9 +306,10 @@ namespace Vanara.Extensions /// A pointer to the memory allocated by . public static IntPtr StructureToPtr(this T value, Func memAlloc, out int bytesAllocated) { - bytesAllocated = Marshal.SizeOf(value); + var ttype = typeof(T).IsEnum ? Enum.GetUnderlyingType(typeof(T)) : typeof(T); + bytesAllocated = Marshal.SizeOf(ttype); var ret = memAlloc(bytesAllocated); - Marshal.StructureToPtr(value, ret, false); + Marshal.StructureToPtr(Convert.ChangeType(value, ttype), ret, false); return ret; }