| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* win32dlopen.c |
| 4 |
* dynamic loader for Windows |
| 5 |
* |
| 6 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 7 |
* Portions Copyright (c) 1994, Regents of the University of California |
| 8 |
* |
| 9 |
* |
| 10 |
* IDENTIFICATION |
| 11 |
* src/port/win32dlopen.c |
| 12 |
* |
| 13 |
*------------------------------------------------------------------------- |
| 14 |
*/ |
| 15 |
|
| 16 |
#include "c.h" |
| 17 |
|
| 18 |
static char last_dyn_error[512]; |
| 19 |
|
| 20 |
static void |
| 21 |
set_dl_error(void) |
| 22 |
{ |
| 23 |
DWORD err = GetLastError(); |
| 24 |
|
| 25 |
if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | |
| 26 |
FORMAT_MESSAGE_FROM_SYSTEM, |
| 27 |
NULL, |
| 28 |
err, |
| 29 |
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), |
| 30 |
last_dyn_error, |
| 31 |
sizeof(last_dyn_error) - 1, |
| 32 |
NULL) == 0) |
| 33 |
{ |
| 34 |
snprintf(last_dyn_error, sizeof(last_dyn_error) - 1, |
| 35 |
"unknown error %lu", err); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
char * |
| 40 |
dlerror(void) |
| 41 |
{ |
| 42 |
if (last_dyn_error[0]) |
| 43 |
return last_dyn_error; |
| 44 |
else |
| 45 |
return NULL; |
| 46 |
} |
| 47 |
|
| 48 |
int |
| 49 |
dlclose(void *handle) |
| 50 |
{ |
| 51 |
if (!FreeLibrary((HMODULE) handle)) |
| 52 |
{ |
| 53 |
set_dl_error(); |
| 54 |
return 1; |
| 55 |
} |
| 56 |
last_dyn_error[0] = 0; |
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
void * |
| 61 |
dlsym(void *handle, const char *symbol) |
| 62 |
{ |
| 63 |
void *ptr; |
| 64 |
|
| 65 |
ptr = GetProcAddress((HMODULE) handle, symbol); |
| 66 |
if (!ptr) |
| 67 |
{ |
| 68 |
set_dl_error(); |
| 69 |
return NULL; |
| 70 |
} |
| 71 |
last_dyn_error[0] = 0; |
| 72 |
return ptr; |
| 73 |
} |
| 74 |
|
| 75 |
void * |
| 76 |
dlopen(const char *file, int mode) |
| 77 |
{ |
| 78 |
HMODULE h; |
| 79 |
int prevmode; |
| 80 |
|
| 81 |
/* Disable popup error messages when loading DLLs */ |
| 82 |
prevmode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX); |
| 83 |
h = LoadLibrary(file); |
| 84 |
SetErrorMode(prevmode); |
| 85 |
|
| 86 |
if (!h) |
| 87 |
{ |
| 88 |
set_dl_error(); |
| 89 |
return NULL; |
| 90 |
} |
| 91 |
last_dyn_error[0] = 0; |
| 92 |
return (void *) h; |
| 93 |
} |