C++/CLI의 마샬링 개요

혼합 모드에서는 때때로 네이티브 형식과 관리되는 형식 간에 데이터를 마샬링해야 합니다. 마샬링 라이브러리사용하면 간단한 방법으로 데이터를 마샬링하고 변환할 수 있습니다. 마샬링 라이브러리는 함수 집합과 공통 형식에 marshal_context 대해 마샬링을 수행하는 클래스로 구성됩니다. 라이브러리는 Visual Studio 버전의 include/msclr 디렉터리에 있는 다음 헤더에 정의됩니다.

헤더 설명
marshal.h marshal_context 클래스 및 컨텍스트 없는 마샬링 함수
marshal_atl.h ATL 형식 마샬링에 대한 함수
marshal_cppstd.h 표준 C++ 형식을 마샬링하는 함수
marshal_windows.h Windows 형식 마샬링에 대한 함수

msclr 폴더의 기본 경로는 사용 중인 버전과 빌드 번호에 따라 다음과 같습니다.

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

marshal_context 클래스를 사용하거나 사용하지 않고 마샬링 라이브러리를 사용할 수 있습니다. 일부 변환에는 컨텍스트가 필요합니다. 다른 변환은 marshal_as 함수를 사용하여 구현할 수 있습니다. 다음 표에서는 지원되는 현재 변환, 컨텍스트가 필요한지 여부 및 포함해야 하는 마샬링 파일을 나열합니다.

From type 입력하려면 Marshal 메서드 파일 포함
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

마샬링하려면 관리되는 데이터 형식에서 네이티브 데이터 형식으로 마샬링하는 경우에만 컨텍스트가 필요하며 변환하는 네이티브 형식에는 자동 클린 대한 소멸자가 없습니다. 마샬링 컨텍스트는 소멸자에서 할당된 네이티브 데이터 형식을 삭제합니다. 따라서 컨텍스트가 필요한 변환은 컨텍스트가 삭제될 때까지만 유효합니다. 마샬링된 값을 저장하려면 값을 고유한 변수에 복사해야 합니다.

참고 항목

문자열에 s가 포함된 NULL경우 문자열 마샬링 결과가 보장되지 않습니다. 포함된 NULLs는 문자열이 잘리거나 보존될 수 있습니다.

이 예제에서는 include 헤더 선언에 msclr 디렉터리를 포함하는 방법을 보여줍니다.

#include "msclr\marshal_cppstd.h"

마샬링 라이브러리는 확장 가능하므로 사용자 고유의 마샬링 형식을 추가할 수 있습니다. 마샬링 라이브러리 확장에 대한 자세한 내용은 방법: 마샬링 라이브러리 확장 방법을 참조 하세요.

참고 항목

C++ 지원 라이브러리
방법: 마샬링 라이브러리 확장