5,667,575 members and growing! (11,183 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

How to copy a String into a struct using C#

By dgiljr

How to copy a String into a struct using C#
C#, Windows, .NET, Visual Studio, Dev

Posted: 9 Jun 2004
Updated: 10 Jun 2004
Views: 49,812
Bookmarked: 9 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
29 votes for this Article.
Popularity: 3.22 Rating: 2.20 out of 5
13 votes, 44.8%
1
3 votes, 10.3%
2
1 vote, 3.4%
3
6 votes, 20.7%
4
6 votes, 20.7%
5

Introduction

This article show a simple code snippet using which you can copy a string into a struct.

Using the code

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);
        Marshal.FreeBSTR(pBuf);
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

dgiljr



Occupation: Web Developer
Location: United States United States

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 17 of 17 (Total in Forum: 17) (Refresh)FirstPrevNext
GeneralMarshalling BSTR *memberburatino2222:48 4 Jun '07  
GeneralMixed structuresmemberMancunian4:00 21 Apr '06  
GeneralIs it possible to create a function of this?sussAnonymous18:37 7 Apr '05  
GeneralRe: Is it possible to create a function of this?memberdgiljr5:16 8 Apr '05  
GeneralRe: Is it possible to create a function of this?sussAnonymous16:08 26 Apr '05  
Generalpointlessmemberleppie8:12 10 Jun '04  
GeneralRe: pointlessmemberdgiljr9:35 10 Jun '04  
GeneralRe: pointlessmembersoumyas_v19:12 28 Oct '06  
GeneralGood, but...membercasperOne3:15 10 Jun '04  
GeneralRe: Good, but...memberdgiljr5:32 10 Jun '04  
GeneralRe: Good, but...membercasperOne7:21 10 Jun '04  
GeneralRe: Good, but...memberdgiljr9:32 10 Jun '04  
GeneralRe: Good, but...membercasperOne9:52 10 Jun '04  
GeneralRe: Good, but...supporterMark Abela13:21 10 Jun '04  
GeneralRe: Good, but...memberdgiljr4:57 11 Jun '04  
GeneralRe: Good, but...supporterMark Abela12:42 14 Jun '04  
GeneralRe: Good, but...memberdgiljr4:46 15 Jun '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 Jun 2004
Editor: Nishant Sivakumar
Copyright 2004 by dgiljr
Everything else Copyright © CodeProject, 1999-2008
Web20 | Advertise on the Code Project