C# - Marshal.StructureToPtr方法简介
2016-04-06 12:28
549 查看
本博客(http://blog.csdn.net/livelylittlefish)贴出作者(三二一、小鱼)相关研究、学习内容所做的笔记,欢
迎广大朋友指正!
具体可以参考http://msdn.microsoft.com。
将数据从托管对象封送到非托管内存块,属于.NET Framework 类库
命名空间:System.Runtime.InteropServices
程序集:mscorlib(在 mscorlib.dll 中)
C#:
[ComVisibleAttribute(true)] public static void StructureToPtr (Object structure,IntPtr ptr,bool fDeleteOld);
C++:
[ComVisibleAttribute(true)]public: static void StructureToPtr (Object^ structure, IntPtr ptr, bool fDeleteOld);
structure:托管对象,包含要封送的数据。该对象必须是格式化类的实例。
ptr:指向非托管内存块的指针,必须在调用此方法之前分配该指针。
fDeleteOld:设置为 true 可在执行Marshal.DestroyStructure方法前对 ptr 参数调用此方法。请注意,传递 false 可导致内存泄漏。
异常类型:ArgumentException
条件:structrue参数是泛型类型
StructureToPtr将结构的内容复制到 ptr 参数指向的预分配内存块。如果 fDeleteOld 参数为 true,则使用嵌入指
针上适当的删除 API 来删除最初由 ptr 指向的缓冲区,但该缓冲区必须包含有效数据。此方法为在镜像托管类中指
定的每个引用字段执行清理工作。
假设 ptr 指向非托管内存块。此内存块的布局由相应的托管类 structure 描述。StructureToPtr将字段值从结构封
送到指针。假设 ptr 块包含引用字段,该字段指向当前包含“abc”的字符串缓冲区。假设托管端上相应的字段是包含“vwxyz”的字符串。如果不另行通知它,StructureToPtr将分配一个新的非托管缓冲区来保存“vwxyz”,并将它挂钩到 ptr 块。这将丢弃旧缓冲区“abc”使之漂移而不将其释放回非托管堆。最后,您将得到一个孤立的缓冲区,它表示在代码中存在内存泄漏。如果将 fDeleteOld 参数设置为真,则 StructureToPtr 在继续为“vwxyz”分配新缓冲区之前释放保存“abc”的缓冲区。
定义PERSON结构,并将该结构的一个变量拷贝到非托管内存,再将该内存中的PERSON还原为PERSON对象,观察其内容的变化。
源代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Runtime.InteropServices;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace testStructureToPtr
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static class define //define some constant
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const int MAX_LENGTH_OF_IDENTICARDID = 20; //maximum length of identicardid
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const int MAX_LENGTH_OF_NAME = 50; //maximum length of name
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const int MAX_LENGTH_OF_COUNTRY = 50; //maximum length of country
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const int MAX_LENGTH_OF_NATION = 50; //maximum length of nation
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const int MAX_LENGTH_OF_BIRTHDAY = 8; //maximum length of birthday
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const int MAX_LENGTH_OF_ADDRESS = 200; //maximum length of address
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public struct PERSON //person structure
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//MarshalAs:指示如何在托管代码和非托管代码之间封送数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//UnmanagedType:指定如何将参数或字段封送到非托管内存块
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[MarshalAs(UnmanagedType.ByValArray, SizeConst = define.MAX_LENGTH_OF_IDENTICARDID)]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public byte[] identicardid;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[MarshalAs(UnmanagedType.ByValArray, SizeConst = define.MAX_LENGTH_OF_NAME)]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public byte[] name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[MarshalAs(UnmanagedType.ByValArray, SizeConst = define.MAX_LENGTH_OF_COUNTRY)]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public byte[] country;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[MarshalAs(UnmanagedType.ByValArray, SizeConst = define.MAX_LENGTH_OF_NATION)]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public byte[] nation;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[MarshalAs(UnmanagedType.ByValArray, SizeConst = define.MAX_LENGTH_OF_BIRTHDAY)]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public byte[] birthday;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[MarshalAs(UnmanagedType.ByValArray, SizeConst = define.MAX_LENGTH_OF_ADDRESS)]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public byte[] address;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
class testProgram
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static byte _fillChar = 0; //the fill character
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//convert string to byte array in Ascii with length is len
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static byte[] CodeBytes(string str, int len)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (string.IsNullOrEmpty(str))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str = string.Empty;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] result = new byte[len];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] strBytes = Encoding.Default.GetBytes(str);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//copy the array converted into result, and fill the remaining bytes with 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < len; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
result[i] = ((i < strBytes.Length) ? strBytes[i] : _fillChar);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return result;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//show the person information
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static void ShowPerson(PERSON person)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("cardid :" + Encoding.ASCII.GetString(person.identicardid));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("name :" + Encoding.ASCII.GetString(person.name));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("country :" + Encoding.ASCII.GetString(person.country));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("nation :" + Encoding.ASCII.GetString(person.nation));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("birthday :" + Encoding.ASCII.GetString(person.birthday));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("address :" + Encoding.ASCII.GetString(person.address));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static void Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PERSON person;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
person.identicardid = CodeBytes("123456198001011111", define.MAX_LENGTH_OF_IDENTICARDID);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
person.name = CodeBytes("jackson", define.MAX_LENGTH_OF_NAME);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
person.country = CodeBytes("China", define.MAX_LENGTH_OF_COUNTRY);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
person.nation = CodeBytes("HanZu", define.MAX_LENGTH_OF_NATION);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
person.birthday = CodeBytes("19800101", define.MAX_LENGTH_OF_BIRTHDAY);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
person.address = CodeBytes("Luoshan Road, Shanghai", define.MAX_LENGTH_OF_ADDRESS);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int nSizeOfPerson = Marshal.SizeOf(person);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr intPtr = Marshal.AllocHGlobal(nSizeOfPerson);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("The person infomation is as follows:");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ShowPerson(person);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//将数据从托管对象封送到非托管内存块,该内存块开始地址为intPtr
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Marshal.StructureToPtr(person, intPtr, true);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//将数据从非托管内存块封送到新分配的指定类型的托管对象anotherPerson
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PERSON anotherPerson = (PERSON)Marshal.PtrToStructure(intPtr, typeof(PERSON));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("The person after copied is as follows:");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ShowPerson(anotherPerson);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catch (ArgumentException)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
finally
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Marshal.FreeHGlobal(intPtr); //free tha memory
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
运行过程中的对象地址及其内容如下:
intPtr指向的内存块的内容就是程序中对person对象所赋的初值,如下图所示,共计378个字节:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/livelylittlefish/0x0019b2f8=1684216.jpg)
对象person和another的地址及其identicardid成员的地址:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/livelylittlefish/person_20and_20anotherPerson.jpg)
对象person的identicardid成员的内容,即程序中的值123456198001011111,最后的2个字节为0,图中显示的是20个元素的ASCII码的16进制数值:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/livelylittlefish/person.identicardid.jpg)
运行结果如下:
迎广大朋友指正!
具体可以参考http://msdn.microsoft.com。
Marshal.StructureToPtr方法简介
1. 功能及位置
将数据从托管对象封送到非托管内存块,属于.NET Framework 类库命名空间:System.Runtime.InteropServices
程序集:mscorlib(在 mscorlib.dll 中)
2. 语法
C#:[ComVisibleAttribute(true)] public static void StructureToPtr (Object structure,IntPtr ptr,bool fDeleteOld);
C++:
[ComVisibleAttribute(true)]public: static void StructureToPtr (Object^ structure, IntPtr ptr, bool fDeleteOld);
3. 参数说明
structure:托管对象,包含要封送的数据。该对象必须是格式化类的实例。ptr:指向非托管内存块的指针,必须在调用此方法之前分配该指针。
fDeleteOld:设置为 true 可在执行Marshal.DestroyStructure方法前对 ptr 参数调用此方法。请注意,传递 false 可导致内存泄漏。
4. 异常
异常类型:ArgumentException条件:structrue参数是泛型类型
5. 备注
StructureToPtr将结构的内容复制到 ptr 参数指向的预分配内存块。如果 fDeleteOld 参数为 true,则使用嵌入指针上适当的删除 API 来删除最初由 ptr 指向的缓冲区,但该缓冲区必须包含有效数据。此方法为在镜像托管类中指
定的每个引用字段执行清理工作。
假设 ptr 指向非托管内存块。此内存块的布局由相应的托管类 structure 描述。StructureToPtr将字段值从结构封
送到指针。假设 ptr 块包含引用字段,该字段指向当前包含“abc”的字符串缓冲区。假设托管端上相应的字段是包含“vwxyz”的字符串。如果不另行通知它,StructureToPtr将分配一个新的非托管缓冲区来保存“vwxyz”,并将它挂钩到 ptr 块。这将丢弃旧缓冲区“abc”使之漂移而不将其释放回非托管堆。最后,您将得到一个孤立的缓冲区,它表示在代码中存在内存泄漏。如果将 fDeleteOld 参数设置为真,则 StructureToPtr 在继续为“vwxyz”分配新缓冲区之前释放保存“abc”的缓冲区。
6. 举例
定义PERSON结构,并将该结构的一个变量拷贝到非托管内存,再将该内存中的PERSON还原为PERSON对象,观察其内容的变化。源代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Runtime.InteropServices;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace testStructureToPtr
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static class define //define some constant
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const int MAX_LENGTH_OF_IDENTICARDID = 20; //maximum length of identicardid
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const int MAX_LENGTH_OF_NAME = 50; //maximum length of name
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const int MAX_LENGTH_OF_COUNTRY = 50; //maximum length of country
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const int MAX_LENGTH_OF_NATION = 50; //maximum length of nation
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const int MAX_LENGTH_OF_BIRTHDAY = 8; //maximum length of birthday
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public const int MAX_LENGTH_OF_ADDRESS = 200; //maximum length of address
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public struct PERSON //person structure
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//MarshalAs:指示如何在托管代码和非托管代码之间封送数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//UnmanagedType:指定如何将参数或字段封送到非托管内存块
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[MarshalAs(UnmanagedType.ByValArray, SizeConst = define.MAX_LENGTH_OF_IDENTICARDID)]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public byte[] identicardid;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[MarshalAs(UnmanagedType.ByValArray, SizeConst = define.MAX_LENGTH_OF_NAME)]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public byte[] name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[MarshalAs(UnmanagedType.ByValArray, SizeConst = define.MAX_LENGTH_OF_COUNTRY)]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public byte[] country;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[MarshalAs(UnmanagedType.ByValArray, SizeConst = define.MAX_LENGTH_OF_NATION)]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public byte[] nation;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[MarshalAs(UnmanagedType.ByValArray, SizeConst = define.MAX_LENGTH_OF_BIRTHDAY)]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public byte[] birthday;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[MarshalAs(UnmanagedType.ByValArray, SizeConst = define.MAX_LENGTH_OF_ADDRESS)]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public byte[] address;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
class testProgram
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static byte _fillChar = 0; //the fill character
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//convert string to byte array in Ascii with length is len
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static byte[] CodeBytes(string str, int len)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (string.IsNullOrEmpty(str))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str = string.Empty;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] result = new byte[len];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] strBytes = Encoding.Default.GetBytes(str);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//copy the array converted into result, and fill the remaining bytes with 0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < len; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
result[i] = ((i < strBytes.Length) ? strBytes[i] : _fillChar);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return result;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//show the person information
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static void ShowPerson(PERSON person)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("cardid :" + Encoding.ASCII.GetString(person.identicardid));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("name :" + Encoding.ASCII.GetString(person.name));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("country :" + Encoding.ASCII.GetString(person.country));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("nation :" + Encoding.ASCII.GetString(person.nation));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("birthday :" + Encoding.ASCII.GetString(person.birthday));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("address :" + Encoding.ASCII.GetString(person.address));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static void Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PERSON person;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
person.identicardid = CodeBytes("123456198001011111", define.MAX_LENGTH_OF_IDENTICARDID);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
person.name = CodeBytes("jackson", define.MAX_LENGTH_OF_NAME);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
person.country = CodeBytes("China", define.MAX_LENGTH_OF_COUNTRY);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
person.nation = CodeBytes("HanZu", define.MAX_LENGTH_OF_NATION);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
person.birthday = CodeBytes("19800101", define.MAX_LENGTH_OF_BIRTHDAY);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
person.address = CodeBytes("Luoshan Road, Shanghai", define.MAX_LENGTH_OF_ADDRESS);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int nSizeOfPerson = Marshal.SizeOf(person);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
IntPtr intPtr = Marshal.AllocHGlobal(nSizeOfPerson);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("The person infomation is as follows:");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ShowPerson(person);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//将数据从托管对象封送到非托管内存块,该内存块开始地址为intPtr
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Marshal.StructureToPtr(person, intPtr, true);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//将数据从非托管内存块封送到新分配的指定类型的托管对象anotherPerson
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PERSON anotherPerson = (PERSON)Marshal.PtrToStructure(intPtr, typeof(PERSON));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("The person after copied is as follows:");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ShowPerson(anotherPerson);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catch (ArgumentException)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
finally
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Marshal.FreeHGlobal(intPtr); //free tha memory
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
运行过程中的对象地址及其内容如下:
intPtr指向的内存块的内容就是程序中对person对象所赋的初值,如下图所示,共计378个字节:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/livelylittlefish/0x0019b2f8=1684216.jpg)
对象person和another的地址及其identicardid成员的地址:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/livelylittlefish/person_20and_20anotherPerson.jpg)
对象person的identicardid成员的内容,即程序中的值123456198001011111,最后的2个字节为0,图中显示的是20个元素的ASCII码的16进制数值:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/livelylittlefish/person.identicardid.jpg)
运行结果如下:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/livelylittlefish/teststructuretoptr.jpg)
相关文章推荐
- C#实现冒泡排序
- C#学习笔记(初级)
- C#实例之随机数的猜想
- C#枚举,常量(初级)
- C#实例之计算字符串中不同字符的个数
- C#类接口实例
- C# 添加鼠标滚轮事件
- C#Windows服务:一些方法(启动、停止等)
- C#实例之从题库总随机抽取题号
- C#格式转换注意事项
- C#中数组、ArrayList和List三者的区别
- C# Lambda表达式
- C#操作SharePoint文档库文档
- C# Math类简介
- C# 浅拷贝与深拷贝区别
- C# 读写 Web.config
- C# word 编写图标和导出文字以及表格
- 详解C#切换窗口
- 实现一个所有任务都是按各自自定义的时间间隔周期性执行的线程池
- C#在线预览文档(word,excel,pdf,txt,png)