PostgreSQL Source Code  git master
win32dlopen.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * win32dlopen.c
4  * dynamic loader for Windows
5  *
6  * Portions Copyright (c) 1996-2024, 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
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),
31  sizeof(last_dyn_error) - 1,
32  NULL) == 0)
33  {
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 }
unsigned char symbol
Definition: api.h:2
void err(int eval, const char *fmt,...)
Definition: err.c:43
static PgChecksumMode mode
Definition: pg_checksums.c:56
#define snprintf
Definition: port.h:238
char * dlerror(void)
Definition: win32dlopen.c:40
void * dlsym(void *handle, const char *symbol)
Definition: win32dlopen.c:61
void * dlopen(const char *file, int mode)
Definition: win32dlopen.c:76
static char last_dyn_error[512]
Definition: win32dlopen.c:18
int dlclose(void *handle)
Definition: win32dlopen.c:49
static void set_dl_error(void)
Definition: win32dlopen.c:21