static void Main(string[] args)
{
byte[] a = new byte[]{,,,};
byte[] b = new byte[] {,,,};
IntPtr pt = Marshal.AllocHGlobal(a.Length);
//从source数组的startIndex下标开始复制length个对象到ptr;
Marshal.Copy(b,,pt+,b.Length);
//从ptr复制length个对象到目标数组的,从目标数组的startIndex开始写入。
Marshal.Copy((pt+),a,,);
unsafe
{
byte* pb = (byte*) pt;
for (int i = ; i < a.Length; i++)
{
Console.WriteLine("/b:"+(*pb++) + "/a:" + a[i]);
}
}
//释放非托管内存;
Marshal.FreeHGlobal(pt); byte[] arBt = new byte[]{,,,,,,,,,,,,,,0x0f,};
IntPtr ptr = Marshal.AllocHGlobal(arBt.Length);
//写入数据;
Marshal.Copy(arBt, , ptr, arBt.Length);
short[] arSt = new short[arBt.Length / sizeof(short)];
int[] arInt = new int[arBt.Length / sizeof(int)];
//复制为short数据;
Marshal.Copy(ptr, arSt, , arSt.Length);
//调整数据 此时arSt不变 下面的arInt改变;
Marshal.WriteByte(ptr, , );
Marshal.WriteByte(ptr, , (byte)(Marshal.ReadByte(ptr,)*));
//复制为int数据;
Marshal.Copy(ptr, arInt, , arInt.Length);
for (int i = ; i < arBt.Length; i++)
{
Console.Write(arBt[i] + "-");
}
Console.WriteLine();
for (int i = ; i < arBt.Length; i++)
{
Console.Write(Marshal.ReadByte(ptr,i) + "-");
}
Console.WriteLine();
unsafe
{  //获取指定数组中指定索引处的元素的地址
short* ps = (short*)Marshal.UnsafeAddrOfPinnedArrayElement(arSt, );
byte[] tmp0 = BitConverter.GetBytes(*ps);
Console.WriteLine(*ps+ "/" + (ushort)*ps+ ",byte>>>&0=" + tmp0[] + ",&1=" + tmp0[]);
          //获取指定数组中指定索引处的元素的地址
int* pi = (int*)Marshal.UnsafeAddrOfPinnedArrayElement(arInt, );
byte[] tmp1 = BitConverter.GetBytes(*pi);
Console.WriteLine(*pi + "/" + (uint)*pi + ",byte>>>&0=" + tmp1[] + ",&1=" + tmp1[] +",&2="+ tmp1[] + ",&3=" + tmp1[]); Console.WriteLine("-----short 2 byte-----");
for (int i = ; i < arSt.Length; i++)
{
byte[] tmp = BitConverter.GetBytes(arSt[i]);
Console.WriteLine(arSt[i] + "/" + (ushort)arSt[i] + ",byte>>>&0=" + tmp[] + ",&1=" + tmp[]);
}
Console.WriteLine("-----int 2 byte-----");
for (int i = ; i < arInt.Length; i++)
{
byte[] tmp = BitConverter.GetBytes(arInt[i]);
Console.WriteLine(arInt[i] + "/" + (uint)arInt[i] + ",byte>>>&0=" + tmp[] + ",&1=" + tmp[] + ",&2=" + tmp[] + ",&3=" + tmp[]);
}
}
Marshal.FreeHGlobal(ptr);
       unsafe
{
Test tt = new Test();
tt.t1 = ;
tt.t2 = true;
tt.t3 = ;
tt.t4 = ;
tt.t6 = false;
string s = "ABCD大师0X00";
char[] chs = s.ToCharArray();
char* block = stackalloc char[];
fixed (char* cpt = chs)
{
for (int i = ; i < chs.Length; i++)
{
tt.t5[i] = *(cpt + i);
block[i] = *(cpt + i);
}
Console.WriteLine(new string(tt.t5));
}
}
}
        [StructLayout(LayoutKind.Sequential, Pack = )]
public unsafe struct Test
{
public byte t1;
public bool t2;
public ushort t3;
public int t4;
//固定大小的缓冲区
public fixed char t5[];
public bool t6;
}

//----------------------------------

通过上面的例子,我们可以看出,使用C#指针操作内存,非常方便。使用Marshal我们可以获得非托管内存的指针IntPtr。该指针我们可以强制转换为

sbytebyteshortushortintuintlongulongcharfloatdoubledecimalbool的类型指针。之后我们可以Copy,Read ,Write等操作内存。

同C++一样我们获得的指针可以通过指针运算符 *,->,&,++,--进行指定内存的数据和位移操作。也可以通过Marshal,将我们的byte类型数据进行类型转换操作。

Marshal类提供的转换函数功能之强大。

可参阅https://msdn.microsoft.com/zh-cn/library/system.runtime.interopservices.marshal_methods%28v=vs.80%29.aspx;

C#指针操作Marshal实例的更多相关文章

  1. C语言指针操作

    欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/pointer-manipulation. ...

  2. Python操作Mysql实例代码教程在线版(查询手册)_python

    实例1.取得MYSQL的版本 在windows环境下安装mysql模块用于python开发 MySQL-python Windows下EXE安装文件下载 复制代码 代码如下: # -*- coding ...

  3. C# 指针操作相关

    C# 指针操作相关 =========================================            // ** 取int 地址 以指针访问指向值 ** //         ...

  4. 基本的window.document操作及实例

    基本的window.document操作及实例 找元素 1.根据id找 var d1 = document.getElementById("d1"); alert(d1); 2.根 ...

  5. Selenium2学习-040-JavaScript弹出框(alert、confirm、prompt)操作演示实例

    弹出框是网页自动化测试常见得操作页面元素之一,常见的JavaScript弹出框有如下三种: 1.alert(message):方法用于显示带有一条指定消息和一个 OK 按钮的警告框.DemoAlert ...

  6. C#开发中使用Npoi操作excel实例代码

    C#开发中使用Npoi操作excel实例代码 出处:西西整理 作者:西西 日期:2012/11/16 9:35:50 [大 中 小] 评论: 0 | 我要发表看法 Npoi 是什么? 1.整个Exce ...

  7. Selenium2学习-014-WebUI自动化实战实例-012-Selenium 操作下拉列表实例-div+{js|jquery}

    之前已经讲过了 Selenium 操作 Select 实现的下拉列表:Selenium2学习-010-WebUI自动化实战实例-008-Selenium 操作下拉列表实例-Select,但是在实际的日 ...

  8. Selenium2学习-010-WebUI自动化实战实例-008-Selenium 操作下拉列表实例-Select

    此文主要讲述用 Java 编写 Selenium 自动化测试脚本编写过程中,对下拉列表框 Select 的操作. 下拉列表是 Web UI 自动化测试过程中使用率非常高的,通常有两种形式的下拉列表,一 ...

  9. Day4:T1小技巧(类似于指针操作)T2搜索+小细节

    Day4:其中有很多小技巧get T1 一直没有听到过像这样的小技巧的略专业名词,有点类似于指针操作,之前有碰到过很多这样的题目 每次都是以不同的形式出现,但是感觉思想还是有点接近的吧(就比如某天有一 ...

随机推荐

  1. JSON 换行、JSON \r\n、怎么处理 ?(转载)

    参考地址: http://www.cnblogs.com/mamingbo/archive/2010/11/27/1889583.html 在最后json的字符串上:.Replace("\r ...

  2. SQL Server 一列或多列重复数据的查询,删除

    业务需求 最近给公司做一个小工具,把某个数据库(数据源)的数据导进另一个数据(目标数据库).要求导入目标数据库的数据不能出现重复.但情况是数据源本身就有重复的数据.所以要先清除数据源数据. 于是就把关 ...

  3. Objective-C:@property参数详解

    格式:@property(param1, param2) 类型 属性名 参数可有可无 三类参数: 1.读写属性:readwrite/readonly readwrite:产生setter\getter ...

  4. oracle用户

    如果要了解oracle中用户信息,可以查询数据字典dba_users.在sql*plus中,使用system用户登录,查询语句如下: select username,account_status fr ...

  5. ThinkPHP中数据库操作返回值总结

    转自:http://www.baiwar.com/post/thinkphp-database-operations-in-the-return-value.html Thinkphp中的Think\ ...

  6. Jquery表单提交方式

    1.使用调用submit方法 function tes1(){ //执行判断 if(校验通过){ $("#formId").submit(); }else{ return; } } ...

  7. linux 系统安装 mysql

    安装mysql所需要的依赖环境 yum -y install gcc gcc-c++ gcc-g77 autoconf automake zlib*  libxml* ncurses-devel li ...

  8. The bundle does not contain an app icon for iPhone / iPod Touch of exactly '57x57' pixels

    遇到这个问题问题,搜索了一圈要么不知道,要么喊改deploymenet target, 最后我是在项目属性-info-icon files(ios5)-下面添加了一个icon,然后弄了个icon.pn ...

  9. HDU 2594 扩展kmp模板题

    题目大意: 给定两个字符串,在第一个字符串中找到一个最大前缀作为第二个字符串的后缀 #include <iostream> #include <cstdio> #include ...

  10. MySQL数据库远程连接

    12.00 MySQL数据库远程连接 参考: http://www.jb51.net/article/24508.htm http://www.linuxdiyf.com/viewarticle.ph ...