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 = ;
int nValue2 = ;
//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, );
int nVal2 = Marshal.ReadInt32(ptr2, );
//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 = ,
Height = ,
}; //获取结构体占用空间的大小
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(); }
}
}
C#中的IntPtr的更多相关文章
- C#中的IntPtr类型
本文转自:http://zhidao.baidu.com/question/22825956.html 问: c#中无法将类型“int”隐式转换为“System.IntPtr” 这个是我引用了一个ap ...
- 如何调用com组件中包含IntPtr类型参数的函数
背景 公司的支付平台最近对接了西安移动的支付接口,接口中签名的方法是对方提供了一个com组件,组件中包含了一个签名的方法和一个验签的方法,注册了签名之后,在vs中进行了引用,引用之后,查看组件的定义如 ...
- 为什么C#中要设计IntPtr?
示例代码: IntPtr vertex = someObj.Get().Lock(0, someObj.Get().GetSizeInBytes(), HardwareBuffer.LOCKOPTIO ...
- 如何将C++中的SOCKADDR_IN*参数类型转换成C#中的参数类型
将C++中的参数类型SOCKADDR_IN*映射为C#中的IntPtr参数类型的示例代码如下: IntPtr ptrSockaddr = new IntPtr(); //ip地址 sockaddr_i ...
- 在 C# 中通过 P/Invoke 调用Win32 DLL
在 C# 中通过 P/Invoke 调用Win32 DLL 发布日期 : 1/13/2005 | 更新日期 : 1/13/2005 Jason Clark 下载本文的代码: NET0307.exe ( ...
- 用 Span 对 C# 进程中三大内存区域进行统一访问 ,太厉害了!
一:背景 1. 讲故事 前段时间写了几篇 C# 漫文,评论留言中有很多朋友多次提到 Span,周末抽空看了下,确实是一个非常
- 【C# 线程】IntPtr 类
IntPtr类由来 .NET提供了一个结构体System.IntPtr专门用来代表句柄或指针.句柄是对象的标识符,当调用这些API创建对象时,它们并不直接返回指向对象的指针,而是会返回一个32位或64 ...
- .NET学习之路----我对P/Invoke技术的理解(一)
看了P/Invoke技术的介绍,于是想写下点东西,东西包含两个部分:知识的纪录和我的理解及疑问. r托管代码中调用非托管API函数的过程 1.定位包含API的DLL: 2.载入DLL 3.找到DLL中 ...
- 简单bat语法
一.简单批处理内部命令简介 1.Echo 命令 打开回显或关闭请求回显功能,或显示消息.如果没有任何参数,echo 命令将显示当前回显设置. 语法 echo [{on off}] [message] ...
随机推荐
- JsonConvert.DeserializeObject反序列化
JObject ci = JsonConvert.DeserializeObject<JObject>(row); string s = ci["txtContent" ...
- Java 学习笔记 使用并发包ReentrantLock简化生产者消费者模式代码
说明 ReentrantLock是java官方的一个线程锁类,ReentarntLock实现了Lock的接口 我们只需要使用这个,就可以不用使用synchronized同步关键字以及对应的notify ...
- vue webpack配置Error
学写慕课网的Vue核心技术Vue+Vue-Router+Vuex+SSR实战精讲时,发现因为webpack,babel等升级了.按照视频的代码配置webpack会出问题. 报错:TypeError: ...
- 【代码笔记】Web-CSS-CSS Positioning
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- How to Apply Patches to a WLS 8.1 Environment
APPLIES TO: Oracle Weblogic Server - Version 8.1 to 8.1Information in this document applies to any p ...
- windows蓝屏代码
原始链接 引用自 https://docs.microsoft.com/zh-cn/windows-hardware/drivers/debugger/bug-check-code-referenc ...
- Android为TV端助力 反编译
http://blog.csdn.net/vipzjyno1/article/details/21039349/ apktool.bat d -f test.apk test 这条命令修改 ...
- Linux 中使用 firewalld
firewalld 是一种动态防火墙管理解决方案.Centos 7 默认使用 firewalld.firewalld 是对 iptables 的一个封装,可以让你更容易地管理 iptables 规则. ...
- spring学习总结——高级装配学习三(Bean的作用域)
一.bean的作用域 在默认情况下,Spring应用上下文中所有bean都是作为以单例(singleton)的形式创建的.也就是说,不管给定的一个bean被注入到其他bean多少次,每次所注入的都是同 ...
- 【温故而知新】HTTP 报文
HTTP 报文是在 HTTP 应用程序之间发送的数据块.这些数据块以一些文本形式的元信息开头,这些信息描述了报文的内容及含义. 报文流 报文在客户端.服务器和代理之间的流动称为报文流. HTTP 使用 ...