Overview of Marshaling in C++

http://msdn.microsoft.com/en-us/library/bb384865(VS.90).aspx

Visual C++ Libraries
Overview of Marshaling in C++

In mixed mode, you sometimes must marshal your data between native and managed types. Visual Studio 2008 introduced the marshaling library to help you marshal and convert data in a simple way.

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 type

To type

Marshal method

Include file

System::String^

const char *

marshal_context

marshal.h

const char *

System::String^

marshal_as

marshal.h

char *

System::String^

marshal_as

marshal.h

System::String^

const wchar_t*

marshal_context

marshal.h

const wchar_t *

System::String^

marshal_as

marshal.h

wchar_t *

System::String^

marshal_as

marshal.h

System::IntPtr

HANDLE

marshal_as

marshal_windows.h

HANDLE

System::IntPtr

marshal_as

marshal_windows.h

System::String^

BSTR

marshal_context

marshal_windows.h

BSTR

System::String^

marshal_as

marshal.h

System::String^

bstr_t

marshal_as

marshal_windows.h

bstr_t

System::String^

marshal_as

marshal_windows.h

System::String^

std::string

marshal_as

marshal_cppstd.h

std::string

System::String^

marshal_as

marshal_cppstd.h

System::String^

std::wstring

marshal_as

marshal_cppstd.h

std::wstring

System::String^

marshal_as

marshal_cppstd.h

System::String^

CStringT<char>

marshal_as

marshal_atl.h

CStringT<char>

System::String^

marshal_as

marshal_atl.h

System::String^

CStringT<wchar_t>

marshal_as

marshal_atl.h

CStringT<wchar_t>

System::String^

marshal_as

marshal_atl.h

System::String^

CComBSTR

marshal_as

marshal_atl.h

CComBSTR

System::String^

marshal_as

marshal_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 Note:

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

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 .

In earlier versions, you could marshal data by using Platform Invoke . For more information about PInvoke , see Calling Native Functions from Managed Code .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值