14 #ifndef _INC_MSCLR_MARSHAL
15 #define _INC_MSCLR_MARSHAL
27 #pragma comment(lib, "comsupp.lib")
28 #pragma comment(lib, "oleaut32.lib")
31 #pragma warning(error : 4996)
38 #define _EXCEPTION_GREATER_THAN_INT_MAX "Size of string exceeds INT_MAX."
39 #define _EXCEPTION_NULLPTR "NULLPTR is not supported for this conversion."
40 #define _EXCEPTION_MB2WC "Conversion from MultiByte to WideChar failed. Please check the content of the string and/or locale settings."
41 #define _EXCEPTION_WC2MB "Conversion from WideChar to MultiByte failed. Please check the content of the string and/or locale settings."
83 _size = ::WideCharToMultiByte(CP_THREAD_ACP, WC_NO_BEST_FIT_CHARS, _pinned_ptr, _str->Length,
NULL, 0,
NULL,
NULL);
84 if (_size == 0 && _str->Length != 0)
103 size_t _written = ::WideCharToMultiByte(CP_THREAD_ACP, WC_NO_BEST_FIT_CHARS, _pinned_ptr, _str->Length, _buf, static_cast<int>(_size) ,
NULL,
NULL);
104 if( _written >= _size ||
105 (_written == 0 && _size != 1))
109 _buf[_written] =
'\0';
119 _size = ::MultiByteToWideChar(CP_THREAD_ACP, 0, _str, static_cast<int>(_count),
NULL, 0);
121 if (_size == 0 && _count != 0)
138 size_t _written = ::MultiByteToWideChar(CP_THREAD_ACP, 0, _src, static_cast<int>(_count), _dest, static_cast<int>(_size));
139 if( _written >= _size ||
140 (_written == 0 && _size != 1))
144 _dest[_written] = L
'\0';
150 if (_size >
INT_MAX || _size <=0 )
157 throw gcnew System::InsufficientMemoryException();
162 return gcnew System::String(_wchar_buf.
get(), 0,
static_cast<int>(_size)-1);
172 return gcnew System::String(_src, 0 ,static_cast<int>(_count));
180 ref class context_node_base;
182 template<
class _To_Type,
class _From_Type>
185 template <
class _To_Type,
class _From_Type,
189 template<
class _To_Type>
192 const char* _ptr = _from_object;
193 return marshal_as<_To_Type, const char *>(_ptr);
196 template<
class _To_Type>
199 const wchar_t* _ptr = _from_object;
200 return marshal_as<_To_Type, const wchar_t*>(_ptr);
203 ref class marshal_context;
205 template <
class _To_Type,
class _From_Type>
206 inline _To_Type
marshal_as(
const _From_Type&);
212 template <
class _To_Type,
class _From_Type>
216 __declspec(deprecated(
"This conversion is not supported by the library or the header file needed for this conversion is not included. Please refer to the documentation on 'How to: Extend the Marshaling Library' for adding your own marshaling method."))
217 static _To_Type
marshal_as(const _From_Type& _from_object)
219 return _This_conversion_is_not_supported;
223 template <
class _To_Type,
class _From_Type>
227 __declspec(deprecated(
"This conversion requires a marshal_context. Please use a marshal_context for this conversion."))
228 static _To_Type
marshal_as(const _From_Type& _from_object)
230 return _This_conversion_requires_a_context;
234 template <
class _To_Type,
class _From_Type>
247 static const bool _Needs_Context=
true;
250 template<
class _To_Type,
class _From_Type>
254 static const bool _Needs_Context=
false;
261 System::Collections::Generic::LinkedList <Object^> _clean_up_list;
263 template<
class _To_Type,
class _From_Type,
bool _Needs_Context>
266 template<
class _To_Type,
class _From_Type>
272 static inline _To_Type
marshal_as(
const _From_Type& _from, System::Collections::Generic::LinkedList<Object^>% _clean_up_list)
278 _cn^ _obj =
gcnew _cn(_to_object, _from);
279 _clean_up_list.AddLast(_obj);
284 template<
class _To_Type,
class _From_Type>
291 static inline _To_Type
marshal_as(
const _From_Type& _from, System::Collections::Generic::LinkedList<Object^>% _clean_up_list)
294 return ::msclr::interop::marshal_as<_To_Type, _From_Type>(_from);
299 template<
class _To_Type,
class _From_Type>
306 template<
class _To_Type,
class _From_Type>
314 template<
class _To_Type>
321 template<
class _To_Type>
328 template<
class _To_Type>
331 return ::msclr::interop::marshal_as<_To_Type>(_from);
339 for each(Object^ _obj
in _clean_up_list)
361 return marshal_as<System::String^, const char *>(_from_object);
367 if (_from_object ==
NULL)
371 return gcnew System::String(_from_object);
379 return marshal_as<System::String^, const wchar_t *>(_from_object);
396 if (_from_object ==
nullptr)
407 throw gcnew System::InsufficientMemoryException();
438 _ip = System::Runtime::InteropServices::Marshal::StringToHGlobalUni (_from_object);
439 _to_object =
static_cast<wchar_t*
>(_ip.ToPointer());
450 if(_ip != System::IntPtr::Zero)
451 System::Runtime::InteropServices::Marshal::FreeHGlobal(_ip);
458 #pragma warning(pop) // error:4996
char_buffer & operator=(const char_buffer &)
#define NULL
Definition: vcruntime.h:236
System::String marshal_as(_In_z_ wchar_t *const &_from_object)
Definition: marshal.h:377
T * release()
Definition: marshal.h:63
#define _EXCEPTION_GREATER_THAN_INT_MAX
Definition: marshal.h:38
context_node(_Outref_ _Post_z_ const wchar_t *&_to_object, System::String^_from_object)
Definition: marshal.h:436
Definition: marshal.h:264
#define _Out_writes_all_(size)
Definition: sal.h:354
_Check_return_ size_t GetUnicodeStringSize(_In_reads_z_(_count+1) const char *_str, size_t _count)
Definition: marshal.h:112
_In_reads_bytes_(_Size) void const *_Src
_Check_return_ size_t GetAnsiStringSize(System::String^_str)
Definition: marshal.h:78
Definition: marshal.h:187
_To_Type marshal_as(_In_z_ const char _from[])
Definition: marshal.h:315
#define _EXCEPTION_WC2MB
Definition: marshal.h:41
static _To_Type marshal_as(const _From_Type &_from, System::Collections::Generic::LinkedList< Object^>%_clean_up_list)
Definition: marshal.h:291
System::String InternalAnsiToStringHelper(_In_reads_z_(_count+1) const char *_src, size_t _count)
Definition: marshal.h:147
__const_Char_ptr PtrToStringChars(__const_String_handle s)
Definition: vcclr.h:40
marshal_context()
Definition: marshal.h:334
Definition: marshal.h:244
#define _In_reads_z_(size)
Definition: sal.h:320
#define _Check_return_
Definition: sal.h:554
#define _In_z_
Definition: sal.h:310
static _To_Type marshal_as(const _From_Type &_from, System::Collections::Generic::LinkedList< Object^>%_clean_up_list)
Definition: marshal.h:272
T * _ptr
Definition: marshal.h:75
_To_Type marshal_as(_From_Type^_from)
Definition: marshal.h:307
~context_node()
Definition: marshal.h:416
T * get() const
Definition: marshal.h:58
_To_Type marshal_as(_In_z_ const char _from_object[])
Definition: marshal.h:190
#define false
Definition: stdbool.h:16
void WriteUnicodeString(_Out_writes_all_(_size) _Post_z_ wchar_t *_dest, size_t _size, _In_reads_bytes_(_count) const char *_src, size_t _count)
Definition: marshal.h:130
void WriteAnsiString(_Out_writes_all_(_size) _Post_z_ char *_buf, size_t _size, System::String^_str)
Definition: marshal.h:93
_To_Type marshal_as(_In_z_ const wchar_t _from[])
Definition: marshal.h:322
unsigned short wchar_t
Definition: sourceannotations.h:25
Definition: marshal.h:257
#define _EXCEPTION_MB2WC
Definition: marshal.h:40
__declspec(deprecated("This conversion is not supported by the library or the header file needed for this conversion is not included. Please refer to the documentation on 'How to: Extend the Marshaling Library' for adding your own marshaling method.")) static _To_Type marshal_as(const _From_Type &_from_object)
Definition: marshal.h:216
#define _Outref_
Definition: sal.h:487
char * _ptr
Definition: marshal.h:391
_To_Type marshal_as(System::IntPtr _from)
Definition: marshal.h:329
Definition: marshal.h:183
~marshal_context()
Definition: marshal.h:337
__declspec(deprecated("This conversion requires a marshal_context. Please use a marshal_context for this conversion.")) static _To_Type marshal_as(const _From_Type &_from_object)
Definition: marshal.h:227
char_buffer(size_t _size)
Definition: marshal.h:48
~char_buffer()
Definition: marshal.h:53
_To_Type marshal_as(const _From_Type &_from)
Definition: marshal.h:300
#define INT_MAX
Definition: limits.h:35
#define _Outref_result_maybenull_
Definition: sal.h:488
#define _Post_z_
Definition: sal.h:688
~context_node()
Definition: marshal.h:442
#define true
Definition: stdbool.h:17
context_node(_Outref_result_maybenull_ _Post_z_ const char *&_to_object, System::String^_from_object)
Definition: marshal.h:393
System::IntPtr _ip
Definition: marshal.h:434
System::String InternalUnicodeToStringHelper(_In_reads_z_(_count+1) const wchar_t *_src, size_t _count)
Definition: marshal.h:165