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] ...
随机推荐
- .net 用ajaxFileUpload 上传超过20M文件设置
1.在web.config的 <system.web> 节点里面添加 <httpRuntime targetFramework="4.5.2" execu ...
- js 高级程序设计(笔记)
第二章 1.为了避免浏览器在呈现页面时出现明显的延迟,现代Web 应用程序一般都把全部JavaScript 引用放在<body>元素中页面内容的后面. 第三章 1.ECMAScript 中 ...
- Spring Boot应用总结更新
一.SpringBoot的产生背景: SpringBoot的产生背景伴随着微服务,微服务的相关概念参考上一篇的博客,分布式架构理论: 微服务的宏观概念理解: 将一个大应用拆分成多个小应用,一个小应用是 ...
- C语言使用HZK16显示每个像素的代码
下边内容段是关于C语言使用HZK16显示每个像素的内容. #include<stdio.h>#include<stdlib.h>void main(){ int i,j; ch ...
- python3 set(集合)
add(增加元素) name = set(['Tom','Lucy','Ben']) name.add('Juny') print(name) #输出:{'Lucy', 'Juny', 'Ben', ...
- Spring注解定时器使用
一.首先要配置我们的spring-service.xml 1.xmlns 多加下面的内容 xmlns:task="http://www.springframework.org/schema/ ...
- MapReduce shuffle过程剖析及调优
MapReduce简介 在Hadoop MapReduce中,框架会确保reduce收到的输入数据是根据key排序过的.数据从Mapper输出到Reducer接收,是一个很复杂的过程,框架处理了所有问 ...
- 单元测试(qunit)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- 【转】VUE 爬坑之旅-- 如何对公共JS,CSS进行统一管理,全局调用
原文:https://blog.csdn.net/zgh0711/article/details/78664262 vue 中,将页面分为了各个组件,我们写好组件,就可以将这个组件运用到其他各个页面中 ...
- 【题解】P1119 灾后重建
题目地址 理解Floyed的本质 Floyed的本质是动态规划. 在地K次循环中,Floyed算法枚举任意点对(X,Y),在这之前,K从未做过任何点对的中点.因此,可以利用K为中转的路径长度更新. 在 ...