Click here to Skip to main content
6,595,854 members and growing! (19,970 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:56,354
Bookmarked:13 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
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


Member

Occupation: Web Developer
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 17 of 17 (Total in Forum: 17) (Refresh)FirstPrevNext
GeneralMarshalling BSTR * Pinmemberburatino2222:48 4 Jun '07  
GeneralMixed structures PinmemberMancunian4:00 21 Apr '06  
GeneralIs it possible to create a function of this? PinsussAnonymous18:37 7 Apr '05  
GeneralRe: Is it possible to create a function of this? Pinmemberdgiljr5:16 8 Apr '05  
GeneralRe: Is it possible to create a function of this? PinsussAnonymous16:08 26 Apr '05  
Generalpointless Pinmemberleppie8:12 10 Jun '04  
GeneralRe: pointless Pinmemberdgiljr9:35 10 Jun '04  
GeneralRe: pointless Pinmembersoumyas_v19:12 28 Oct '06  
GeneralGood, but... PinmembercasperOne3:15 10 Jun '04  
GeneralRe: Good, but... Pinmemberdgiljr5:32 10 Jun '04  
GeneralRe: Good, but... PinmembercasperOne7:21 10 Jun '04  
GeneralRe: Good, but... Pinmemberdgiljr9:32 10 Jun '04  
GeneralRe: Good, but... PinmembercasperOne9:52 10 Jun '04  
GeneralRe: Good, but... PinsupporterMark Abela13:21 10 Jun '04  
GeneralRe: Good, but... Pinmemberdgiljr4:57 11 Jun '04  
GeneralRe: Good, but... PinsupporterMark Abela12:42 14 Jun '04  
GeneralRe: Good, but... Pinmemberdgiljr4: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-2009
Web19 | Advertise on the Code Project