1 /***
2 *marshal.h
3 *
4 *           Copyright (c) Microsoft Corporation. All rights reserved.
5 *
6 *Purpose:     Marshalling classes
7 *
8 *           [Public]
9 *
10 ****/
11
12 #pragma once
13
14 #ifndef _INC_MSCLR_MARSHAL
15 #define _INC_MSCLR_MARSHAL
16
17 #using <mscorlib.dll>
18 #using <system.dll>
19
20 #include <sal.h>
21 #include <windows.h>
22 #include <vcclr.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <memory>
27
28 #pragma comment(lib, "comsupp.lib")
29 #pragma comment(lib, "oleaut32.lib")
30
31 #pragma warning(push)
32 #pragma warning(error : 4996)
33
34 namespace msclr{
35     namespace interop{
36
37 namespace details
38 {
39 #define _EXCEPTION_GREATER_THAN_INT_MAX "Size of string exceeds INT_MAX."
40 #define _EXCEPTION_NULLPTR "NULLPTR is not supported for this conversion."
41 #define _EXCEPTION_MB2WC "Conversion from MultiByte to WideChar failed.  Please check the content of the string and/or locale settings."
42 #define _EXCEPTION_WC2MB "Conversion from WideChar to MultiByte failed.  Please check the content of the string and/or locale settings."
43
44     inline __checkReturn size_t GetAnsiStringSize(System::String^ _str)
45     {
46         size_t _size = 0;
47         cli::pin_ptr<const wchar_t> _pinned_ptr = PtrToStringChars(_str);
48         
49         _size = ::WideCharToMultiByte(CP_THREAD_ACP, WC_NO_BEST_FIT_CHARS, _pinned_ptr, _str->Length, NULL, 0, NULL,NULL);
50         if (_size == 0 && _str->Length != 0)
51         {
52             throw gcnew System::ArgumentException(_EXCEPTION_WC2MB);
53         }
54         // adding 1 for terminating nul
55         _size+=1;
56         return _size;
57     }
58
59     inline void WriteAnsiString(__out_ecount_full_z(_size) char* _buf, size_t _size, System::String^ _str)
60     {
61         cli::pin_ptr<const wchar_t> _pinned_ptr = PtrToStringChars(_str);
62         
63         //checking for overflow
64         if (_size > INT_MAX)
65         {
Lines 66 ... 371 are skipped.
372             _to_object = _ptr;
373         }
374
375         ~context_node()
376         {
377             this->!context_node();
378         }
379
380     protected:
381         !context_node()
382         {
383             if(_ptr != NULL)
384                 delete [] _ptr;
385         }
386 };
387
388
389 template<>
390 ref class context_node<const wchar_t*, System::String^> :
391     public context_node_base
392 {
393     private:
394         System::IntPtr _ip;
395     public:
396         context_node(__out_z const wchar_t*& _to_object, System::String^ _from_object) 
397         {
398             _ip = System::Runtime::InteropServices::Marshal::StringToHGlobalUni (_from_object);
399             _to_object = static_cast<wchar_t*>(_ip.ToPointer());
400         }
401
402         ~context_node()
403         {
404             this->!context_node();
405         }
406
407     protected:
408         !context_node()
409         {
410             if(_ip != System::IntPtr::Zero)
411                 System::Runtime::InteropServices::Marshal::FreeHGlobal(_ip);
412         }
413 };
414
415     } //namespace interop
416 } //namespace msclr
417
418 #pragma warning(pop) // error:4996
419 #endif // _INC_MSCLR_MARSHAL
420