结构和托管指针转换
原创2023年6月8日小于 1 分钟
using System.Runtime.InteropServices;
public static IntPtr StructToIntPtr<T>(T info) where T : struct
{
int size = Marshal.SizeOf(info);
IntPtr intPtr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(info, intPtr, false);
return intPtr;
}
public static T IntptrToStruct<T>(IntPtr intPtr) where T : struct
{
return (T)Marshal.PtrToStructure(intPtr, typeof(T));
}