首页 > 开发 > 综合 > 正文

使用C#拷贝String到struct

2024-07-21 02:19:26
字体:
来源:转载
供稿:网友
  • 网站运营seo文章大全
  • 提供全面的站长运营经验及seo技术!
  • 使用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





    [email protected]

    http://home.ripway.com/2004-6/124912/
    发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表