繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> Asp.Net开发 >> 使用C#拷贝String到struct

使用C#拷贝String到struct

2007-08-15 08:00:00  作者:  来源:互联网  浏览次数:0  文字大小:【】【】【
简介:使用C#拷贝String到struct By dgiljr 介绍 本文介绍使用C#拷贝String到struct 。 代码 using System; using System.Runtime.InteropServices; using System.Text; class Class1 { [StructLayout(LayoutKind.Seq...
关键字:C# 拷贝 struct String

使用C#拷贝String到struct

By dgiljr

介绍

本文介绍使用C#拷贝String到struct 。

代码

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_structs.ASP

biqiong2004@hotmail.com

http://home.ripway.com/2004-6/124912/

责任编辑:admin
相关文章