Header And Logo

PostgreSQL
| The world's most advanced open source database.

win32.c

Go to the documentation of this file.
00001 /* src/backend/port/dynloader/win32.c */
00002 
00003 #include "postgres.h"
00004 
00005 char       *dlerror(void);
00006 int         dlclose(void *handle);
00007 void       *dlsym(void *handle, const char *symbol);
00008 void       *dlopen(const char *path, int mode);
00009 
00010 static char last_dyn_error[512];
00011 
00012 static void
00013 set_dl_error(void)
00014 {
00015     DWORD       err = GetLastError();
00016 
00017     if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
00018                       FORMAT_MESSAGE_FROM_SYSTEM,
00019                       NULL,
00020                       err,
00021                       MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
00022                       last_dyn_error,
00023                       sizeof(last_dyn_error) - 1,
00024                       NULL) == 0)
00025     {
00026         snprintf(last_dyn_error, sizeof(last_dyn_error) - 1,
00027                  "unknown error %lu", err);
00028     }
00029 }
00030 
00031 char *
00032 dlerror(void)
00033 {
00034     if (last_dyn_error[0])
00035         return last_dyn_error;
00036     else
00037         return NULL;
00038 }
00039 
00040 int
00041 dlclose(void *handle)
00042 {
00043     if (!FreeLibrary((HMODULE) handle))
00044     {
00045         set_dl_error();
00046         return 1;
00047     }
00048     last_dyn_error[0] = 0;
00049     return 0;
00050 }
00051 
00052 void *
00053 dlsym(void *handle, const char *symbol)
00054 {
00055     void       *ptr;
00056 
00057     ptr = GetProcAddress((HMODULE) handle, symbol);
00058     if (!ptr)
00059     {
00060         set_dl_error();
00061         return NULL;
00062     }
00063     last_dyn_error[0] = 0;
00064     return ptr;
00065 }
00066 
00067 void *
00068 dlopen(const char *path, int mode)
00069 {
00070     HMODULE     h;
00071     int         prevmode;
00072 
00073     /* Disable popup error messages when loading DLLs */
00074     prevmode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
00075     h = LoadLibrary(path);
00076     SetErrorMode(prevmode);
00077 
00078     if (!h)
00079     {
00080         set_dl_error();
00081         return NULL;
00082     }
00083     last_dyn_error[0] = 0;
00084     return (void *) h;
00085 }