文章目录
把C的结构体传送给lua使用
把C的结构体传送给lua使用
#include <stdio.h>
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
typedef struct haha
{
int a
;
int b
;
}haha
;
lua_State
* L
;
int main
( int argc
, char *argv
[] )
{
L
= luaL_newstate();
luaL_openlibs(L
);
lua_register(L
, "average", average
);
luaL_loadfile(L
,"avg.lua");
haha a
={8,9};
lua_pcall(L
,0,0,0);
lua_getglobal(L
, "func1");
lua_newtable(L
);
lua_pushinteger(L
, a
.a
);
lua_setfield(L
, -2, "name");
lua_pushinteger(L
, a
.b
);
lua_setfield(L
, -2, "age");
printf("%d\n",lua_pcall(L
, 1, 1,0));
int result
= lua_tonumber(L
,-1);
printf("%d\n",result
);
luaL_dofile(L
, "avg.lua");
lua_close(L
);
printf( "Press enter to exit..." );
getchar();
return 0;
}
function func1(L
)
print("esfs")
print(L
.age
)
return -2;
end
转载请注明原文地址:https://tech.qufami.com/read-19192.html