C#结构体指针的定义及使用详解(intptr的用法)
在解析C#结构体指针前,必须知道C#结构体是如何定义的。在c#中同样定义该结构体。
C#结构体指针之C#结构体的定义:
- [StructLayout(LayoutKind.Sequential)]
- public struct VGAStat
- {
- public int ChannelNum;//通道数量
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
- public char[] Version;//版本信息
- public uint CPUUsage;//CPU占用
- public bool WorkStatusOk; //工作状态
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
- public tagCheckArg[] ChannelStatistic;//通道信息
- }
定义完结构体后,就可将接收到的C#结构体指针转换为定义的结构体对象。
- VGAStat entries = (VGAStat)Marshal.PtrToStructure(iptr, typeof(VGAStat));
- //iptr为接收到的非托管的结构体指针。
反之,也可将结构体赋值后封送到非托管内存。
假如vga为定义后实例化并赋值了的结构体。
- IntPtr intptr = Marshal.AllocHGlobal(Marshal.SizeOf(vga));
- Marshal.StructureToPtr(vga, intptr, true);
- //在此发送intptr指针给目的方
- Marshal.FreeHGlobal(intptr);//释放分配的非托管内存。
C#结构体指针的定义及使用的相关内容那个就向你介绍到这里,希望对你了解和学习C#结构体指针有所帮助。
将string转为IntPtr:IntPtr System.Runtime.InteropServices.Marshal.StringToCoTaskMemAuto(string)
将IntPtr转为string:string System.Runtime.InteropServices.MarshalPtrToStringAuto(IntPtr)
http://msdn.microsoft.com/zh-cn/library/system.runtime.interopservices.marshal%28v=vs.110%29.aspx
C#结构体指针的定义及使用详解(intptr的用法)的更多相关文章
- 【C语言入门教程】7.3 结构体指针的定义和引用
C 语言中指针的操作非常灵活,它也能指向结构体变量对结构体变量进行操作.在学习结构指针之前,需要再次加深对指针的认识.声明指针变量时所使用的数据类型修饰符实际上的作用是定义指针访问内存的范围,如果指针 ...
- C++ 结构体指针的定义
struct node { …… } ; struct node *p1, *p2 ; typedef struct node { …… }Node; typedef Node* pNode; typ ...
- (三)结构体指针、sizeof
(一)结构体指针定义 今天上班写了一段测试代码,结果在linux下编译出现段错误,刚开始一直找不到原因,后来找了度娘才搞懂了.我先贴出来第一次写的代码以及gcc编译器下报的错误: #include&l ...
- Leetcode 2. Add Two Numbers(指针和new的使用)结构体指针
---恢复内容开始--- You are given two non-empty linked lists representing two non-negative integers. The di ...
- C与指针(结构体指针,函数指针,数组指针,指针数组)定义与使用
类型 普通指针 指针数组(非指针类型) 数组指针 结构体指针 函数指针 二重指针 定义方式 int *p; int *p[5]; int (*p)[5]; int a[3][5]; struct{.. ...
- C语言定义结构体指针数组并初始化;里面全是结构体的地址
#include <stdio.h> #include <string.h> struct tells;//声明结构体 struct info { char *infos; } ...
- ctypes 操作 python 与 c++ dll 互传结构体指针
CMakeLists.txt # project(工程名) project(blog-3123958139-1) # add_library(链接库名称 SHARED 链接库代码) add_libra ...
- python 传递结构体指针到 c++ dll
CMakeLists.txt # project(工程名) project(xxx) # add_library(链接库名称 SHARED 链接库代码) add_library(xxx SHARED ...
- Delphi 中的结构体与结构体指针
好多程序都给结构体变量设定了一个结构体指针 例如: PAbc = ^TAbc; TAbc = record a: string[10]; b: string[5]; c: string[1]; end ...
随机推荐
- mysql自定义函数语法
创建函数 DELIMITER $$DROP FUNCTION IF EXISTS `test` $$CREATE FUNCTION `test`(a int ,b int)RETURNS int BE ...
- Kafka- Spark消费Kafka
在高版本的API中 val brokers = properties.getProperty("kafka.host.list") val topics = Set(propert ...
- python循环切片
x = [0,99, 'a', 1, 2, 'b',5, 3, 0,'a' ,1, 8, 5,'b',5,9,5] b=[] c=[] d=[] for i in range(len(x)): if ...
- pugixml 1.9 manual解读(部分)
Plain character data nodes (node_pcdata) represent plain text in XML. PCDATA nodes have a value, but ...
- spring boot: 在maven中装入springframework框架
1.在maven 的pom.xml中加入 <dependency> <groupId>org.springframework</groupId> <artif ...
- 命令行执行大sql文件
mysql -h localhost -u root -p 123456 < F:/hello world/niuzi.sql
- List 中去除 null 方法讨论
先看下面的程序段: public static void main(String[] args) { List<Integer> arrays = new ArrayList<Int ...
- 问题杂烩(scrollTop/背景透明度动画)
今天给同学找我帮忙写js,是公司里的活..我是不是应该跟他要钱哈哈,不过一顿饭肯定是免不了的了. 言归正传,今天写了三个小东西,因为兼容性的问题,用jq写的(很是别扭的说,但是没办法啊,一边看api一 ...
- codeforces 652B B. z-sort(水题)
题目链接: B. z-sort time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- hdu-5641 King's Phone (水题)
题目链接: King's Phone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...