2 * mono-embed.c: Example code APIs to register a libraries using
3 * mono_dl_fallback_register. Real implementations should instead
4 * use a binary search for implementing the dl_mapping_open and
5 * dl_mapping_symbol methods here.
8 * Mono Team (http://www.mono-project.com)
10 * Copyright 2001-2004 Ximian, Inc.
11 * Copyright 2004-2010 Novell, Inc.
15 #include "mono/utils/mono-dl.h"
16 #include "mono/utils/mono-embed.h"
24 static GHashTable *mono_dls;
27 dl_mapping_open (const char *file, int flags, char **err, void *user_data)
29 MonoDlMapping *mappings;
31 if (mono_dls == NULL){
32 *err = g_strdup ("Library not registered");
36 mappings = (MonoDlMapping *) g_hash_table_lookup (mono_dls, file);
37 *err = g_strdup (mappings == NULL ? "File not registered" : "");
42 dl_mapping_symbol (void *handle, const char *symbol, char **err, void *user_data)
44 MonoDlMapping *mappings = (MonoDlMapping *) handle;
46 for (;mappings->name; mappings++){
47 if (strcmp (symbol, mappings->name) == 0){
49 return mappings->addr;
52 *err = g_strdup ("Symbol not found");
57 * mono_dl_register_library:
58 u * @name: Library name, this is the name used by the DllImport as the external library name
59 * @mappings: the mappings to register for P/Invoke.
61 * The mappings registered using this function are used as fallbacks if the dynamic linker
62 * fails, or if the platform doesn't have a dynamic linker.
64 * Mappings is a pointer to the first element of an array of
65 * MonoDlMapping values. The list must be terminated with both
66 * the name and addr fields set to NULL.
68 * This is typically used like this:
69 * MonoDlMapping sample_library_mappings [] = {
70 * { "CallMe", CallMe },
78 * mono_dl_register_library ("sample", sample_library_mappings);
82 * Then the C# code can use this P/Invoke signature:
84 * [DllImport ("sample")]
85 * extern static int CallMe (int f);
88 mono_dl_register_library (const char *name, MonoDlMapping *mappings)
90 if (mono_dls == NULL){
91 mono_dls = g_hash_table_new (g_str_hash, g_str_equal);
92 mono_dl_fallback_register (dl_mapping_open, dl_mapping_symbol, NULL, NULL);
95 g_hash_table_insert (mono_dls, g_strdup (name), mappings);