Click here to Skip to main content
15,888,454 members
Articles / Programming Languages / C#
Article

How to copy a String into a struct using C#

Rate me:
Please Sign up or sign in to vote.
1.95/5 (30 votes)
10 Jun 2004 108.2K   17   17
How to copy a String into a struct using C#

Introduction

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

Using the code

C#
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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMarshalling BSTR * Pin
buratino224-Jun-07 21:48
buratino224-Jun-07 21:48 
GeneralMixed structures Pin
Mancunian21-Apr-06 3:00
Mancunian21-Apr-06 3:00 
QuestionIs it possible to create a function of this? Pin
Anonymous7-Apr-05 17:37
Anonymous7-Apr-05 17:37 
AnswerRe: Is it possible to create a function of this? Pin
dgiljr8-Apr-05 4:16
dgiljr8-Apr-05 4:16 
GeneralRe: Is it possible to create a function of this? Pin
Anonymous26-Apr-05 15:08
Anonymous26-Apr-05 15:08 
Generalpointless Pin
leppie10-Jun-04 7:12
leppie10-Jun-04 7:12 
GeneralRe: pointless Pin
dgiljr10-Jun-04 8:35
dgiljr10-Jun-04 8:35 
GeneralRe: pointless Pin
soumyasg28-Oct-06 18:12
soumyasg28-Oct-06 18:12 
GeneralGood, but... Pin
casperOne10-Jun-04 2:15
casperOne10-Jun-04 2:15 
GeneralRe: Good, but... Pin
dgiljr10-Jun-04 4:32
dgiljr10-Jun-04 4:32 
GeneralRe: Good, but... Pin
casperOne10-Jun-04 6:21
casperOne10-Jun-04 6:21 
GeneralRe: Good, but... Pin
dgiljr10-Jun-04 8:32
dgiljr10-Jun-04 8:32 
You're absolutely right in it's current form that it has one specific use.
We have our own proprietary protocol to send transactions from different servers with some being Unix and all of our clients on windows so our owners wanted a universal solution without having to perform a total rewrite. The current method of managing those buffer changes uses substringing and that is a nightmare if I have 50 fields and field position 10 changed in the number of bytes. In that case I would have to readjust fields 11 to 50 to compensate for that. If I use a struct all I have to do is pass the buffer into the struct and let it handle the byte ordering and if I needed to change one of the field lengths I only have to change the SizeConst and the struct will automatically adjust the byte order for me.

I am very interested in serialization as I am new to C#. How would you implement my issue using serialization?

Diego
GeneralRe: Good, but... Pin
casperOne10-Jun-04 8:52
casperOne10-Jun-04 8:52 
GeneralRe: Good, but... Pin
Mark Abela10-Jun-04 12:21
Mark Abela10-Jun-04 12:21 
GeneralRe: Good, but... Pin
dgiljr11-Jun-04 3:57
dgiljr11-Jun-04 3:57 
GeneralRe: Good, but... Pin
Mark Abela14-Jun-04 11:42
Mark Abela14-Jun-04 11:42 
GeneralRe: Good, but... Pin
dgiljr15-Jun-04 3:46
dgiljr15-Jun-04 3:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.