P/Invoke Interop Assistant工具C到C#自动转换

C#互操作的类型基本位于System.Runtime.InteropServices命名空间下,本系列随笔主要记录本人在开发过程中使用的到一些类型函数、技巧及工具

计算类型的大小

int size = sizeof(GLYCOUNTER);//只能作用于unsafe代码上下文

获取类型实例的大小

Marshal.SizeOf(counters)

结构体与指针类型互相转换

 //指针->结构体
 GLYCOUNTERS counters = (GLYCOUNTERS)Marshal.PtrToStructure(glyParam.lpBuffer, typeof(GLYCOUNTERS));
 //结构体->指针
 IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(counters));
 Marshal.StructureToPtr(counters,p, false);

C/C++ 数据结构转C#结构体工具

> 工具源码及下载地址:[http://clrinterop.codeplex.com/releases/view/17579](http://clrinterop.codeplex.com/releases/view/17579)
原文地址:https://www.cnblogs.com/liunlls/p/PInvoke_2017_02_09.html