使用C#拷贝String到struct
2008-05-01 02:30
465 查看
使用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_ href="http://www.66of.com" target=_blank>structs.asp
biqiong2004@hotmail.com
http://home.ripway.com/2004-6/12
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_ href="http://www.66of.com" target=_blank>structs.asp
biqiong2004@hotmail.com
http://home.ripway.com/2004-6/12
相关文章推荐
- 使用C#拷贝String到struct
- C# StringBuilder的介绍和使用、Stopwatch 计算程序运行的时间
- 【C#】String 格式化使用说明
- c# string.Concat的使用
- Java和C#中String直接赋值与使用new创建(==与equals进行比较)的区别
- 在C#(CSharp)中如何使用string.Format输出大括号{}
- C#中 string.format方法使用大全
- C#中使用string.Split 用字符串分隔
- Unity X C#小记之使用List与string的搭配
- 结构及其使用 struct (C#)
- C#中StringBuilder类和string的使用及对比
- C# 字符串string的使用
- C# string.Format 格式化使用说明
- C#中,字符串的声明,你使用String还是string?
- 在struct 中使用string,赋值会报错
- C# 字符串String的常用使用方法
- multimap <string, struct>使用范例
- 结构及其使用 struct (C#)
- C# FontStyle枚举的使用 (將String转换成FontStyle枚举)
- C#使用String.Format()方法时,出现异常"输入的字符串格式有误"的解决方法