2 * marshal-windows.c: Windows marshal support.
4 * Copyright 2016 Microsoft
5 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
10 #if defined(HOST_WIN32)
14 #include "mono/metadata/marshal-windows-internals.h"
16 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
18 mono_marshal_alloc_hglobal (size_t size)
20 return GlobalAlloc (GMEM_FIXED, size);
24 mono_marshal_realloc_hglobal (gpointer ptr, size_t size)
26 return GlobalReAlloc (ptr, size, GMEM_MOVEABLE);
30 mono_marshal_free_hglobal (gpointer ptr)
35 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
38 mono_marshal_alloc_co_task_mem (size_t size)
40 return CoTaskMemAlloc (size);
44 mono_marshal_free_co_task_mem (void *ptr)
51 mono_marshal_realloc_co_task_mem (gpointer ptr, size_t size)
53 return CoTaskMemRealloc (ptr, size);
57 ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi (MonoString *string)
62 tres = mono_string_to_utf8_checked (string, &error);
63 if (mono_error_set_pending_exception (&error))
69 * mono_string_to_utf8_checked() returns a memory area at least as large as the size of the
70 * MonoString, even if it contains NULL characters. The copy we allocate here has to be equally
73 len = MAX (strlen (tres) + 1, string->length);
74 ret = ves_icall_System_Runtime_InteropServices_Marshal_AllocHGlobal ((gpointer)len);
75 memcpy (ret, tres, len);
81 ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalUni (MonoString *string)
86 size_t len = ((mono_string_length (string) + 1) * 2);
87 gunichar2 *res = ves_icall_System_Runtime_InteropServices_Marshal_AllocHGlobal ((gpointer)len);
89 memcpy (res, mono_string_chars (string), mono_string_length (string) * 2);
90 res [mono_string_length (string)] = 0;
96 mono_string_to_lpstr (MonoString *s)
100 GError *error = NULL;
106 as = CoTaskMemAlloc (1);
111 tmp = g_utf16_to_utf8 (mono_string_chars (s), s->length, NULL, &len, &error);
113 MonoException *exc = mono_get_exception_argument ("string", error->message);
114 g_error_free (error);
115 mono_set_pending_exception (exc);
118 as = CoTaskMemAlloc (len + 1);
119 memcpy (as, tmp, len + 1);
125 #endif /* HOST_WIN32 */