Share via


This method converts data between native and managed environments.


To_Type marshal_as<To_Type>(
   From_Type input


[in] The value that you want to marshal to a To_Type variable.

Return Value

A variable of type To_Type that is the converted value of input.


This method is a simplified way to convert data between native and managed types. To determine what data types are supported, see Overview of Marshaling in C++. Some data conversions require a context. You can convert those data types by using the marshal_context Class.

If you try to marshal a pair of data types that are not supported, marshal_as will generate an error C4996 at compile time. Read the message supplied with this error for more information. The C4996 error can be generated for more than just deprecated functions. One example of this is trying to marshal a pair of data types that are not supported.

The marshaling library consists of several header files. Any conversion requires only one file, but you can include additional files if you need to for other conversions. To see which conversions are associated with which files, look in the table in Marshaling Overview. Regardless of what conversion you want to do, the namespace requirement is always in effect.

Throws System::ArgumentNullException(_EXCEPTION_NULLPTR) if the input parameter is null.


This example marshals from a const char* to a System::String variable type.

// marshal_as_test.cpp
// compile with: /clr
#include <stdlib.h>
#include <string.h>
#include <msclr\marshal.h>

using namespace System;
using namespace msclr::interop;

int main() {
   const char* message = "Test String to Marshal";
   String^ result;
   result = marshal_as<String^>( message );
   return 0;


Header file: <msclr\marshal.h>, <msclr\marshal_windows.h>, <msclr\marshal_cppstd.h>, or <msclr\marshal_atl.h>

Namespace: msclr::interop

See also

Overview of Marshaling in C++
marshal_context Class