Marshaling Library in Orcas

Hi, I am Weidong Huang, an SDET on the VC++ library team.  I joined MS six months ago and have had a wonderful experience so far. In this post, I would like to talk about a new feature of Orcas, the Marshaling Library, which is also the first feature I’ve worked on at MS.

What is marshaling library?

The marshaling library helps to marshal data types from managed to native and vice-versa in a simple and straightforward manner. 

Customers programming in mixed mode are often faced with the need to marshal data types.  Current solutions, like the runtime marshaling mechanisms, are complex and confusing. Thus, the marshaling library aims to provide an easier approach.

Currently, the marshaling library comes with two template functions  – one requires a marshal_context and the other does not.  I will explain the marshal_context a little later.  Both functions have a similar structure: “To_Type marshal_as<To_Type>(From%)”.   You use the marshaling library in the following ways and both functions return the result that you want.

(1)    marshal_as<To_Type>(From %);

(2)    marshal_context ctx;  ctx.marshal<To_Type>(From %);

To me, it is really impressive and easy to remember because it looks like just the conversions we often use, such as “dynamic_cast<To_Type>(From)” and “static_cast<To_Type>(From)”. 

Currently, we support 22 commonly used conversions. Most of these are conversions between managed type string to native string.  Please refer to the marshaling library specification for the details.

What is marshal_context?

It would be better if we only needed to remember the function that does not require marshal_context. So why we need marshal_context and what kinds of conversions need marshal_context? 

Basically, a marshal_context is a list of data nodes which contain the converted result data.  Marshal_context takes care of the whole life of these data, from creating to destroying.  You would have to remember to free the result manually if it weren’t for the marshal_context, for example when you convert a System::String^ to const char*, which is painful to most programmers.

Not all conversions need marshal_context.  The marshal_context only comes out when you marshal from managed to native data types and the native type you are converting to does not have a destructor for automatic clean up.  The marshaling context will help you destroy the allocated native data type in its destructor. 

In case you don’t want to remember which conversion requires context and which doesn’t (or you like typing), you can always use the second one for all the conversions and the library will automatically direct it to the first one whenever necessary.

Tips of using marshaling library

·         Feel free to use marshaling library instead of runtime marshal class.  It is easier, safer and there are no performance drops.

·         Feel free to extend the library.  The library is designed be extensible by customers with conversions not supported by the library.

·         Don’t use the conversion result after the context is out of range.  Since all the results are managed by marshal_context,  they are only valid until the context is deleted.  To save any marshaled values, you must copy the values to your own variables.

·         Use multiple marshal_contexts for large amount conversions when the memory of the context grows to cause frequent page swaps in your machine.

·         Reference the marshaling library specification online for the detailed usage.

Any suggestions you might have about marshaling library will be a great help to us and highly appreciated.  Feel free to ask questions about the usage and anything about the marshaling library!

Thanks
Weidong Huang
Visual C++ Library QA Team