James Kuszmaul | 82f6c04 | 2021-01-17 11:30:16 -0800 | [diff] [blame^] | 1 | /** |
| 2 | * @file dll.c Dynamic library loading for Windows |
| 3 | * |
| 4 | * Copyright (C) 2010 Creytiv.com |
| 5 | */ |
| 6 | #include <windows.h> |
| 7 | #include <re_types.h> |
| 8 | #include "../mod_internal.h" |
| 9 | |
| 10 | |
| 11 | #define DEBUG_MODULE "dll" |
| 12 | #define DEBUG_LEVEL 5 |
| 13 | #include <re_dbg.h> |
| 14 | |
| 15 | |
| 16 | /** |
| 17 | * Open a DLL file |
| 18 | * |
| 19 | * @param name Name of DLL to open |
| 20 | * |
| 21 | * @return Handle (NULL if failed) |
| 22 | */ |
| 23 | void *_mod_open(const char *name) |
| 24 | { |
| 25 | HINSTANCE DllHandle = 0; |
| 26 | |
| 27 | DEBUG_INFO("loading %s\n", name); |
| 28 | |
| 29 | DllHandle = LoadLibraryA(name); |
| 30 | if (!DllHandle) { |
| 31 | DEBUG_WARNING("open: %s LoadLibraryA() failed\n", name); |
| 32 | return NULL; |
| 33 | } |
| 34 | |
| 35 | return DllHandle; |
| 36 | } |
| 37 | |
| 38 | |
| 39 | /** |
| 40 | * Resolve a symbol address in a DLL |
| 41 | * |
| 42 | * @param h DLL Handle |
| 43 | * @param symbol Symbol to resolve |
| 44 | * |
| 45 | * @return Address of symbol |
| 46 | */ |
| 47 | void *_mod_sym(void *h, const char *symbol) |
| 48 | { |
| 49 | HINSTANCE DllHandle = (HINSTANCE)h; |
| 50 | union { |
| 51 | FARPROC sym; |
| 52 | void *ptr; |
| 53 | } u; |
| 54 | |
| 55 | if (!DllHandle) |
| 56 | return NULL; |
| 57 | |
| 58 | DEBUG_INFO("get symbol: %s\n", symbol); |
| 59 | |
| 60 | u.sym = GetProcAddress(DllHandle, symbol); |
| 61 | if (!u.sym) { |
| 62 | DEBUG_WARNING("GetProcAddress: no symbol %s\n", symbol); |
| 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | return u.ptr; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | /** |
| 71 | * Close a DLL |
| 72 | * |
| 73 | * @param h DLL Handle |
| 74 | */ |
| 75 | void _mod_close(void *h) |
| 76 | { |
| 77 | HINSTANCE DllHandle = (HINSTANCE)h; |
| 78 | |
| 79 | DEBUG_INFO("unloading %p\n", h); |
| 80 | |
| 81 | if (!DllHandle) |
| 82 | return; |
| 83 | |
| 84 | FreeLibrary(DllHandle); |
| 85 | } |