博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 中IntPtr 与 string,数组互转
阅读量:4303 次
发布时间:2019-05-27

本文共 2198 字,大约阅读时间需要 7 分钟。

一、IntPtr 与 string互转

string str = "aa";

IntPtr init = Marshal.StringToHGlobalAnsi(str);

string ss= Marshal.PtrToStringAnsi(init);

//最后释放掉

Marshal.FreeHGlobal(init);  

二、char*与string互转

 string a = "11";      

 char* aChar = (char*)System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(a).ToPointer(); 

string result = Marshal.PtrToStringAnsi((IntPtr)aChar);   

三、char* 与 IntPtr互转

可以直接强制类型转换

IntPtr init = (IntPtr)aChar;

char* aChar = (char*)init;

 

在c++里数据转指针是很容易的,但是在托管代码里,转起来就比较费劲了。转换方法如下:
  • 1

internal static IntPtr ArrayToIntptr(byte[] source)

{
if (source == null)
return IntPtr.Zero;
unsafe
{
fixed (byte* point = source)
{
IntPtr ptr = new IntPtr(point);
return ptr;
}
}
}
internal static IntPtr ArrayToIntptr(byte[] source)
{
if (source == null)
return IntPtr.Zero;
byte[] da = source;
IntPtr ptr = Marshal.AllocHGlobal(da.Length);
Marshal.Copy(da, 0, ptr, da.Length);
return ptr;
}
如果不安全代码要用在.net里,只是在项目属性里,设置一下就行,但是要在unity里用的话就必须要加点东西了。
在unity里使用不安全代码时,要在Assetm目录下建一个smcs.rsp文件,在里边写一句-unsafe就行了,这个文件就是一个文本文件,然后改一下后缀名就行。

将数组转换为IntPtr

 
  1. //第一种,使用不安全的代码块来访问直接指向字节数组的指针。

  2. static IntPtr ArrayToIntptr(byte[] source)

  3. {

  4. if (source == null)

  5. {

  6. return IntPtr.Zero;

  7. }

  8.  
  9. unsafe

  10. {

  11. fixed (byte* point = source)

  12. {

  13. IntPtr ptr = new IntPtr(point);

  14. return ptr;

  15. }

  16. }

  17. }

  18.  
  19.  
  20. //第二种,可以使用 GCHandle 来获得对象。

  21. GCHandle

  22. using System.Runtime.InteropServices;

  23.  
  24. byte[] test = new byte[5];

  25. GCHandle hObject = GCHandle.Alloc(test, GCHandleType.Pinned);

  26. IntPtr pObject = hObject.AddrOfPinnedObject();

  27.  
  28. if(hObject.IsAllocated)

  29. {

  30. hObject.Free();

  31. }

  32.  
  33.  
  34. //第三种, 通过 LocalAlloc 创建内存块并将数据封送处理到该内存块。

  35. LocalAlloc

  36. [DllImport("coredll.dll",SetLastError=true)]

  37. public static extern IntPtr LocalAlloc(uint uFlags, uint uBytes);

  38. [DllImport("coredll.dll",SetLastError=true)]

  39. public static extern IntPtr LocalFree(IntPtr hMem);

  40. [DllImport("coredll.dll",SetLastError=true)]

  41. public static extern IntPtr LocalReAlloc(IntPtr hMem, uint uBytes, uint fuFlags);

  42.  
  43. public const uint LMEM_FIXED = 0;

  44. public const uint LMEM_MOVEABLE = 2;

  45. public const uint LMEM_ZEROINIT = 0x0040;

  46.  
  47. byte[] test = new byte[5];

  48. IntPtr p = LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT, (uint)test.Length);

  49.  
  50. if (p == IntPtr.Zero)

  51. {

  52. throw new OutOfMemoryException();

  53. }

  54. else

  55. {

  56. Marshal.Copy(test, 0, p, test.Length);

  57. }

 

转载地址:http://pdlws.baihongyu.com/

你可能感兴趣的文章
VNPY - CTA策略模块策略开发
查看>>
VNPY - 事件引擎
查看>>
MongoDB基本语法和操作入门
查看>>
学习笔记_vnpy实战培训day04_作业
查看>>
OCO订单(委托)
查看>>
学习笔记_vnpy实战培训day05
查看>>
学习笔记_vnpy实战培训day06
查看>>
Python super钻石继承
查看>>
回测引擎代码分析流程图
查看>>
Excel 如何制作时间轴
查看>>
股票网格交易策略
查看>>
matplotlib绘图跳过时间段的处理方案
查看>>
vnpy学习_04回测评价指标的缺陷
查看>>
ubuntu终端一次多条命令方法和区别
查看>>
python之偏函数
查看>>
vnpy学习_06回测结果可视化改进
查看>>
读书笔记_量化交易如何建立自己的算法交易01
查看>>
设计模式03_工厂
查看>>
设计模式04_抽象工厂
查看>>
设计模式05_单例
查看>>