Click here to Skip to main content
Licence 
First Posted 9 Jun 2004
Views 69,126
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  
Hi guys.
I'm trying to write a method:
in the managed part it should preffereabley receive a string array, but in not, a string ref would be fine, the string should change in the unmanged part (allocation and stuff).
something like that:
c#:
{
string s = "";
updateString(ref s); (the c++ method);
console.writeLn(s);
Marshal.FreeBstr(Marshal.StringToBSTR(s));
}
c++:
void updateString(BSTR * s){
*s = SysAllocString(L"whatever");
//or:
Cstring cstring = ""whatever";
*s = cstring.AllocSysString();
}
 
In both ways, the data gets to the managed part ok, but the memory isn't freed, not entirely anyways (after checking the Task manager and looping over the function call for a large number of iterations)
 
What am I missing??! Confused | :confused:
 
Thanks
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  
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
Web02 | 2.5.120529.1 | Last Updated 11 Jun 2004
Article Copyright 2004 by dgiljr
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid