3 * marshal.h: Routines for marshaling complex types in P/Invoke methods.
6 * Paolo Molaro (lupus@ximian.com)
8 * (C) 2002 Ximian, Inc. http://www.ximian.com
12 #ifndef __MONO_MARSHAL_H__
13 #define __MONO_MARSHAL_H__
15 #include <mono/metadata/class.h>
16 #include <mono/metadata/object-internals.h>
17 #include <mono/metadata/class-internals.h>
18 #include <mono/metadata/opcodes.h>
19 #include <mono/metadata/reflection.h>
23 typedef struct _MonoMethodBuilder MonoMethodBuilder;
25 /* marshaling helper functions */
28 mono_marshal_init (void);
31 mono_marshal_cleanup (void);
34 mono_class_native_size (MonoClass *klass, guint32 *align);
37 mono_marshal_load_type_info (MonoClass* klass);
40 mono_marshal_type_size (MonoType *type, MonoMarshalSpec *mspec, guint32 *align,
41 gboolean as_field, gboolean unicode);
44 mono_type_native_stack_size (MonoType *type, guint32 *alignment);
47 mono_array_to_savearray (MonoArray *array);
50 mono_array_to_lparray (MonoArray *array);
53 mono_string_utf8_to_builder (MonoStringBuilder *sb, char *text);
56 mono_string_utf16_to_builder (MonoStringBuilder *sb, gunichar2 *text);
59 mono_string_builder_to_utf8 (MonoStringBuilder *sb);
62 mono_string_builder_to_utf16 (MonoStringBuilder *sb);
65 mono_string_to_ansibstr (MonoString *string_obj);
68 mono_string_to_bstr (MonoString *string_obj);
71 mono_string_to_byvalstr (gpointer dst, MonoString *src, int size);
74 mono_string_to_byvalwstr (gpointer dst, MonoString *src, int size);
77 mono_delegate_to_ftnptr (MonoDelegate *delegate);
80 mono_ftnptr_to_delegate (MonoClass *klass, gpointer ftn);
82 void mono_delegate_free_ftnptr (MonoDelegate *delegate);
85 mono_marshal_set_last_error (void);
88 mono_marshal_asany (MonoObject *obj, MonoMarshalNative string_encoding, int param_attrs);
91 mono_marshal_free_asany (MonoObject *o, gpointer ptr, MonoMarshalNative string_encoding, int param_attrs);
94 mono_marshal_get_write_barrier (void);
96 /* method builder functions */
99 mono_mb_free (MonoMethodBuilder *mb);
102 mono_mb_new (MonoClass *klass, const char *name, MonoWrapperType type);
105 mono_mb_patch_addr (MonoMethodBuilder *mb, int pos, int value);
108 mono_mb_patch_addr_s (MonoMethodBuilder *mb, int pos, gint8 value);
111 mono_mb_add_data (MonoMethodBuilder *mb, gpointer data);
114 mono_mb_emit_native_call (MonoMethodBuilder *mb, MonoMethodSignature *sig, gpointer func);
117 mono_mb_emit_managed_call (MonoMethodBuilder *mb, MonoMethod *method, MonoMethodSignature *opt_sig);
120 mono_mb_add_local (MonoMethodBuilder *mb, MonoType *type);
123 mono_mb_create_method (MonoMethodBuilder *mb, MonoMethodSignature *signature, int max_stack);
126 mono_mb_emit_ldarg (MonoMethodBuilder *mb, guint argnum);
129 mono_mb_emit_ldarg_addr (MonoMethodBuilder *mb, guint argnum);
132 mono_mb_emit_ldloc (MonoMethodBuilder *mb, guint num);
135 mono_mb_emit_ldloc_addr (MonoMethodBuilder *mb, guint locnum);
138 mono_mb_emit_stloc (MonoMethodBuilder *mb, guint num);
141 mono_mb_emit_exception (MonoMethodBuilder *mb, const char *exc_name, const char *msg);
144 mono_mb_emit_icon (MonoMethodBuilder *mb, gint32 value);
147 mono_mb_emit_branch (MonoMethodBuilder *mb, guint8 op);
150 mono_mb_emit_short_branch (MonoMethodBuilder *mb, guint8 op);
153 mono_mb_emit_add_to_local (MonoMethodBuilder *mb, guint16 local, gint32 incr);
156 mono_mb_emit_ldflda (MonoMethodBuilder *mb, gint32 offset);
159 mono_mb_emit_byte (MonoMethodBuilder *mb, guint8 op);
162 mono_mb_emit_i2 (MonoMethodBuilder *mb, gint16 data);
165 mono_mb_emit_i4 (MonoMethodBuilder *mb, gint32 data);
168 mono_mb_emit_ldstr (MonoMethodBuilder *mb, char *str);
171 mono_type_to_ldind (MonoType *type);
174 mono_type_to_stind (MonoType *type);
176 /* functions to create various architecture independent helper functions */
179 mono_marshal_method_from_wrapper (MonoMethod *wrapper);
182 mono_marshal_get_remoting_invoke (MonoMethod *method);
185 mono_marshal_get_xappdomain_invoke (MonoMethod *method);
188 mono_marshal_get_remoting_invoke_for_target (MonoMethod *method, MonoRemotingTarget target_type);
191 mono_marshal_get_remoting_invoke_with_check (MonoMethod *method);
194 mono_marshal_get_delegate_begin_invoke (MonoMethod *method);
197 mono_marshal_get_delegate_end_invoke (MonoMethod *method);
200 mono_marshal_get_delegate_invoke (MonoMethod *method);
203 mono_marshal_get_runtime_invoke (MonoMethod *method);
206 mono_marshal_get_managed_wrapper (MonoMethod *method, MonoClass *delegate_klass, MonoObject *this);
209 mono_marshal_get_icall_wrapper (MonoMethodSignature *sig, const char *name, gconstpointer func);
212 mono_marshal_get_native_wrapper (MonoMethod *method);
215 mono_marshal_get_native_func_wrapper (MonoImage *image, MonoMethodSignature *sig, MonoMethodPInvoke *piinfo, MonoMarshalSpec **mspecs, gpointer func);
218 mono_marshal_get_struct_to_ptr (MonoClass *klass);
221 mono_marshal_get_ptr_to_struct (MonoClass *klass);
224 mono_marshal_get_stfld_wrapper (MonoType *type);
227 mono_marshal_get_ldfld_wrapper (MonoType *type);
230 mono_marshal_get_ldflda_wrapper (MonoType *type);
233 mono_marshal_get_ldfld_remote_wrapper (MonoClass *klass);
236 mono_marshal_get_stfld_remote_wrapper (MonoClass *klass);
239 mono_marshal_get_synchronized_wrapper (MonoMethod *method);
242 mono_marshal_get_unbox_wrapper (MonoMethod *method);
245 mono_marshal_get_isinst (MonoClass *klass);
248 mono_marshal_get_castclass (MonoClass *klass);
251 mono_marshal_get_proxy_cancast (MonoClass *klass);
254 mono_marshal_get_stelemref (void);
257 mono_marshal_get_generic_array_helper (MonoClass *class, MonoClass *iface,
258 gchar *name, MonoMethod *method);
260 /* marshaling internal calls */
263 mono_marshal_alloc (gulong size);
266 mono_marshal_free (gpointer ptr);
269 mono_marshal_free_array (gpointer *ptr, int size);
272 mono_marshal_realloc (gpointer ptr, gpointer size);
275 ves_icall_System_Runtime_InteropServices_Marshal_copy_to_unmanaged (MonoArray *src, gint32 start_index,
276 gpointer dest, gint32 length);
279 ves_icall_System_Runtime_InteropServices_Marshal_copy_from_unmanaged (gpointer src, gint32 start_index,
280 MonoArray *dest, gint32 length);
283 ves_icall_System_Runtime_InteropServices_Marshal_ReadIntPtr (gpointer ptr, gint32 offset);
286 ves_icall_System_Runtime_InteropServices_Marshal_ReadByte (gpointer ptr, gint32 offset);
289 ves_icall_System_Runtime_InteropServices_Marshal_ReadInt16 (gpointer ptr, gint32 offset);
292 ves_icall_System_Runtime_InteropServices_Marshal_ReadInt32 (gpointer ptr, gint32 offset);
295 ves_icall_System_Runtime_InteropServices_Marshal_ReadInt64 (gpointer ptr, gint32 offset);
298 ves_icall_System_Runtime_InteropServices_Marshal_WriteByte (gpointer ptr, gint32 offset, unsigned char val);
301 ves_icall_System_Runtime_InteropServices_Marshal_WriteIntPtr (gpointer ptr, gint32 offset, gpointer val);
304 ves_icall_System_Runtime_InteropServices_Marshal_WriteInt16 (gpointer ptr, gint32 offset, gint16 val);
307 ves_icall_System_Runtime_InteropServices_Marshal_WriteInt32 (gpointer ptr, gint32 offset, gint32 val);
310 ves_icall_System_Runtime_InteropServices_Marshal_WriteInt64 (gpointer ptr, gint32 offset, gint64 val);
313 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi (char *ptr);
316 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi_len (char *ptr, gint32 len);
319 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni (guint16 *ptr);
322 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni_len (guint16 *ptr, gint32 len);
325 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringBSTR (gpointer ptr);
328 ves_icall_System_Runtime_InteropServices_Marshal_GetComSlotForMethodInfoInternal (MonoReflectionMethod *m);
331 ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error (void);
334 ves_icall_System_Runtime_InteropServices_Marshal_SizeOf (MonoReflectionType *rtype);
337 ves_icall_System_Runtime_InteropServices_Marshal_StructureToPtr (MonoObject *obj, gpointer dst, MonoBoolean delete_old);
340 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure (gpointer src, MonoObject *dst);
343 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure_type (gpointer src, MonoReflectionType *type);
346 ves_icall_System_Runtime_InteropServices_Marshal_OffsetOf (MonoReflectionType *type, MonoString *field_name);
349 ves_icall_System_Runtime_InteropServices_Marshal_StringToBSTR (MonoString *string);
352 ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi (MonoString *string);
355 ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalUni (MonoString *string);
358 ves_icall_System_Runtime_InteropServices_Marshal_DestroyStructure (gpointer src, MonoReflectionType *type);
361 ves_icall_System_Runtime_InteropServices_Marshal_AllocCoTaskMem (int size);
364 ves_icall_System_Runtime_InteropServices_Marshal_FreeCoTaskMem (void *ptr);
367 ves_icall_System_Runtime_InteropServices_Marshal_AllocHGlobal (int size);
370 ves_icall_System_Runtime_InteropServices_Marshal_FreeHGlobal (void *ptr);
373 ves_icall_System_Runtime_InteropServices_Marshal_FreeBSTR (void *ptr);
376 ves_icall_System_Runtime_InteropServices_Marshal_UnsafeAddrOfPinnedArrayElement (MonoArray *arrayobj, int index);
379 ves_icall_System_Runtime_InteropServices_Marshal_GetDelegateForFunctionPointerInternal (void *ftn, MonoReflectionType *type);
382 ves_icall_System_Runtime_InteropServices_Marshal_AddRef (gpointer pUnk);
385 ves_icall_System_Runtime_InteropServices_Marshal_QueryInterface (gpointer pUnk, gpointer riid, gpointer* ppv);
388 ves_icall_System_Runtime_InteropServices_Marshal_Release (gpointer pUnk);
392 ves_icall_System_ComObject_CreateRCW (MonoReflectionType *type);
395 ves_icall_System_ComObject_Finalizer(MonoComObject* obj);
398 ves_icall_System_ComObject_FindInterface (MonoComObject* obj, MonoReflectionType* type);
401 ves_icall_System_ComObject_CacheInterface (MonoComObject* obj, MonoReflectionType* type, gpointer pItf);
404 ves_icall_System_ComObject_GetIUnknown (MonoComObject* obj);
407 ves_icall_System_ComObject_SetIUnknown (MonoComObject* obj, gpointer pUnk);
411 #endif /* __MONO_MARSHAL_H__ */