3 * Windows marshal support.
5 * Copyright 2016 Microsoft
6 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
11 #if defined(HOST_WIN32)
15 #include "mono/metadata/marshal-windows-internals.h"
17 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
19 mono_marshal_alloc_hglobal (size_t size)
21 return GlobalAlloc (GMEM_FIXED, size);
25 mono_marshal_realloc_hglobal (gpointer ptr, size_t size)
27 return GlobalReAlloc (ptr, size, GMEM_MOVEABLE);
31 mono_marshal_free_hglobal (gpointer ptr)
36 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
39 mono_marshal_alloc_co_task_mem (size_t size)
41 return CoTaskMemAlloc (size);
45 mono_marshal_free_co_task_mem (void *ptr)
52 mono_marshal_realloc_co_task_mem (gpointer ptr, size_t size)
54 return CoTaskMemRealloc (ptr, size);
58 ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi (MonoString *string)
63 tres = mono_string_to_utf8_checked (string, &error);
64 if (mono_error_set_pending_exception (&error))
70 * mono_string_to_utf8_checked() returns a memory area at least as large as the size of the
71 * MonoString, even if it contains NULL characters. The copy we allocate here has to be equally
74 len = MAX (strlen (tres) + 1, string->length);
75 ret = ves_icall_System_Runtime_InteropServices_Marshal_AllocHGlobal ((gpointer)len);
76 memcpy (ret, tres, len);
82 ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalUni (MonoString *string)
87 size_t len = ((mono_string_length (string) + 1) * 2);
88 gunichar2 *res = ves_icall_System_Runtime_InteropServices_Marshal_AllocHGlobal ((gpointer)len);
90 memcpy (res, mono_string_chars (string), mono_string_length (string) * 2);
91 res [mono_string_length (string)] = 0;
97 mono_string_to_utf8str (MonoString *s)
101 GError *error = NULL;
107 as = CoTaskMemAlloc (1);
112 tmp = g_utf16_to_utf8 (mono_string_chars (s), s->length, NULL, &len, &error);
114 MonoException *exc = mono_get_exception_argument ("string", error->message);
115 g_error_free (error);
116 mono_set_pending_exception (exc);
119 as = CoTaskMemAlloc (len + 1);
120 memcpy (as, tmp, len + 1);
126 #endif /* HOST_WIN32 */