Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
wdlfcn.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2010 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "precompiled.h"
25 
27 
28 
29 static HMODULE HMODULE_from_void(void* handle)
30 {
31  return (HMODULE)handle;
32 }
33 
34 static void* void_from_HMODULE(HMODULE hModule)
35 {
36  return (void*)hModule;
37 }
38 
39 
40 int dlclose(void* handle)
41 {
42  WARN_IF_FALSE(FreeLibrary(HMODULE_from_void(handle)));
43  return 0;
44 }
45 
46 
47 char* dlerror()
48 {
49  // the obvious implementation involves sys_StatusDescription, but we
50  // don't want to return a pointer to a static array because that's not
51  // thread-safe. we therefore check GetLastError directly.
52  switch(GetLastError())
53  {
54  case ERROR_MOD_NOT_FOUND:
55  return "module not found";
56  case ERROR_PROC_NOT_FOUND:
57  return "symbol not found";
58  default:
59  return "unknown";
60  }
61 }
62 
63 
64 void* dlopen(const char* so_name, int flags)
65 {
66  ENSURE(!(flags & RTLD_GLOBAL));
67 
68  OsPath pathname = Path(so_name).ChangeExtension(L".dll");
69  HMODULE hModule = LoadLibraryW(OsString(pathname).c_str());
70  return void_from_HMODULE(hModule);
71 }
72 
73 
74 void* dlsym(void* handle, const char* sym_name)
75 {
76  HMODULE hModule = HMODULE_from_void(handle);
77  void* sym = GetProcAddress(hModule, sym_name);
78  ENSURE(sym);
79  return sym;
80 }
#define RTLD_GLOBAL
Definition: wdlfcn.h:34
static void * void_from_HMODULE(HMODULE hModule)
Definition: wdlfcn.cpp:34
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:282
Definition: path.h:75
void * dlopen(const char *so_name, int flags)
Definition: wdlfcn.cpp:64
int dlclose(void *handle)
Definition: wdlfcn.cpp:40
void * dlsym(void *handle, const char *sym_name)
Definition: wdlfcn.cpp:74
static HMODULE HMODULE_from_void(void *handle)
Definition: wdlfcn.cpp:29
Path ChangeExtension(Path extension) const
Definition: path.h:185
#define WARN_IF_FALSE(expression)
Definition: status.h:360
static Handle handle(size_t idx, u64 tag)
Definition: h_mgr.cpp:121
char * dlerror()
Definition: wdlfcn.cpp:47
static std::string OsString(const OsPath &path)
Definition: os_path.h:42