|
1 |
| -/* $PostgreSQL: pgsql/src/backend/port/dynloader/win32.c,v 1.5 2004/12/02 19:38:50 momjian Exp $ */ |
| 1 | +/* $PostgreSQL: pgsql/src/backend/port/dynloader/win32.c,v 1.6 2005/08/12 21:23:10 momjian Exp $ */ |
2 | 2 |
|
3 | 3 | #include <windows.h>
|
| 4 | +#include <stdio.h> |
4 | 5 |
|
5 | 6 | char *dlerror(void);
|
6 | 7 | int dlclose(void *handle);
|
7 | 8 | void *dlsym(void *handle, const char *symbol);
|
8 | 9 | void *dlopen(const char *path, int mode);
|
9 | 10 |
|
| 11 | +static char last_dyn_error[512]; |
| 12 | + |
| 13 | +static void set_dl_error(void) |
| 14 | +{ |
| 15 | + DWORD err = GetLastError(); |
| 16 | + |
| 17 | + if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | |
| 18 | + FORMAT_MESSAGE_FROM_SYSTEM, |
| 19 | + NULL, |
| 20 | + err, |
| 21 | + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 22 | + last_dyn_error, |
| 23 | + sizeof(last_dyn_error)-1, |
| 24 | + NULL) == 0) |
| 25 | + { |
| 26 | + snprintf(last_dyn_error, sizeof(last_dyn_error)-1, |
| 27 | + "unknown error %lu", err); |
| 28 | + } |
| 29 | +} |
| 30 | + |
10 | 31 | char *
|
11 | 32 | dlerror(void)
|
12 | 33 | {
|
13 |
| - return "dynamic load error"; |
| 34 | + if (last_dyn_error[0]) |
| 35 | + return last_dyn_error; |
| 36 | + else |
| 37 | + return NULL; |
14 | 38 | }
|
15 | 39 |
|
16 | 40 | int
|
17 | 41 | dlclose(void *handle)
|
18 | 42 | {
|
19 |
| - return FreeLibrary((HMODULE) handle) ? 0 : 1; |
| 43 | + if (!FreeLibrary((HMODULE) handle)) |
| 44 | + { |
| 45 | + set_dl_error(); |
| 46 | + return 1; |
| 47 | + } |
| 48 | + last_dyn_error[0] = 0; |
| 49 | + return 0; |
20 | 50 | }
|
21 | 51 |
|
22 | 52 | void *
|
23 | 53 | dlsym(void *handle, const char *symbol)
|
24 | 54 | {
|
25 |
| - return (void *) GetProcAddress((HMODULE) handle, symbol); |
| 55 | + void *ptr; |
| 56 | + ptr = GetProcAddress((HMODULE) handle, symbol); |
| 57 | + if (!ptr) |
| 58 | + { |
| 59 | + set_dl_error(); |
| 60 | + return NULL; |
| 61 | + } |
| 62 | + last_dyn_error[0] = 0; |
| 63 | + return ptr; |
26 | 64 | }
|
27 | 65 |
|
28 | 66 | void *
|
29 | 67 | dlopen(const char *path, int mode)
|
30 | 68 | {
|
31 |
| - return (void *) LoadLibrary(path); |
| 69 | + HMODULE h; |
| 70 | + int prevmode; |
| 71 | + |
| 72 | + /* Disable popup error messages when loading DLLs */ |
| 73 | + prevmode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX); |
| 74 | + h = LoadLibrary(path); |
| 75 | + SetErrorMode(prevmode); |
| 76 | + |
| 77 | + if (!h) |
| 78 | + { |
| 79 | + set_dl_error(); |
| 80 | + return NULL; |
| 81 | + } |
| 82 | + last_dyn_error[0] = 0; |
| 83 | + return (void *) h; |
32 | 84 | }
|
0 commit comments