Search Wiki:
Resource Page Description
An extension (template specialization) to marshal_as<> library of Visual C++ 2008, to convert Unicode string arrays between managed (CLR) and native (STL/C++) code.


This project shows how to specialize marshal_as<> template library of Visual Studio 2008, to allow marshaling of list of (Unicode) strings between managed and native worlds.

The main file is the header: marshal_stringarray.h
This file contains the marshal_as aforementioned template specialization, and it should be included in projects that requires this kind of marshaling.

There are several options to store list of strings in C++.
For this project, I chose to use std :: wstring as Unicode (UTF-16) string class, and std :: vector as container of strings.
Both classes ( std :: wstring and std :: vector) are from STL.

Using this marshaling is as simple as this:

// Marshal from STL string array to CLR string array
ClrStringArray^ clrNames = marshal_as< ClrStringArray^ >( names );

// Marshal from CLR string array to STL string array
StlStringArray stlNames = marshal_as< StlStringArray >( clrNames );


Extending and "crafting" the code to make it working with similar classes (like CStringW) should be easy enough.

Giovanni Dicanio
Last edited Jun 4 2008 at 8:59 PM  by GiovanniD, version 4
Comments
mobydisk wrote  Dec 24 2008 at 8:13 PM  
There is already a method to do this marshal_cppstd.h:
#include <msclr\marshal_cppstd.h>
std::string stlNames = msclr::interop::marshal_as<std::string>( names );
For more information, check out:
http://msdn.microsoft.com/en-us/library/bb384865.aspx

GiovanniD wrote  Jan 1 2009 at 2:12 PM  
mobydisk: the code I proposed is to marshal string *arrays*, not simple strings.

Updating...
Page view tracker