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

使用C#拷贝String到struct

2008-05-01 02:30 465 查看
使用C#拷贝Stringstruct

By dgiljr

介绍

本文介绍使用C#拷贝Stringstruct

代码

using System;
using System.Runtime.InteropServices;
using System.Text;

class Class1
{

[structLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct Mystruct
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=4)] public String fname;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=4)] public String lname;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=7)] public String phone;
}

public static void Main()
{
String buffer = "abcdefgh2223333";
IntPtr pBuf = Marshal.StringToBSTR(buffer);
Mystruct ms = (Mystruct)Marshal.PtrTostructure(pBuf,typeof(Mystruct));
Console.WriteLine("fname is: {0}",ms.fname);
Console.WriteLine("lname is: {0}",ms.lname);
Console.WriteLine("phone is: {0}",ms.phone);
}
}

From :
http://www.codeproject.com/csharp/gil_ href="http://www.66of.com" target=_blank>structs.asp

biqiong2004@hotmail.com
http://home.ripway.com/2004-6/12
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: