• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

How to create c extension for lua and pass complex structure step by step

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Original link: http://sunxiunan.com/?p=1498

 

You could download the project from http://groups.google.com/group/lua5/web/luautil.rar

At first, create a windows dll application. The IDE I used is VC2008.

 

I suggest you download and install the "Lua for windows " from luaforge.net, it contains most useful packages for Lua windows development. You could copy "include" and "lib" directory under the installation directory to your project.

 

 

 

In the beginning, you should include Lua header files.

 

Please note, you should use extern "C" to declare the header for Lua. In linker’s input, set the lua51.lib.

 

 

The project should have a function like luaopen_xxx (in my code, it is luaopen_luautil), and have strings=>functions map structure like "lua_util". Then you could call your extension like require "luautil".

 

 

The L_MSleep will call windows’ api Sleep().

 

Another function L_NewTable will generate a complex table for Lua script. It will call lua_settable() and lua_setfield() function to manipulate the table in Lua. lua_pushstring() and lua_pushnumber() could push string type and number type data to Lua. You could find the document about these functions in Lua 5.1 manual.

 

 

The table that Lua get will like:

 

local table1 = { 
           userName1 = "hahaha", 
           serialNumber1 = "1234613423", 
           tbl = {userName2 = "wwwww", serialNumber2 = "asdfasdadf", "5566", "wowowo"}}

 

After you build the project, copy the dll to lua installation folder "lua\5.1\clibs".

 

In the end, we could test the extension like:

 

require "luautil"
 local newtable =luautil.newtable()

for k, v in pairs(newtable) do
            print(k, v)
            if type(v) == "table" then
                for k2, v2 in pairs(v) do
                    print("–", k2, v2)
               end
           end
        end

 

– sleep 5 seconds.
 luautil.msleep(5000)


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
cocos2d使用idea开发lua发布时间:2022-07-22
下一篇:
Cocos2d-x的lua官方加密的解密方法发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap