STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
marshal_windows.h
Go to the documentation of this file.
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 namespace msclr{
22  namespace interop{
23 
24 //--------------------------------------------------------------------------------
25 // Context-free conversions templates specialization for windows types
26 //--------------------------------------------------------------------------------
27 
28 //
29 // for BSTR -> System::String^, please see wchar_t* -> String^ in marshal.h
30 //
31 
32 template <>
33 inline System::String^ marshal_as(const _bstr_t & _from_object)
34 {
35  BSTR _bstr = _from_object.copy(false);
36  if (_bstr == NULL)
37  {
38  return nullptr;
39  }
40  // Using PtrToStringBSTR here instead of marshal_as<String^, BSTR>() because we want to perserve the embedded NULLs
41  return System::Runtime::InteropServices::Marshal::PtrToStringBSTR(System::IntPtr(_bstr));
42 }
43 
44 template <>
45 inline _bstr_t marshal_as(System::String^ const & _from_object)
46 {
47  if (_from_object == nullptr)
48  {
49  return _bstr_t(static_cast<BSTR>(NULL));
50  }
51 
52  _bstr_t _ret_bstr_t;
53  cli::pin_ptr<const wchar_t> _pinned_ptr = PtrToStringChars(_from_object);
54  _ret_bstr_t.Attach(::SysAllocStringLen(_pinned_ptr, _from_object->Length));
55  if (!_ret_bstr_t)
56  {
57  throw gcnew System::InsufficientMemoryException();
58  }
59  return _ret_bstr_t;
60 }
61 
62 template <>
63 inline System::IntPtr marshal_as(const HANDLE& _from_object)
64 {
65  return System::IntPtr(_from_object);
66 }
67 
68 template <class _To_Type>
69 inline _To_Type marshal_as(System::IntPtr _from_object);
70 
71 template <>
72 inline HANDLE marshal_as(System::IntPtr _from_object)
73 {
74  return static_cast<HANDLE>(_from_object.ToPointer());
75 }
76 
77 //--------------------------------------------------------------------------------
78 // Context conversion templates specialization for windows types
79 //--------------------------------------------------------------------------------
80 
81 template<>
82 ref class context_node<BSTR, System::String^> :
83  public context_node_base
84 {
85  private:
86  System::IntPtr _ip;
87  public:
88  context_node(BSTR& _to_object, System::String^ from)
89  {
90  _ip = System::Runtime::InteropServices::Marshal::StringToBSTR (from);
91  _to_object = static_cast<BSTR>(_ip.ToPointer());
92  }
93 
95  {
96  this->!context_node();
97  }
98 
99  protected:
101  {
102  if(_ip != System::IntPtr::Zero)
103  System::Runtime::InteropServices::Marshal::FreeBSTR(_ip);
104  }
105 };
106 
107  } //namespace interop
108 } //namespace msclr
109 
110 #endif /* _INC_MSCLR_MARSHAL_WINDOWS */
Definition: comutil.h:144
#define NULL
Definition: vcruntime.h:236
Definition: gcroot.h:43
void Attach(BSTR s)
Definition: comutil.h:597
__const_Char_ptr PtrToStringChars(__const_String_handle s)
Definition: vcclr.h:40
BSTR copy(bool fCopy=true) const
Definition: comutil.h:545
Definition: marshal.h:244
_To_Type marshal_as(_In_z_ const char _from_object[])
Definition: marshal.h:190
System::IntPtr _ip
Definition: marshal_windows.h:86
Definition: marshal.h:183
context_node(BSTR &_to_object, System::String^from)
Definition: marshal_windows.h:88
void * HANDLE
Definition: concrt.h:72
~context_node()
Definition: marshal_windows.h:94