First set of licensing changes
[mono.git] / mono / utils / mono-dl-windows.c
1 /*
2  * mono-dl.c: Interface to the dynamic linker
3  *
4  * Author:
5  *    Mono Team (http://www.mono-project.com)
6  *
7  * Copyright 2001-2004 Ximian, Inc.
8  * Copyright 2004-2009 Novell, Inc.
9  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
10  */
11 #include <config.h>
12
13 #if defined(HOST_WIN32)
14
15 #include "mono/utils/mono-dl.h"
16 #include "mono/utils/mono-embed.h"
17 #include "mono/utils/mono-path.h"
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <ctype.h>
22 #include <string.h>
23 #include <glib.h>
24
25 #include <windows.h>
26 #include <psapi.h>
27
28
29 const char*
30 mono_dl_get_so_prefix (void)
31 {
32         return "";
33 }
34
35 const char**
36 mono_dl_get_so_suffixes (void)
37 {
38         static const char *suffixes[] = {
39                 ".dll",
40                 "",
41         };
42         return suffixes;
43 }
44
45 void*
46 mono_dl_open_file (const char *file, int flags)
47 {
48         gpointer hModule = NULL;
49         if (file) {
50                 gunichar2* file_utf16 = g_utf8_to_utf16 (file, strlen (file), NULL, NULL, NULL);
51                 guint last_sem = SetErrorMode (SEM_FAILCRITICALERRORS);
52                 guint32 last_error = 0;
53
54                 hModule = LoadLibrary (file_utf16);
55                 if (!hModule)
56                         last_error = GetLastError ();
57
58                 SetErrorMode (last_sem);
59                 g_free (file_utf16);
60
61                 if (!hModule)
62                         SetLastError (last_error);
63         } else {
64                 hModule = GetModuleHandle (NULL);
65         }
66         return hModule;
67 }
68
69 void
70 mono_dl_close_handle (MonoDl *module)
71 {
72         if (!module->main_module)
73                 FreeLibrary (module->handle);
74 }
75
76 void*
77 mono_dl_lookup_symbol (MonoDl *module, const char *symbol_name)
78 {
79         HMODULE *modules;
80         DWORD buffer_size = sizeof (HMODULE) * 1024;
81         DWORD needed, i;
82         gpointer proc = NULL;
83
84         /* get the symbol directly from the specified module */
85         if (!module->main_module)
86                 return GetProcAddress (module->handle, symbol_name);
87
88         /* get the symbol from the main module */
89         proc = GetProcAddress (module->handle, symbol_name);
90         if (proc != NULL)
91                 return proc;
92
93         /* get the symbol from the loaded DLLs */
94         modules = (HMODULE *) g_malloc (buffer_size);
95         if (modules == NULL)
96                 return NULL;
97
98         if (!EnumProcessModules (GetCurrentProcess (), modules,
99                                  buffer_size, &needed)) {
100                 g_free (modules);
101                 return NULL;
102         }
103
104         /* check whether the supplied buffer was too small, realloc, retry */
105         if (needed > buffer_size) {
106                 g_free (modules);
107
108                 buffer_size = needed;
109                 modules = (HMODULE *) g_malloc (buffer_size);
110
111                 if (modules == NULL)
112                         return NULL;
113
114                 if (!EnumProcessModules (GetCurrentProcess (), modules,
115                                          buffer_size, &needed)) {
116                         g_free (modules);
117                         return NULL;
118                 }
119         }
120
121         for (i = 0; i < needed / sizeof (HANDLE); i++) {
122                 proc = GetProcAddress (modules [i], symbol_name);
123                 if (proc != NULL) {
124                         g_free (modules);
125                         return proc;
126                 }
127         }
128
129         g_free (modules);
130         return NULL;
131 }
132
133 int
134 mono_dl_convert_flags (int flags)
135 {
136         return 0;
137 }
138
139 char*
140 mono_dl_current_error_string (void)
141 {
142         char* ret = NULL;
143         wchar_t* buf = NULL;
144         DWORD code = GetLastError ();
145
146         if (FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
147                 code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&buf, 0, NULL))
148         {
149                 ret = g_utf16_to_utf8 (buf, wcslen(buf), NULL, NULL, NULL);
150                 LocalFree (buf);
151         } else {
152                 g_assert_not_reached ();
153         }
154         return ret;
155 }
156
157 int
158 mono_dl_get_executable_path (char *buf, int buflen)
159 {
160         return -1; //TODO
161 }
162
163 const char*
164 mono_dl_get_system_dir (void)
165 {
166         return NULL;
167 }
168
169 #endif