forked from node-ffi/node-ffi
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathwin32-dlfcn.cc
171 lines (131 loc) · 3.12 KB
/
win32-dlfcn.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* @file Minimal emulation of POSIX dlopen/dlsym/dlclose on Windows.
* @license Public domain.
*
* This code works fine for the common scenario of loading a
* specific DLL and calling one (or more) functions within it.
*
* No attempt is made to emulate POSIX symbol table semantics.
* The way Windows thinks about dynamic linking is fundamentally
* different, and there's no way to emulate the useful aspects of
* POSIX semantics.
*/
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <stdio.h>
#include <malloc.h>
#include "win32-dlfcn.h"
/**
* Win32 error code from last failure.
*/
static DWORD lastError = 0;
#ifdef __cplusplus
extern "C" {
#endif
/**
* Convert UTF-8 string to Windows UNICODE (UCS-2 LE).
*
* Caller must free() the returned string.
*/
static
WCHAR*
UTF8toWCHAR(
const char* inputString /** UTF-8 string. */
)
{
int outputSize;
WCHAR* outputString;
outputSize = MultiByteToWideChar(CP_UTF8, 0, inputString, -1, NULL, 0);
if (outputSize == 0)
return NULL;
outputString = (WCHAR*) malloc(outputSize * sizeof(WCHAR));
if (outputString == NULL) {
SetLastError(ERROR_OUTOFMEMORY);
return NULL;
}
if (MultiByteToWideChar(CP_UTF8, 0, inputString, -1, outputString, outputSize) != outputSize)
{
free(outputString);
return NULL;
}
return outputString;
}
/**
* Open DLL, returning a handle.
*/
void*
dlopen(
const char* file, /** DLL filename (UTF-8). */
int mode /** mode flags (ignored). */
)
{
WCHAR* unicodeFilename;
UINT errorMode;
void* handle;
UNREFERENCED_PARAMETER(mode);
if (file == NULL)
return (void*) GetModuleHandle(NULL);
unicodeFilename = UTF8toWCHAR(file);
if (unicodeFilename == NULL) {
lastError = GetLastError();
return NULL;
}
errorMode = GetErrorMode();
/* Have LoadLibrary return NULL on failure; prevent GUI error message. */
SetErrorMode(errorMode | SEM_FAILCRITICALERRORS);
handle = (void*) LoadLibraryW(unicodeFilename);
if (handle == NULL)
lastError = GetLastError();
SetErrorMode(errorMode);
free(unicodeFilename);
return handle;
}
/**
* Close DLL.
*/
int
dlclose(
void* handle /** Handle from dlopen(). */
)
{
int rc = 0;
if (handle != (void*) GetModuleHandle(NULL))
rc = !FreeLibrary((HMODULE) handle);
if (rc)
lastError = GetLastError();
return rc;
}
/**
* Look up symbol exported by DLL.
*/
void*
dlsym(
void* handle, /** Handle from dlopen(). */
const char* name /** Name of exported symbol (ASCII). */
)
{
void* address = (void*) GetProcAddress((HMODULE) handle, name);
if (address == NULL)
lastError = GetLastError();
return address;
}
/**
* Return message describing last error.
*/
char*
dlerror(void)
{
static char errorMessage[64];
if (lastError != 0) {
sprintf(errorMessage, "Win32 error %lu", lastError);
lastError = 0;
return errorMessage;
} else {
return NULL;
}
}
#ifdef __cplusplus
}
#endif