lua 调用 dll 的简单demo

lua 调用 dll 的简单demo

作者:fenlei2   2023-06-19 22:02:11  

详情

windows下的 lua 与 c 进行交互, 本人新手,只能做到这么多了

c代码

//#include ""#include ""#ifdef _cplusplus extern "C"{#endif#include ""#include ""#include ""extern int isquare(lua_State *L);extern int alert(lua_State *L);#ifdef _cplusplus}#endifint luaopen_add(lua_State *L){ lua_register( L, "square", isquare ); lua_register(L,"alert",alert);// lua_register(L,"cube",icube); return 0;}int alert(lua_State *L){ const char * desc = lua_tostring(L,-1); MessageBox(NULL,desc,"alert",MB_OK); return 1;}int isquare(lua_State *L){ float rtrn = lua_tonumber(L, -1); //printf("Top of square(), nbr=%f",rtrn); lua_pushnumber(L,rtrn*rtrn); return 1; }

编译命令

cl /c /I ../include

include 为 lua 头文件所在目录

link /def: /dll ../ "" "" ""

导出函数到 dll

; ADD; MY_DLLMAIN 将成为生成的dll的名称DEscriptION 'test'EXPORTS isquare @ 1 alert @ 2; 这个名称即为函数的实际导出名称 @1为函数的导出编号

lua

square = ("", "isquare")alert=("", "alert")print(alert)print ( square(2) )alert("222")

执行结果

虽然有乱码存在问题,但是期望结果基本已经达到

请在电脑上注册登陆