MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dlfcn.c
1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 #include <stdio.h>
3 #include <windows.h>
4 #include <dlfcn.h>
5 #include <stdbool.h>
6 
7 /*
8  * Keep track if the user tried to call dlopen(NULL, xx) to be able to give a sane
9  * error message
10  */
11 static bool self = false;
12 
13 void* dlopen(const char* path, int mode) {
14  if (path == NULL) {
15  // We don't support opening ourself
16  self = true;
17  return NULL;
18  }
19 
20  void* handle = LoadLibrary(path);
21  if (handle == NULL) {
22  char *buf = malloc(strlen(path) + 20);
23  sprintf(buf, "%s.dll", path);
24  handle = LoadLibrary(buf);
25  free(buf);
26  }
27 
28  return handle;
29 }
30 
31 void* dlsym(void* handle, const char* symbol) {
32  return GetProcAddress(handle, symbol);
33 }
34 
35 int dlclose(void* handle) {
36  // dlclose returns zero on success.
37  // FreeLibrary returns nonzero on success.
38  return FreeLibrary(handle) != 0;
39 }
40 
41 static char dlerror_buf[200];
42 
43 const char *dlerror(void) {
44  if (self) {
45  return "not supported";
46  }
47 
48  DWORD err = GetLastError();
49  LPVOID error_msg;
50  if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
51  FORMAT_MESSAGE_FROM_SYSTEM |
52  FORMAT_MESSAGE_IGNORE_INSERTS,
53  NULL, err, 0, (LPTSTR)&error_msg, 0, NULL) != 0) {
54  strncpy(dlerror_buf, error_msg, sizeof(dlerror_buf));
55  dlerror_buf[sizeof(dlerror_buf) - 1] = '\0';
56  LocalFree(error_msg);
57  } else {
58  return "Failed to get error message";
59  }
60 
61  return dlerror_buf;
62 }