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

How to copy a String into a struct using C#

By dgiljr | 10 Jun 2004
How to copy a String into a struct using C#
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
2.20/5 - 29 votes
μ 2.20, σa 2.97 [?]

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 * Pinmemberburatino2222:48 4 Jun '07  
GeneralMixed structures PinmemberMancunian4:00 21 Apr '06  
QuestionIs it possible to create a function of this? PinsussAnonymous18:37 7 Apr '05  
AnswerRe: 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    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.120210.1 | Last Updated 11 Jun 2004
Article Copyright 2004 by dgiljr
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid