November 18, 2018
Hot Topics:

Visual C++ Orcas Marshaling Library

  • August 2, 2007
  • By Nick Wienholt
  • Send Email »
  • More Articles »

Going the other way is slightly more complex because of the memory management requirements of C-style strings. The SecureString to wchar_t* conversion requires the use of contexts; this means that a template specialization derived from context_node_base needs to be used. The actual class is reasonably simple. The actual conversion is handled by the constructor, and a destructor is also present to handle the clean-up of the data once the marshal_context class that is holding the context_node_base-derived object is deleted. A Finalize method is also declared (which the destructor actually forwards to in this case) to handle memory clean-up via the .NET finalizer queue if destruction is missed.

ref class msclr::interop::context_node<wchar_t*,
   System::Security::SecureString^> :
   public context_node_base
   System::IntPtr _ip;

   context_node(wchar_t*& _to_object,
      System::Security::SecureString^ _from_object)
      _ip = System::Runtime::InteropServices::Marshal::
         SecureStringToGlobalAllocUnicode (_from_object);
      _to_object = static_cast<wchar_t*>(_ip.ToPointer());


      if(_ip != System::IntPtr::Zero)

The compiler will automatically detect an attempt to use context-free marshaling with a conversion that requires marshaling. If the following code is compiled:

System::Security::SecureString^ mySecureString =
wchar_t* c_style_string1 =

the compiler will produce the following error:

C:\Program Files\Microsoft Visual Studio 9.0\VC\include\msclr\
   marshal.h(200) : error C4996: 'msclr::interop::
   This conversion requires a marshal_context. Please use a
   marshal_context for this conversion.

      _To_Type=wchar_t *,
      _From_Type=System::Security::SecureString ^
   C:\Program Files\Microsoft Visual Studio 9.0\VC\include\msclr\
      marshal.h(191) : see declaration of 'msclr::interop::
      _To_Type=wchar_t *,
      _From_Type=System::Security::SecureString ^
   .\myFile.cpp(19) : see reference to function template
      instantiation '_To_Type msclr::interop::marshal_as
      (const _From_Type &)' being compiled
      _To_Type=wchar_t *,
      _From_Type=System::Security::SecureString ^

This compiler-enforcement of the use of context-based marshaling when required will serve to prevent any inadvertent memory leaks.


C++ Marshaling Library provides a user-friendly, extensible, one-stop-shop for going between native and managed types. By using inline templates, the conversions achieve the greatest possible performance as the correct conversion routine is identified and substituted at compile time, eliminating the potentially expensive lookups and type-safety checks that runtime-based marshaling can impose. The C++ Marshaling Library is extremely easy to use and extend, and by building custom conversion routines on top of the Marshaling Library, developers provide routines that are easy to find and organise in a large code base, and have a familiar syntax that other developers are more likely to use over stand-alone conversion routines, promoting code re-use.

About the Author

Nick Wienholt is a Windows and .NET consultant based in Sydney, Australia. He has worked on a variety of IT projects over the last decade and continues to stay involved in the developer community. Nick is the co-founder and president of the Sydney Deep .NET User group, writes technical articles for Pinnacle Publishing and the Microsoft Developer Network, and is a participant in many .NET-related newsgroups. Nick's most recent book is Maximizing .NET Performance.

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.


Thanks for your registration, follow us on our social networks to keep up-to-date