一般写c#代码基本用不到 相互转换 只有调用c++中的dll动态库的时候才用的到

struct转intptr

public static IntPtr StructToIntPtr<T>(T req) where T : struct
{
int size = Marshal.SizeOf(req);
byte[] bytes = new byte[size];
IntPtr structPtr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(req, structPtr, false);
Marshal.Copy(structPtr, bytes, , size);
return structPtr;
}

用完intptr记得释放申请的这块内存(Marshal.FreeHGlobal(IntPtr);)

Intptr转struct

public static T IntPtrToStruct<T>(IntPtr ptr) where T : struct
{
object t = Marshal.PtrToStructure(ptr, typeof(T));
Marshal.FreeHGlobal(ptr);
if (t is T t1)
{
return t1;
}
else
{
return default(T);
}
}

IntPtr、Struct 相互转换的更多相关文章

  1. C# byte[]、struct、intptr等的相互转换

    1.struct byte[]互相转换 //struct转换为byte[] public static byte[] StructToBytes(object structObj) { int siz ...

  2. [转载]C#中int和IntPtr相互转换

    方法一. int转IntPtr int i = 12;           IntPtr p = new IntPtr(i); IntPtr转int int myi = (int)p;         ...

  3. (一)一个工作任务引起的乱战——c#中结构体与byte[]间相互转换

    一个工作任务涉及到c#与c++系统间的udp通信,处理了蛮长时间没有完成任务,但是期间接触到不少小知识点.本人是初接触c#,c++语言没有接触过.可能写的东西都很小儿科,暂且记录下来当工作日记把. 先 ...

  4. 如何让IntPtr指向一块内存,以及托管内存与非托管内存的相互转化

    IntPtr idp= IntPtr.Zero; StringBuilder idata = new StringBuilder("000000"); string idata = ...

  5. C# Struct结构体里数组长度的指定

    typedef struct Point{ unsigned short x; unsigned short y; }mPoint;//点坐标 typedef struct Line{ mPoint ...

  6. 关于C# byte[]与struct的转换

    转自:http://blog.chinaunix.net/uid-215617-id-2213082.html Some of the C# code I've been writing recent ...

  7. IntPtr与自定义结构互转

    //IntPtr转自定义结构 struct onlydata { IntPtr hwnd; }; onlydata pd=new onlydata(); IntPtr pd; pd=Marshal.P ...

  8. 转:struct sockaddr与struct sockaddr_in ,struct sockaddr_un的区别和联系

    在linux环境下,结构体struct sockaddr在/usr/include/linux/socket.h中定义,具体如下:typedef unsigned short sa_family_t; ...

  9. 使用Marshal.Copy把Txt行数据转为Struct类型值

    添加重要的命名空间: using System.Runtime.InteropServices; 先建立结构相同(char长度相同)的Struct类型用于转换: [StructLayout(Layou ...

随机推荐

  1. 简单利用XSS获取Cookie信息实例演示

    简单利用XSS获取Cookie信息实例演示   首先要找到一个有XXS的站,这里就不整什么大站了,谷歌一下inurl:'Product.asp?BigClassName',搜出来的命中率也比较高.随便 ...

  2. laravel 操作日志;

    在网上寻找了许多方法,觉得有的地方看不懂,  决定自己写一些关于laravel中调用本身中的操作日志: Laravel 日志工具在强大的 Monolog 函数库上提供一层简单的功能.Laravel 默 ...

  3. 17个你必须牢记的Win10快捷键

    电脑初学者掌握了盲打技术,可以提高录入速度:游戏玩家掌握了快捷键,可以在瞬息百变的对战中提高生存的机会:而Windows玩家掌握了快捷键,不但可以提高电脑操作速度,更能享受到初级玩家望着你那仰慕的眼神 ...

  4. poj2391 最大流+拆点

    题意:F块草坪,上面有n头牛,可以容纳m个牛遮雨.将草坪一份为2,成为二部图. 对于此题,和poj2112很像,只是2112很明显的二部图.这道题就开始敲,但是建图遇到问题,草坪的2个值怎么处理,于是 ...

  5. JDK 8 中包列表及介绍

    了解了Java 8中所有包的作用,对Java 8有了一个整体的了解,另外也是提高了自身的阅读能力.本文列出了Java 8中所有的包,并且对每一个包的功能做了简要的说明,希望对你有所帮助. ------ ...

  6. bzoj4152 The Captain

    Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. Input 第一行包含一个正整数n(2 ...

  7. 巨蟒python全栈开发-第11阶段 ansible3_1入门四个模块command&shell&script&copy

    大纲 1.系统安装与机器克隆 2.ansible介绍和host-pattern格式 3.command模块 4.shell模块 5.script模块 6.copy模块

  8. LightOJ 1236 Pairs Forming LCM【整数分解】

    题目链接: http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1236 题意: 找与n公倍数为n的个数. 分析: ...

  9. Codesign error: Certificate identity appearing twice

    第一种解决方法: I think I figured out why the simple delete is not working. Because the dev certificate is ...

  10. Python语言的特点