Skip to content

Latest commit

 

History

History
68 lines (55 loc) · 4.14 KB

overview-of-marshaling-in-cpp.md

File metadata and controls

68 lines (55 loc) · 4.14 KB
descriptiontitlems.datems.topicf1_keywordshelpviewer_keywordsms.assetid
Learn more about: Overview of Marshaling in C++/CLI
Overview of Marshaling in C++
07/12/2019
reference
marshaling
marshalling
Visual C++, marshaling
C++ Support Library, marshaling
marshaling, about marshaling
997dd4bc-5f98-408f-b890-f35de9ce3bb8

Overview of Marshaling in C++/CLI

In mixed mode, you sometimes must marshal your data between native and managed types. The marshaling library helps you marshal and convert data in a simple way. The marshaling library consists of a set of functions and a marshal_context class that perform marshaling for common types. The library is defined in these headers in the include/msclr directory for your Visual Studio edition:

HeaderDescription
marshal.hmarshal_context class and context-free marshaling functions
marshal_atl.hFunctions for marshaling ATL types
marshal_cppstd.hFunctions for marshaling standard C++ types
marshal_windows.hFunctions for marshaling Windows types

The default path for msclr folder is something like this depending on which edition you have and the build number:

C:\\Program Files (x86)\\Microsoft Visual Studio\\Preview\\Enterprise\\VC\\Tools\\MSVC\\14.15.26528\\include\\msclr

You can use the marshaling library with or without a marshal_context Class. Some conversions require a context. Other conversions can be implemented using the marshal_as function. The following table lists the current conversions supported, whether they require a context, and what marshal file you have to include:

From typeTo typeMarshal methodInclude file
System::String^const char *marshal_contextmarshal.h
const char *System::String^marshal_asmarshal.h
char *System::String^marshal_asmarshal.h
System::String^const wchar_t*marshal_contextmarshal.h
const wchar_t *System::String^marshal_asmarshal.h
wchar_t *System::String^marshal_asmarshal.h
System::IntPtrHANDLEmarshal_asmarshal_windows.h
HANDLESystem::IntPtrmarshal_asmarshal_windows.h
System::String^BSTRmarshal_contextmarshal_windows.h
BSTRSystem::String^marshal_asmarshal.h
System::String^bstr_tmarshal_asmarshal_windows.h
bstr_tSystem::String^marshal_asmarshal_windows.h
System::String^std::stringmarshal_asmarshal_cppstd.h
std::stringSystem::String^marshal_asmarshal_cppstd.h
System::String^std::wstringmarshal_asmarshal_cppstd.h
std::wstringSystem::String^marshal_asmarshal_cppstd.h
System::String^CStringT<char>marshal_asmarshal_atl.h
CStringT<char>System::String^marshal_asmarshal_atl.h
System::String^CStringT<wchar_t>marshal_asmarshal_atl.h
CStringT<wchar_t>System::String^marshal_asmarshal_atl.h
System::String^CComBSTRmarshal_asmarshal_atl.h
CComBSTRSystem::String^marshal_asmarshal_atl.h

Marshaling requires a context only 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 destroys the allocated native data type in its destructor. Therefore, conversions that require a context will be valid only until the context is deleted. To save any marshaled values, you must copy the values to your own variables.

Note

If you have embedded NULLs in your string, the result of marshaling the string is not guaranteed. The embedded NULLs can cause the string to be truncated or they might be preserved.

This example shows how to include the msclr directory in an include header declaration:

#include "msclr\marshal_cppstd.h"

The marshaling library is extensible so that you can add your own marshaling types. For more information about extending the marshaling library, see How to: Extend the Marshaling Library.

See also

C++ Support Library
How to: Extend the Marshaling Library

close