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 ...
随机推荐
- Python- and & or 的短路原则
条件1 and 条件2 条件1 or 条件2 短路原则 对于and 如果前面的第一个条件为假,那么这个and前后两个条件组成的表达式的计算结果就一定为假,第二个条件就不会被计算 对于or 如果前面的第 ...
- matlab之viscircles()函数
函数功能:画圆 语法: viscircles(centers,radii) viscircles(ax,centers,radii) viscircles(___,Name,Value) h = vi ...
- Codeforces 897C Nephren gives a riddle:模拟【珂学】
题目链接:http://codeforces.com/contest/897/problem/C 题意: 给你一些字符串: A: [What are you doing at the end of t ...
- 英语发音规则---Y字母
英语发音规则---Y字母 一.总结 一句话总结: 1.Y字母在单词最前面读发[j]? yes /jes/ [jɛs] n. 是 yard /jɑːd/ [jɑd] n. 院子 yellow /'jel ...
- C#--父子页面传值、刷新(showModalDialog)
父页面: var obj = new Object(); obj.name="name"; var rtnValue=window.showModalDialog("ch ...
- 【leetcode刷题笔记】Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- 【二叉树的递归】03判断二叉树中有没有和为给定值的路径【Path Sum】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树和一个和,判断这个树 ...
- aac格式介绍
AAC编码后数据打包到FLV很简单. 1. FLV音频Tag格式 字节位置 意义0x08, ...
- ViewModel中C# Property自动添加OnPropertyChanged处理的小工具, 以及相应Python知识点
在做WPFMVVM中经常会遇到一些Model.ViewModel的属性添加添加私有字段和更改通知方法来支持Binding. 比如把: public class Test { public s ...
- 机器学习:k-NN算法(也叫k近邻算法)
一.kNN算法基础 # kNN:k-Nearest Neighboors # 多用于解决分裂问题 1)特点: 是机器学习中唯一一个不需要训练过程的算法,可以别认为是没有模型的算法,也可以认为训练数据集 ...