CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
marshal.h
1
2#ifndef __aspose_system_marshal_h__
3#define __aspose_system_marshal_h__
4
5#include <memory.h>
6#include <system/exceptions.h>
7#include <fwd.h>
8#include <cstdint>
9
10namespace System { namespace Security { class SecureString; }}
11
12namespace System { namespace Runtime { namespace InteropServices {
13
19{
20public:
27 template<typename container>
28 static void Copy(const IntPtr source, container&& destination, int startIndex, int length) {
29 memcpy(&destination->data()[0]+startIndex, (const void *)source, length*sizeof(destination->data()[0]));
30 }
37 template<typename container>
38 static void Copy(const void * source, container&& destination, int startIndex, int length) {
39 memcpy(&destination->data()[0]+startIndex, source, length*sizeof(destination->data()[0]));
40 }
41
48 template<typename container>
49 static void Copy(const container &source, int startIndex, void * destination, int length) {
50 memcpy(destination, &source->data()[0]+startIndex, length*sizeof(typename container::ValueType));
51 }
58 template<typename container>
59 static void Copy(const container &source, int startIndex, IntPtr destination, int length) {
60 memcpy((void *)destination, &source->data()[0]+startIndex, length*sizeof(typename container::ValueType));
61 }
62
68 template<typename TDelegate>
69 static TDelegate GetDelegateForFunctionPointer(IntPtr ptr)
70 {
71 if (ptr == 0)
72 {
73 throw ArgumentNullException(u"ptr");
74 }
75
76 return TDelegate().connect(reinterpret_cast<typename TDelegate::Function*>(ptr));
77 }
78
83 static void WriteByte(IntPtr ptr, int offset, uint8_t value)
84 {
85 *((uint8_t*)ptr + offset) = value;
86 }
90 static void WriteByte(IntPtr ptr, uint8_t value)
91 {
92 WriteByte(ptr, 0, value);
93 }
94
99 static void WriteInt16(IntPtr ptr, int offset, int16_t value)
100 {
101 *((int16_t*)((uint8_t*)ptr + offset)) = value;
102 }
103
108 static void WriteInt32(IntPtr ptr, int offset, int32_t value)
109 {
110 *((int32_t*)((uint8_t*)ptr + offset)) = value;
111 }
112
117 static void WriteInt64(IntPtr ptr, int offset, int64_t value)
118 {
119 *((int64_t*)((uint8_t*)ptr + offset)) = value;
120 }
121
126 static void WriteIntPtr(IntPtr ptr, int offset, IntPtr value)
127 {
128 *((IntPtr*)((uint8_t*)ptr + offset)) = value;
129 }
130
135 static uint8_t ReadByte(IntPtr ptr, int offset = 0)
136 {
137 return *((uint8_t*)ptr + offset);
138 }
139
144 static int16_t ReadInt16(IntPtr ptr, int offset = 0)
145 {
146 return *((int16_t*)((uint8_t*)ptr + offset));
147 }
148
153 static int32_t ReadInt32(IntPtr ptr, int offset = 0)
154 {
155 return *((int32_t*)((uint8_t*)ptr + offset));
156 }
157
162 static IntPtr ReadIntPtr(IntPtr ptr, int offset = 0)
163 {
164 return *((IntPtr*)((uint8_t*)ptr + offset));
165 }
166
170 static int32_t GetHRForException(const System::Exception& exc)
171 {
172 return exc->get_HResult();
173 }
174
178 static ASPOSECPP_SHARED_API IntPtr SecureStringToGlobalAllocAnsi(const SharedPtr<Security::SecureString>& s);
179
183 static ASPOSECPP_SHARED_API IntPtr SecureStringToGlobalAllocUnicode(const SharedPtr<Security::SecureString>& s);
184
188 static ASPOSECPP_SHARED_API IntPtr AllocHGlobal(int32_t bytes_count);
189
193 static ASPOSECPP_SHARED_API IntPtr AllocHGlobal(IntPtr bytes_count);
194
197 static ASPOSECPP_SHARED_API void FreeHGlobal(IntPtr address);
198
201 static ASPOSECPP_SHARED_API void ZeroFreeGlobalAllocAnsi(IntPtr s);
202
205 static ASPOSECPP_SHARED_API void ZeroFreeGlobalAllocUnicode(IntPtr s);
206
210 static ASPOSECPP_SHARED_API IntPtr StringToHGlobalAnsi(const String& s);
211
215 static ASPOSECPP_SHARED_API IntPtr StringToHGlobalUni(const String& s);
216
220 static ASPOSECPP_SHARED_API IntPtr StringToHGlobalAuto(const String& s);
221
225 static ASPOSECPP_SHARED_API String PtrToStringAnsi(IntPtr ptr);
226
231 static ASPOSECPP_SHARED_API String PtrToStringAnsi(IntPtr ptr, int length);
232
236 static ASPOSECPP_SHARED_API String PtrToStringAuto(IntPtr ptr);
237
242 static ASPOSECPP_SHARED_API String PtrToStringAuto(IntPtr ptr, int length);
243
247 static ASPOSECPP_SHARED_API String PtrToStringUni(IntPtr ptr);
248
253 static ASPOSECPP_SHARED_API String PtrToStringUni(IntPtr ptr, int length);
254
258 static ASPOSECPP_SHARED_API String PtrToStringUTF8(IntPtr ptr);
259
264 static ASPOSECPP_SHARED_API String PtrToStringUTF8(IntPtr ptr, int length);
265};
266
267}}} // namespace System::Runtime::InteropServices
268
269#endif
Template that represents wrapper of exceptions that are derived from Exception class.
Definition: exception.h:118
Provides marshalling implementation. For compatibility with translated code only, as no managed code ...
Definition: marshal.h:19
static void WriteIntPtr(IntPtr ptr, int offset, IntPtr value)
Writes IntPtr to memory.
Definition: marshal.h:126
static void ZeroFreeGlobalAllocAnsi(IntPtr s)
Frees unmanaged string pointer that was allocated using the SecureStringToGlobalAllocAnsi method.
static TDelegate GetDelegateForFunctionPointer(IntPtr ptr)
Converts an unmanaged function pointer to a delegate of a specified type.
Definition: marshal.h:69
static IntPtr StringToHGlobalUni(const String &s)
Copies the contents of a specified string into unmanaged memory.
static IntPtr SecureStringToGlobalAllocUnicode(const SharedPtr< Security::SecureString > &s)
Copies contents of specified secure string into unmanaged memory.
static void Copy(const container &source, int startIndex, void *destination, int length)
Implements public static void Copy(char[] source, int startIndex, IntPtr destination,...
Definition: marshal.h:49
static void Copy(const container &source, int startIndex, IntPtr destination, int length)
Implements public static void Copy(char[] source, int startIndex, IntPtr destination,...
Definition: marshal.h:59
static String PtrToStringAnsi(IntPtr ptr)
Creates a managed String from an unmanaged zero-terminated UTF8-string.
static String PtrToStringAuto(IntPtr ptr, int length)
Creates a managed String from an unmanaged string.
static int32_t ReadInt32(IntPtr ptr, int offset=0)
Reads int from memory.
Definition: marshal.h:153
static void Copy(const IntPtr source, container &&destination, int startIndex, int length)
Implements public static void Copy(IntPtr source, byte[] destination, int startIndex,...
Definition: marshal.h:28
static void FreeHGlobal(IntPtr address)
Frees unmanaged memory.
static String PtrToStringUni(IntPtr ptr, int length)
Creates a managed String from an unmanaged unicode string.
static IntPtr ReadIntPtr(IntPtr ptr, int offset=0)
Reads IntPtr from memory.
Definition: marshal.h:162
static void WriteByte(IntPtr ptr, int offset, uint8_t value)
Writes byte to memory.
Definition: marshal.h:83
static IntPtr StringToHGlobalAnsi(const String &s)
Copies the contents of a specified string into unmanaged memory.
static void WriteByte(IntPtr ptr, uint8_t value)
Writes byte to memory.
Definition: marshal.h:90
static IntPtr SecureStringToGlobalAllocAnsi(const SharedPtr< Security::SecureString > &s)
Copies contents of specified secure string into unmanaged memory, converting into ANSI format.
static IntPtr StringToHGlobalAuto(const String &s)
Copies the contents of a specified string into unmanaged memory, converting to ANSI format if require...
static int16_t ReadInt16(IntPtr ptr, int offset=0)
Reads short from memory.
Definition: marshal.h:144
static String PtrToStringUni(IntPtr ptr)
Creates a managed String from an unmanaged zero-terminated unicode string.
static void WriteInt64(IntPtr ptr, int offset, int64_t value)
Writes long to memory.
Definition: marshal.h:117
static void ZeroFreeGlobalAllocUnicode(IntPtr s)
Frees unmanaged string pointer that was allocated using the SecureStringToGlobalAllocUnicode method.
static int32_t GetHRForException(const System::Exception &exc)
Gets HResult from exception.
Definition: marshal.h:170
static void WriteInt32(IntPtr ptr, int offset, int32_t value)
Writes int to memory.
Definition: marshal.h:108
static String PtrToStringAuto(IntPtr ptr)
Creates a managed String from an unmanaged zero-terminated string.
static uint8_t ReadByte(IntPtr ptr, int offset=0)
Reads byte from memory.
Definition: marshal.h:135
static IntPtr AllocHGlobal(int32_t bytes_count)
Allocates unmanaged memory.
static void WriteInt16(IntPtr ptr, int offset, int16_t value)
Writes short to memory.
Definition: marshal.h:99
static String PtrToStringUTF8(IntPtr ptr)
Creates a managed String from an unmanaged zero-terminated UTF8-string.
static void Copy(const void *source, container &&destination, int startIndex, int length)
Implements public static void Copy(IntPtr source, byte[] destination, int startIndex,...
Definition: marshal.h:38
static String PtrToStringAnsi(IntPtr ptr, int length)
Creates a managed String from an unmanaged UTF8-string.
static IntPtr AllocHGlobal(IntPtr bytes_count)
Allocates unmanaged memory.
static String PtrToStringUTF8(IntPtr ptr, int length)
Creates a managed String from an unmanaged UTF8-string.
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
String class used across the library. Is a substitute for C# System.String when translating code....
Definition: string.h:125
Definition: db_command.h:9