使用C#拷贝String到struct

来源:互联网 发布:shadow defender 知乎 编辑:程序博客网 时间:2024/05/16 10:38
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
使用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_structs.asp





biqiong2004@hotmail.com

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

拷贝Stringstruct';return true">
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击