Skip to content

Instantly share code, notes, and snippets.

@mojeld
Last active August 27, 2015 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mojeld/b9ee700ee2616299be91 to your computer and use it in GitHub Desktop.
Save mojeld/b9ee700ee2616299be91 to your computer and use it in GitHub Desktop.
How to use lua53.dll in C++Builder
function lua_function_string(s)
return "るあ " .. s;
end
function lua_function_int(i)
return 5050 + i;
end
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
lua_State* lua1 = luaL_newstate_();
luaL_openlibs_(lua1);
if (luaL_dofile_(lua1, "func.lua") == LUA_OK)
{
String s = "";
int ret = lua_getglobal_(lua1, "lua_function_string");
lua_pushstring_(lua1, "ワールド");
ret = lua_call_(lua1, 1, 1);
s = lua_tostring_(lua1, -1);
ret = lua_getglobal_(lua1, "lua_function_int");
lua_pushinteger_(lua1, 100);
lua_call_(lua1, 1, 1);
ret = lua_tointeger_(lua1, -1);
ShowMessage(Format("実行完了\n (%s, %d)", ARRAYOFCONST((s, ret))) );
}
lua_close_(lua1);
FreeLibrary(luaa);
}
//---------------------------------------------------------------------------
#ifndef uLuaDllH
#define uLuaDllH
#include "lua.hpp"
HMODULE luaa = LoadLibrary(L"lua53.dll");
FARPROC proc_luaL_newstate = GetProcAddress(luaa, "luaL_newstate");
typedef lua_State* WINAPI (*TluaL_newstate)(void);
TluaL_newstate luaL_newstate_ = reinterpret_cast<TluaL_newstate>(proc_luaL_newstate);
FARPROC proc_luaL_openlibs = GetProcAddress(luaa, "luaL_openlibs");
typedef void WINAPI (*TluaL_openlibs)(lua_State *L);
TluaL_openlibs luaL_openlibs_ = reinterpret_cast<TluaL_openlibs>(proc_luaL_openlibs);
FARPROC proc_luaL_loadfilex = GetProcAddress(luaa, "luaL_loadfilex");
typedef int WINAPI (*TluaL_loadfilex)(lua_State *L, const char *filename, const char *mode);
TluaL_loadfilex luaL_loadfilex_ = reinterpret_cast<TluaL_loadfilex>(proc_luaL_loadfilex);
#define luaL_loadfile_(L,f) luaL_loadfilex_(L,f,NULL)
FARPROC proc_lua_close = GetProcAddress(luaa, "lua_close");
typedef void WINAPI (*Tlua_close)(lua_State *L);
Tlua_close lua_close_ = reinterpret_cast<Tlua_close>(proc_lua_close);
FARPROC proc_lua_pcallk = GetProcAddress(luaa, "lua_pcallk");
typedef int WINAPI (*Tlua_pcallk)(lua_State *L, int nargs, int nresults, int errfunc,
lua_KContext ctx, lua_KFunction k);
Tlua_pcallk lua_pcallk_ = reinterpret_cast<Tlua_pcallk>(proc_lua_pcallk);
#define lua_pcall_(L,n,r,f) lua_pcallk_(L, (n), (r), (f), 0, NULL)
#define luaL_dofile_(L, fn) \
(luaL_loadfile_(L, fn) || lua_pcall_(L, 0, LUA_MULTRET, 0))
FARPROC proc_lua_getglobal = GetProcAddress(luaa, "lua_getglobal");
typedef int WINAPI (*Tlua_getglobal)(lua_State *L, const char *name);
Tlua_getglobal lua_getglobal_ = reinterpret_cast<Tlua_getglobal>(proc_lua_getglobal);
FARPROC proc_lua_callk = GetProcAddress(luaa, "lua_callk");
typedef int WINAPI (*Tlua_callk)(lua_State *L, int nargs, int nresults,
lua_KContext ctx, lua_KFunction k);
Tlua_callk lua_callk_ = reinterpret_cast<Tlua_callk>(proc_lua_callk);
#define lua_call_(L,n,r) lua_callk_(L, (n), (r), 0, NULL)
FARPROC proc_lua_tolstring = GetProcAddress(luaa, "lua_tolstring");
typedef const char* WINAPI (*Tlua_tolstring)(lua_State *L, int idx, size_t *len);
Tlua_tolstring lua_tolstring_ = reinterpret_cast<Tlua_tolstring>(proc_lua_tolstring);
#define lua_tostring_(L,i) lua_tolstring_(L, (i), NULL)
FARPROC proc_lua_tointegerx = GetProcAddress(luaa, "lua_tointegerx");
typedef lua_Integer WINAPI (*Tlua_tointegerx)(lua_State *L, int idx, int *isnum);
Tlua_tointegerx lua_tointegerx_ = reinterpret_cast<Tlua_tointegerx>(proc_lua_tointegerx);
#define lua_tointeger_(L,i) lua_tointegerx_(L,(i),NULL)
FARPROC proc_lua_pushinteger = GetProcAddress(luaa, "lua_pushinteger");
typedef void WINAPI (*Tlua_pushinteger)(lua_State *L, lua_Integer n);
Tlua_pushinteger lua_pushinteger_ = reinterpret_cast<Tlua_pushinteger>(proc_lua_pushinteger);
FARPROC proc_lua_pushstring = GetProcAddress(luaa, "lua_pushstring");
typedef const char* WINAPI (*Tlua_pushstring)(lua_State *L, const char *s);
Tlua_pushstring lua_pushstring_ = reinterpret_cast<Tlua_pushstring>(proc_lua_pushstring);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment