您的位置:首页 > 编程语言 > C#

使用C#拷贝String到struct

2008-04-24 09:12 211 查看
使用C#拷贝String到structBydgiljr介绍本文介绍使用C#拷贝String到struct。代码
usingSystem;
usingSystem.Runtime.InteropServices;
usingSystem.Text;
classClass1
{
[structLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
publicstructMystruct
{
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=4)]publicStringfname;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=4)]publicStringlname;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=7)]publicStringphone;
}
publicstaticvoidMain()
{
Stringbuffer="abcdefgh2223333";
IntPtrpBuf=Marshal.StringToBSTR(buffer);
Mystructms=(Mystruct)Marshal.PtrTostructure(pBuf,typeof(Mystruct));
Console.WriteLine("fnameis:{0}",ms.fname);
Console.WriteLine("lnameis:{0}",ms.lname);
Console.WriteLine("phoneis:{0}",ms.phone);
}
}
From:structs.asp">http://www.codeproject.com/csharp/gil_structs.aspbiqiong2004@hotmail.comhttp://home.ripway.com/2004-6/124912/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: