---
title: "How to create c extension for lua and pass complex structure step by step"
author: "lua"
date: "2009-11-24T11:00:40Z"
tags: []
slug: "how-to-create-c-extension-for-lua-and-pass-complex-structure-step-by-step"
draft: false
lastmod: "2009-11-24T11:04:55Z"
id: 524278
url: "https://lua.iteye.com/blog/524278"
---

Original link: [http://sunxiunan.com/?p=1498](http://sunxiunan.com/?p=1498)

 

You could download the project from [**http://groups.google.com/group/lua5/web/luautil.rar**](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.

 

 

[![image](http://sunxiunan.com/wp-content/uploads/2009/11/image_thumb11.png "image")](http://sunxiunan.com/wp-content/uploads/2009/11/image11.png)

 

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.

 

[![image](http://sunxiunan.com/wp-content/uploads/2009/11/image_thumb13.png "image")](http://sunxiunan.com/wp-content/uploads/2009/11/image13.png)

 

The project should have a function like luaopen\_xxx (in my code, it is luaopen\_luautil), and have strings=&gt;functions map structure like "lua\_util". Then you could call your extension like **require "luautil".**

 

[![image](http://sunxiunan.com/wp-content/uploads/2009/11/image_thumb14.png "image")](http://sunxiunan.com/wp-content/uploads/2009/11/image14.png)

 

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.

 

[![image](http://sunxiunan.com/wp-content/uploads/2009/11/image_thumb15.png "image")](http://sunxiunan.com/wp-content/uploads/2009/11/image15.png)

 

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