[MLton-commit] r4862
Vesa Karvonen
vesak at mlton.org
Mon Nov 27 04:37:30 PST 2006
Added minimal implementations of dlopen, dlclose, dlsym, and dlerror for
MinGW.
Emulation of dlopen(NULL, ...) behavior is problematic. The current
implementation just returns GetModuleHandle(NULL) which has a similar
meaning. Unfortunately, you can't access the C runtime from the returned
handle nor can you access symbols that have not been explicitly exported.
----------------------------------------------------------------------
U mlton/trunk/runtime/platform/mingw.c
----------------------------------------------------------------------
Modified: mlton/trunk/runtime/platform/mingw.c
===================================================================
--- mlton/trunk/runtime/platform/mingw.c 2006-11-27 09:36:16 UTC (rev 4861)
+++ mlton/trunk/runtime/platform/mingw.c 2006-11-27 12:37:26 UTC (rev 4862)
@@ -748,3 +748,91 @@
WSAStartup (version, &wsaData);
}
}
+
+/* ------------------------------------------------- */
+/* libdl */
+/* ------------------------------------------------- */
+
+static DWORD dlerror_last = ERROR_SUCCESS;
+/* This is for emulating the ugly stateful behavior of dlerror. */
+
+static HMODULE dl_main_module = NULL;
+/* Handle to the main module returned by GetModuleHandle(NULL). It is
+ * assumed that the main module isn't freed during the lifetime of the
+ * process.
+ */
+
+void *dlopen(const char *filename, int flag_IGNORED) {
+ if (!filename) {
+ if (!dl_main_module)
+ dl_main_module = GetModuleHandle(NULL);
+
+ if (!dl_main_module)
+ dlerror_last = GetLastError();
+
+ return dl_main_module;
+ }
+
+ {
+ HMODULE result = LoadLibrary(filename);
+
+ if (!result)
+ dlerror_last = GetLastError();
+
+ return result;
+ }
+}
+
+const char *dlerror(void) {
+ if (ERROR_SUCCESS == dlerror_last) {
+ return NULL;
+ } else {
+ static char buffer[256];
+
+ if (!FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
+ FORMAT_MESSAGE_FROM_SYSTEM,
+ NULL, dlerror_last, 0,
+ buffer, sizeof(buffer),
+ NULL))
+ snprintf(buffer, sizeof(buffer),
+ "Failed to format error message");
+
+ dlerror_last = ERROR_SUCCESS;
+
+ return buffer;
+ }
+}
+
+void *dlsym(void *void_hmodule, const char *symbol) {
+ HMODULE hmodule = void_hmodule;
+
+ if (!hmodule) {
+ dlerror_last = ERROR_INVALID_HANDLE;
+ return NULL;
+ }
+
+ {
+ void* result = GetProcAddress(hmodule, symbol);
+
+ if (!result)
+ dlerror_last = GetLastError();
+
+ return result;
+ }
+}
+
+int dlclose(void *void_hmodule) {
+ HMODULE hmodule = void_hmodule;
+
+ if (!hmodule || hmodule == dl_main_module)
+ return 0;
+
+ {
+ int result = !FreeLibrary(hmodule);
+
+ if (result)
+ dlerror_last = GetLastError();
+
+ return result;
+ }
+}
More information about the MLton-commit
mailing list