1 /***
2 *marshal_windows.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_WINDOWS
15 #define _INC_MSCLR_MARSHAL_WINDOWS
16
17 #include <msclr\marshal.h>
18 #include <windows.h>
19 #include <comutil.h> 
20
21 #using <mscorlib.dll>
22
23 namespace msclr{
24     namespace interop{
25
26 //--------------------------------------------------------------------------------
27 // Context-free conversions templates specialization for windows types
28 //--------------------------------------------------------------------------------
29
30 //
31 // for BSTR -> System::String^, please see wchar_t* -> String^ in marshal.h
32 //
33
34 template <>
35 inline System::String^ marshal_as(const _bstr_t & _from_object)
36 {
37     BSTR _bstr = _from_object.copy(false);
38     if (_bstr == NULL)
39     {
40         return nullptr;
41     }
42     // Using PtrToStringBSTR here instead of marshal_as<String^, BSTR>() because we want to perserve the embedded NULLs
43     return System::Runtime::InteropServices::Marshal::PtrToStringBSTR(System::IntPtr(_bstr));
44 }
45
46 template <>
47 inline _bstr_t marshal_as(System::String^ const & _from_object)
48 {
49     if (_from_object == nullptr)
50     {
51         return _bstr_t(static_cast<BSTR>(NULL));
52     }
53
54     _bstr_t _ret_bstr_t;
55     cli::pin_ptr<const wchar_t> _pinned_ptr = PtrToStringChars(_from_object);
56     _ret_bstr_t.Attach(::SysAllocStringLen(_pinned_ptr, _from_object->Length));
57     if (!_ret_bstr_t)
58     {
59         throw gcnew System::OutOfMemoryException();
Lines 60 ... 69 are skipped.
70 template <class _To_Type>
71 inline _To_Type marshal_as(System::IntPtr _from_object);
72
73 template <>
74 inline HANDLE marshal_as(System::IntPtr _from_object)
75 {
76     return static_cast<HANDLE>(_from_object.ToPointer());
77 }
78
79 //--------------------------------------------------------------------------------
80 // Context conversion templates specialization for windows types
81 //--------------------------------------------------------------------------------
82
83 template<>
84 ref class context_node<BSTR, System::String^> :
85     public context_node_base
86 {
87     private:
88         System::IntPtr _ip;
89     public:
90         context_node(BSTR& _to_object, System::String^ from) 
91         {
92             _ip = System::Runtime::InteropServices::Marshal::StringToBSTR (from);
93             _to_object = static_cast<BSTR>(_ip.ToPointer());
94         }
95
96         ~context_node()
97         {
98             this->!context_node();
99         }
100
101     protected:
102         !context_node()
103         {
104             if(_ip != System::IntPtr::Zero)
105                 System::Runtime::InteropServices::Marshal::FreeBSTR(_ip);
106         }
107 };
108
109     } //namespace interop
110 } //namespace msclr
111
112 #endif // _INC_MSCLR_MARSHAL_WINDOWS
113