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 template <
class _To_Type,
class _From_Type,
181 bool _Needs_Context = context_node<_To_Type,_From_Type>::_Needs_Context>
186 template<
class _To_Type>
189 const char* _ptr = _from_object;
190 return marshal_as<_To_Type, const char *>(_ptr);
193 template<
class _To_Type>
196 const wchar_t* _ptr = _from_object;
197 return marshal_as<_To_Type, const wchar_t*>(_ptr);
200 template<
class _To_Type,
class _From_Type>
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 &)
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:363
_Check_return_ size_t GetUnicodeStringSize(_In_reads_z_(_count+1) const char *_str, size_t _count)
Definition: marshal.h:112
Definition: appdomain.h:36
_Check_return_ size_t GetAnsiStringSize(System::String^_str)
Definition: marshal.h:78
Definition: marshal.h:182
#define _In_reads_bytes_(size)
Definition: sal.h:327
_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
#define NULL
Definition: crtdbg.h:30
__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:329
#define _Check_return_
Definition: sal.h:563
#define _In_z_
Definition: sal.h:319
unsigned short wchar_t
Definition: crtdefs.h:536
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:187
#define false
Definition: stdbool.h:11
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
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:496
char * _ptr
Definition: marshal.h:391
_To_Type marshal_as(System::IntPtr _from)
Definition: marshal.h:329
Definition: marshal.h:201
~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:40
#define _Outref_result_maybenull_
Definition: sal.h:497
#define _Post_z_
Definition: sal.h:697
~context_node()
Definition: marshal.h:442
#define true
Definition: stdbool.h:12
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
_Check_return_ size_t __cdecl strlen(_In_z_ const char *_Str)
System::String InternalUnicodeToStringHelper(_In_reads_z_(_count+1) const wchar_t *_src, size_t _count)
Definition: marshal.h:165