当前位置: 首页 » 资讯 » 综合 » 正文

lua 调用 dll 的简单demo

发布日期:2023-06-19 22:03:18     浏览次数:6
核心提示:windows下的 lua 与 c 进行交互, 本人新手,只能做到这么多了 c代码 //#include ""#include ""#ifdef _cplusplus extern "C"{#endif#include

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")

执行结果

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

头条分类

免责声明:本站所有信息由各公司自行发布,请在交易前确认真实合法性,本站不承担任何交易及此类作品侵权行为的直接责任及连带责任!