-
Notifications
You must be signed in to change notification settings - Fork 1
/
LibraryFunctions.cpp
42 lines (38 loc) · 1.38 KB
/
LibraryFunctions.cpp
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
#include "framework.h"
#include "LibraryFunctions.h"
int luaLoadLibraryA(lua_State* L) {
LPCSTR dllname = luaL_checkstring(L, 1);
HMODULE handle = LoadLibraryA(dllname);
if (handle == NULL) {
return luaL_error(L, "Could not load library '%s': %I", dllname, GetLastError());
}
lua_pushinteger(L, (DWORD) handle);
return 1;
}
int luaGetLibraryProcAddressA(lua_State* L) {
LPCSTR dllname = luaL_checkstring(L, 1);
HMODULE handle = LoadLibraryA(dllname);
if (handle == NULL) {
return luaL_error(L, "Could not load library '%s': %I", dllname, GetLastError());
}
LPCSTR funcname = luaL_checkstring(L, 2);
FARPROC f = GetProcAddress(handle, funcname);
if (f == NULL) {
return luaL_error(L, "Could not load function '%s' from library '%s': %I", funcname, dllname, GetLastError());
}
lua_pushinteger(L, (DWORD)f);
return 1;
}
int luaGetProcAddress(lua_State* L) {
HMODULE handle = (HMODULE) luaL_checkinteger(L, 1);
if (handle == NULL) {
return luaL_error(L, "Invalid library handle: %I", handle, GetLastError());
}
LPCSTR funcname = luaL_checkstring(L, 2);
FARPROC f = GetProcAddress(handle, funcname);
if (f == NULL) {
return luaL_error(L, "Could not load function '%s' from library '%I': %I", funcname, handle, GetLastError());
}
lua_pushinteger(L, (DWORD)f);
return 1;
}