[tools] Add nuget-hash-extractor tool to help produce the runtime ignored assemblies...
[mono-project.git] / mono / utils / mono-embed.c
blobde78a84aa431f0e84026576401ba4acdac96e84d
1 /*
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.
7 * Author:
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.
15 #include "config.h"
16 #include "mono/utils/mono-dl.h"
17 #include "mono/utils/mono-embed.h"
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <ctype.h>
22 #include <string.h>
23 #include <glib.h>
25 static GHashTable *mono_dls;
27 static void *
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");
34 return NULL;
37 mappings = (MonoDlMapping *) g_hash_table_lookup (mono_dls, file);
38 *err = g_strdup (mappings == NULL ? "File not registered" : "");
39 return mappings;
42 static void *
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){
49 *err = g_strdup ("");
50 return mappings->addr;
53 *err = g_strdup ("Symbol not found");
54 return NULL;
57 /**
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 },
72 * { NULL, NULL }
73 * };
75 * ...
76 * main ()
77 * {
78 * ...
79 * mono_dl_register_library ("sample", sample_library_mappings);
80 * ...
81 * }
83 * Then the C# code can use this P/Invoke signature:
85 * [DllImport ("sample")]
86 * extern static int CallMe (int f);
88 void
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);