C#中的IntPtr

IntPtr是一个类,用于包装调用WindowsAPI函数的指针,根据平台的不同,底层指针可以是32位或64位;它用以表示指针或句柄的平台特定类型,C#中主要用它调用C++\C封装的DLl库;下面主要介绍IntPtr的常见用法

1.int类型与IntPtr类型之间的转换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace MyIntPtr
{
    class Program
    {
        static void Main(string[] args)
        {
            int nValue1 = 10;
            int nValue2 = 20;
            //AllocHGlobal(int cb):通过使用指定的字节数,从进程的非托管内存中分配内存。
            IntPtr ptr1 = Marshal.AllocHGlobal(sizeof(int));
            IntPtr ptr2 = Marshal.AllocHGlobal(sizeof(int));
            //WriteInt32(IntPtr ptr, int val):将 32 位有符号整数值写入非托管内存。
            //int->IntPtr
            Marshal.WriteInt32(ptr1, nValue1);
            Marshal.WriteInt32(ptr2, nValue2);
            // ReadInt32(IntPtr ptr, int ofs):从非托管内存按给定的偏移量读取一个 32 位带符号整数
            //IntPtr->int
            int nVal1 = Marshal.ReadInt32(ptr1, 0);
            int nVal2 = Marshal.ReadInt32(ptr2, 0);
            //FreeHGlobal(IntPtr hglobal):释放以前从进程的非托管内存中分配的内存。
            Marshal.FreeHGlobal(ptr1);
            Marshal.FreeHGlobal(ptr2);
        }
    }
}

2.string类型与IntPtr之间的转换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace MyIntPtr
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "aa";
            IntPtr strPtr = Marshal.StringToHGlobalAnsi(str);
            string ss = Marshal.PtrToStringAnsi(strPtr);
            Marshal.FreeHGlobal(strPtr);  
        }
    }
}

 3.结构体与IntPtr之间的转换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace MyIntPtr
{
    class Program
    {
        public struct stuInfo
        {
            public string Name;
            public string Gender;
            public int Age;
            public int Height;
        }
        static void Main(string[] args)
        {
            stuInfo stu = new stuInfo()
            {
                Name = "张三",
                Gender = "",
                Age = 23,
                Height = 172,
            };

            //获取结构体占用空间的大小
            int nSize = Marshal.SizeOf(stu);
            //声明一个相同大小的内存空间
            IntPtr intPtr = Marshal.AllocHGlobal(nSize);
            //IntPtr->Struct
            Marshal.StructureToPtr(stu, intPtr,true);
            //Struct->IntPtr
            stuInfo Info =(stuInfo)Marshal.PtrToStructure(intPtr, typeof(stuInfo));

            Console.ReadKey();

        }
    }
}

 

转载于:https://www.cnblogs.com/QingYiShouJiuRen/p/10274197.html

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值