Extended StructureToPtr so that it works with `enum` values.

pull/60/head
David Hall 2019-06-17 13:44:46 -06:00
parent 36c5b82f91
commit badc08c7e5
1 changed files with 3 additions and 2 deletions

View File

@ -306,9 +306,10 @@ namespace Vanara.Extensions
/// <returns>A pointer to the memory allocated by <paramref name="memAlloc"/>.</returns>
public static IntPtr StructureToPtr<T>(this T value, Func<int, IntPtr> 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;
}