STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
marshal_atl.h
Go to the documentation of this file.
1 /***
2 *marshal_atl.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_ATL
15 #define _INC_MSCLR_MARSHAL_ATL
16 
17 #include <atlsafe.h>
18 #include <atlbase.h>
19 #include <atlstr.h>
20 #include <vcclr.h>
21 #include <atlcomcli.h>
22 
23 #include <msclr\marshal.h>
24 
25 namespace msclr{
26  namespace interop{
27 
28 template <>
29 inline System::String^ marshal_as(const CComBSTR& _from_object)
30 {
31  if (_from_object.m_str == NULL)
32  {
33  return nullptr;
34  }
35  // Using PtrToStringBSTR here instead of marshal_as<String^, BSTR>() because we want to perserve the embedded NULLs
36  return System::Runtime::InteropServices::Marshal::PtrToStringBSTR(System::IntPtr(_from_object.m_str));
37 }
38 
39 template <>
40 inline CComBSTR marshal_as(System::String^ const& _from_object)
41 {
42  if (_from_object == nullptr)
43  {
44  return CComBSTR(static_cast<const wchar_t*>(NULL));
45  }
46  if(_from_object->Length == 0)
47  {
48  return CComBSTR(static_cast<const wchar_t*>(L""));
49  }
50 
51  cli::pin_ptr<const wchar_t> _pinned_ptr = PtrToStringChars(_from_object);
52  return CComBSTR( _from_object->Length, static_cast<const wchar_t *>(_pinned_ptr));
53 }
54 
55 template <>
56 inline System::String^ marshal_as(const CStringA& _from_obj)
57 {
58  return details::InternalAnsiToStringHelper(_from_obj.operator LPCSTR(), _from_obj.GetLength());
59 }
60 
61 template <>
62 inline System::String^ marshal_as(const CStringW& _from_obj)
63 {
64  // this will perserve the embedded nulls
65  return details::InternalUnicodeToStringHelper(_from_obj.operator LPCWSTR(), _from_obj.GetLength());
66 }
67 
68 template <>
69 inline CStringA marshal_as(System::String^ const & _from_obj)
70 {
71  if (_from_obj == nullptr)
72  {
73  throw gcnew System::ArgumentNullException(_EXCEPTION_NULLPTR);
74  }
75  CStringA _to_obj;
76  size_t _size = details::GetAnsiStringSize(_from_obj);
77 
78  //checking for overflow
79  if (_size > INT_MAX)
80  {
81  throw gcnew System::ArgumentOutOfRangeException(_EXCEPTION_NULLPTR);
82  }
83 
84  int _length = static_cast<int>(_size)-1;
85 
86  //GetBuffer() throws, so we don't need to check for NULL
87  char* _dest_buf = _to_obj.GetBuffer(_length);
88 
89  details::WriteAnsiString(_dest_buf, _size, _from_obj);
90  _to_obj.ReleaseBuffer(_length); // We don't want to include the NULL. This call will set the length to _length
91 
92  return _to_obj;
93 }
94 
95 template <>
96 inline CStringW marshal_as(System::String^ const & _from_obj)
97 {
98  if (_from_obj == nullptr)
99  {
100  throw gcnew System::ArgumentNullException(_EXCEPTION_NULLPTR);
101  }
102  cli::pin_ptr<const wchar_t> _pinned_ptr = PtrToStringChars(_from_obj);
103  return CStringW(static_cast<const wchar_t *>(_pinned_ptr), _from_obj->Length);
104 }
105 
106  } //namespace interop
107 } //namespace msclr
108 
109 #endif /* _INC_MSCLR_MARSHAL_ATL */
#define _EXCEPTION_NULLPTR
Definition: marshal.h:39
Definition: appdomain.h:36
_Check_return_ size_t GetAnsiStringSize(System::String^_str)
Definition: marshal.h:78
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
_To_Type marshal_as(_In_z_ const char _from_object[])
Definition: marshal.h:187
void WriteAnsiString(_Out_writes_all_(_size) _Post_z_ char *_buf, size_t _size, System::String^_str)
Definition: marshal.h:93
#define INT_MAX
Definition: limits.h:40
System::String InternalUnicodeToStringHelper(_In_reads_z_(_count+1) const wchar_t *_src, size_t _count)
Definition: marshal.h:165