Click here to Skip to main content
Licence 
First Posted 9 Jun 2004
Views 69,085
Bookmarked 15 times

How to copy a String into a struct using C#

By | 10 Jun 2004 | Article
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

About the Author

dgiljr

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMarshalling BSTR * Pinmemberburatino2221:48 4 Jun '07  
GeneralMixed structures PinmemberMancunian3:00 21 Apr '06  
QuestionIs it possible to create a function of this? PinsussAnonymous17:37 7 Apr '05  
AnswerRe: Is it possible to create a function of this? Pinmemberdgiljr4:16 8 Apr '05  
Absolutely, You can create global strings and functions in an internal class as static types and then create a static reference to that class. That is the only way that I know of that can simulate a C++ header file that could contain global variables. If this doesn't help send me some pseudo code for your situation and what you want to achieve and then I'll see if I could whip it up for you.

 
Diego
GeneralRe: Is it possible to create a function of this? PinsussAnonymous15:08 26 Apr '05  
Generalpointless Pinmemberleppie7:12 10 Jun '04  
GeneralRe: pointless Pinmemberdgiljr8:35 10 Jun '04  
GeneralRe: pointless Pinmembersoumyas_v18:12 28 Oct '06  
GeneralGood, but... PinmembercasperOne2:15 10 Jun '04  
GeneralRe: Good, but... Pinmemberdgiljr4:32 10 Jun '04  
GeneralRe: Good, but... PinmembercasperOne6:21 10 Jun '04  
GeneralRe: Good, but... Pinmemberdgiljr8:32 10 Jun '04  
GeneralRe: Good, but... PinmembercasperOne8:52 10 Jun '04  
GeneralRe: Good, but... PinmemberMark Abela12:21 10 Jun '04  
GeneralRe: Good, but... Pinmemberdgiljr3:57 11 Jun '04  
GeneralRe: Good, but... PinmemberMark Abela11:42 14 Jun '04  
GeneralRe: Good, but... Pinmemberdgiljr3:46 15 Jun '04  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120528.1 | Last Updated 11 Jun 2004
Article Copyright 2004 by dgiljr
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid