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.
13 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
16 #include "mono/utils/mono-dl.h"
17 #include "mono/utils/mono-embed.h"
25 static GHashTable
*mono_dls
;
28 dl_mapping_open (const char *file
, int flags
, char **err
, void *user_data
)
30 MonoDlMapping
*mappings
;
32 if (mono_dls
== NULL
){
33 *err
= g_strdup ("Library not registered");
37 mappings
= (MonoDlMapping
*) g_hash_table_lookup (mono_dls
, file
);
38 *err
= g_strdup (mappings
== NULL
? "File not registered" : "");
43 dl_mapping_symbol (void *handle
, const char *symbol
, char **err
, void *user_data
)
45 MonoDlMapping
*mappings
= (MonoDlMapping
*) handle
;
47 for (;mappings
->name
; mappings
++){
48 if (strcmp (symbol
, mappings
->name
) == 0){
50 return mappings
->addr
;
53 *err
= g_strdup ("Symbol not found");
58 * mono_dl_register_library:
59 u * @name: Library name, this is the name used by the DllImport as the external library name
60 * @mappings: the mappings to register for P/Invoke.
62 * The mappings registered using this function are used as fallbacks if the dynamic linker
63 * fails, or if the platform doesn't have a dynamic linker.
65 * Mappings is a pointer to the first element of an array of
66 * MonoDlMapping values. The list must be terminated with both
67 * the name and addr fields set to NULL.
69 * This is typically used like this:
70 * MonoDlMapping sample_library_mappings [] = {
71 * { "CallMe", CallMe },
79 * mono_dl_register_library ("sample", sample_library_mappings);
83 * Then the C# code can use this P/Invoke signature:
85 * [DllImport ("sample")]
86 * extern static int CallMe (int f);
89 mono_dl_register_library (const char *name
, MonoDlMapping
*mappings
)
91 if (mono_dls
== NULL
){
92 mono_dls
= g_hash_table_new (g_str_hash
, g_str_equal
);
93 mono_dl_fallback_register (dl_mapping_open
, dl_mapping_symbol
, NULL
, NULL
);
96 g_hash_table_insert (mono_dls
, g_strdup (name
), mappings
);