Click here to Skip to main content
14,767,302 members
Articles » Languages » C# » General
Article
Posted 9 Jun 2004

Stats

100.6K views
16 bookmarked

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

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

Share

About the Author

dgiljr
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralMarshalling BSTR * Pin
buratino224-Jun-07 22:48
Memberburatino224-Jun-07 22:48 
GeneralMixed structures Pin
Mancunian21-Apr-06 4:00
MemberMancunian21-Apr-06 4:00 
QuestionIs it possible to create a function of this? Pin
Anonymous7-Apr-05 18:37
MemberAnonymous7-Apr-05 18:37 
AnswerRe: Is it possible to create a function of this? Pin
dgiljr8-Apr-05 5:16
Memberdgiljr8-Apr-05 5:16 
GeneralRe: Is it possible to create a function of this? Pin
Anonymous26-Apr-05 16:08
MemberAnonymous26-Apr-05 16:08 
Generalpointless Pin
leppie10-Jun-04 8:12
Memberleppie10-Jun-04 8:12 
GeneralRe: pointless Pin
dgiljr10-Jun-04 9:35
Memberdgiljr10-Jun-04 9:35 
GeneralRe: pointless Pin
soumyasg28-Oct-06 19:12
Membersoumyasg28-Oct-06 19:12 
GeneralGood, but... Pin
casperOne10-Jun-04 3:15
MembercasperOne10-Jun-04 3:15 
GeneralRe: Good, but... Pin
dgiljr10-Jun-04 5:32
Memberdgiljr10-Jun-04 5:32 
GeneralRe: Good, but... Pin
casperOne10-Jun-04 7:21
MembercasperOne10-Jun-04 7:21 
GeneralRe: Good, but... Pin
dgiljr10-Jun-04 9:32
Memberdgiljr10-Jun-04 9:32 
GeneralRe: Good, but... Pin
casperOne10-Jun-04 9:52
MembercasperOne10-Jun-04 9:52 
GeneralRe: Good, but... Pin
Mark Abela10-Jun-04 13:21
MemberMark Abela10-Jun-04 13:21 
Diego,

I think you should be congratulated for, as we say in the land of OZ, "having a go". This topic is of interest to me and is far from "pointless" when you have to interact with existing systems.

I took a different approach to resolve the moving field issue, using individual Marshal calls and an enum to define the field positions.

public class SomePacket<br />
{<br />
enum Fields {<br />
	PktLength,<br />
	PktLenghByte2,		// Pad out the length with additional fields<br />
	Command,<br />
	CommandByte2,<br />
	CommandByte3,<br />
	CommandByte4,<br />
	SmallIntData,<br />
	SmallIntDataByte2,<br />
	Last<br />
}<br />
<br />
private Int16 m_smallData;<br />
<br />
public int ToBuffer(Byte [] buf, int offset /* offset to start writing buffer*/)<br />
{<br />
	Marshal.WriteInt16(buf, offset + (int)Fields.PktLength], (int_Fields.Last);<br />
	Marshal.WriteInt32(buf, offset + (int)Fields.Command], 1234);<br />
	Marshal.WriteInt16(buf, offset + (int)Fields. SmallIntData], m_smallData);<br />
}<br />
} 

Another issue with you approach is the extra marshal into a string, what happens is the data contains a null, not to mention the overhead of an extra copy.

Cheers,
Mark Abela



Mark Abela
Software Engineer
Inner Range
GeneralRe: Good, but... Pin
dgiljr11-Jun-04 4:57
Memberdgiljr11-Jun-04 4:57 
GeneralRe: Good, but... Pin
Mark Abela14-Jun-04 12:42
MemberMark Abela14-Jun-04 12:42 
GeneralRe: Good, but... Pin
dgiljr15-Jun-04 4:46
Memberdgiljr15-Jun-04 4: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.