-
Notifications
You must be signed in to change notification settings - Fork 47
/
marshal_atl.h
executable file
·109 lines (90 loc) · 3.08 KB
/
marshal_atl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/***
*marshal_atl.h
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose: Marshalling classes
*
* [Public]
*
****/
#pragma once
#ifndef _INC_MSCLR_MARSHAL_ATL
#define _INC_MSCLR_MARSHAL_ATL
#include <atlsafe.h>
#include <atlbase.h>
#include <atlstr.h>
#include <vcclr.h>
#include <atlcomcli.h>
#include <msclr/marshal.h>
namespace msclr{
namespace interop{
template <>
inline System::String^ marshal_as(const CComBSTR& _from_object)
{
if (_from_object.m_str == NULL)
{
return nullptr;
}
// Using PtrToStringBSTR here instead of marshal_as<String^, BSTR>() because we want to perserve the embedded NULLs
return System::Runtime::InteropServices::Marshal::PtrToStringBSTR(System::IntPtr(_from_object.m_str));
}
template <>
inline CComBSTR marshal_as(System::String^ const& _from_object)
{
if (_from_object == nullptr)
{
return CComBSTR(static_cast<const wchar_t*>(NULL));
}
if(_from_object->Length == 0)
{
return CComBSTR(static_cast<const wchar_t*>(L""));
}
cli::pin_ptr<const wchar_t> _pinned_ptr = PtrToStringChars(_from_object);
return CComBSTR( _from_object->Length, static_cast<const wchar_t *>(_pinned_ptr));
}
template <>
inline System::String^ marshal_as(const CStringA& _from_obj)
{
return details::InternalAnsiToStringHelper(_from_obj.operator LPCSTR(), _from_obj.GetLength());
}
template <>
inline System::String^ marshal_as(const CStringW& _from_obj)
{
// this will perserve the embedded nulls
return details::InternalUnicodeToStringHelper(_from_obj.operator LPCWSTR(), _from_obj.GetLength());
}
template <>
inline CStringA marshal_as(System::String^ const & _from_obj)
{
if (_from_obj == nullptr)
{
throw gcnew System::ArgumentNullException(_EXCEPTION_NULLPTR);
}
CStringA _to_obj;
size_t _size = details::GetAnsiStringSize(_from_obj);
//checking for overflow
if (_size > INT_MAX)
{
throw gcnew System::ArgumentOutOfRangeException(_EXCEPTION_NULLPTR);
}
int _length = static_cast<int>(_size)-1;
//GetBuffer() throws, so we don't need to check for NULL
char* _dest_buf = _to_obj.GetBuffer(_length);
details::WriteAnsiString(_dest_buf, _size, _from_obj);
_to_obj.ReleaseBuffer(_length); // We don't want to include the NULL. This call will set the length to _length
return _to_obj;
}
template <>
inline CStringW marshal_as(System::String^ const & _from_obj)
{
if (_from_obj == nullptr)
{
throw gcnew System::ArgumentNullException(_EXCEPTION_NULLPTR);
}
cli::pin_ptr<const wchar_t> _pinned_ptr = PtrToStringChars(_from_obj);
return CStringW(static_cast<const wchar_t *>(_pinned_ptr), _from_obj->Length);
}
} //namespace interop
} //namespace msclr
#endif /* _INC_MSCLR_MARSHAL_ATL */