lua newbie:C-Lua如何从C向lua传递结构/缓冲区? [英] lua newbie : C-Lua How to pass a struct/buffer to lua from C?

查看:160
本文介绍了lua newbie:C-Lua如何从C向lua传递结构/缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循以下样板示例: http://www.matrix44.net/blog/?p = 456

I followed this boiler plate example: http://www.matrix44.net/blog/?p=456

我能够将字符串从C发送到Lua,但是在传递结构(我使用lua_newuserdata)时遇到错误.

I am able to send string across from C to Lua, but I am getting an error when I pass a struct (i used lua_newuserdata).

-C片段---

//s = (ST*) malloc ( sizeof(ST) );
//    lua_pushlightuserdata(state, s);
s = lua_newuserdata(state, sizeof(ST));
s->a=11;
s->b=12;

printf("s =%p \ n",s); //打印0x80a708 结果= lua_pcall(状态,0,LUA_MULTRET,0);

printf ( "s = %p \n", s ); // Prints 0x80a708 result = lua_pcall(state, 0, LUA_MULTRET, 0);

-Lua剪---- print(foo)//打印0x80a708 io.write("received has:\ n",foo.a); //错误:无法运行脚本:script.lua:1:尝试索引全局'foo'(一个userdata值)

--- Lua snip ---- print(foo) // Prints 0x80a708 io.write("received has:\n", foo.a); // ERROR : Failed to run script: script.lua:1: attempt to index global 'foo' (a userdata value)

如何将缓冲区转换为struct/table-record?

How do i cast the buffer into a struct/table-record?

最终,我的C程序不知道它将仅将缓冲区传递给Lua的结构,而Lua知道该结构并将其强制转换为该结构.

Ultimately, my C program doesn't know the struct it will only pass the buffer to Lua and Lua knows the struct and need to be cast there.

谢谢, PS:脚本和C程序的printf都匹配(0x80a708),我可以强制转换??

Thanks, PS : the printf of both the script and C program matches (0x80a708), can I just cast ??

推荐答案

已解决:我使用luajit并能够传递缓冲区并将其转换为lua端的C结构.

SOLVED : I used luajit and was able to pass a buffer and cast it to C struct on the lua side.

这篇关于lua newbie:C-Lua如何从C向lua传递结构/缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆