将Lua嵌入到自己的程序中

将Lua嵌入到自己的程序中

什么是Lua

Lua是具有简单数据描述的扩展编程语言(动态解析语言)。它提供了非常好的面向对象编程, 函数式编程(functional programming),数据驱动式编程(data-driven programming), 它可以作为一个强大、轻量的脚本语言,供任何需要的程序使用。 Lua 以一个用 clean C 写成的库形式提供。(所谓 Clean C ,指的 ANSI C 和 C++ 中共通的一个子集)

Lua例子(FOR 循环)

 
   
for i=1,10 do

    -- the first program in every language

    io.write("Hello world, from ",_VERSION,"!\n")

end


详细描述可以参考Lua网站http://www.lua.org/about.html

背景

本例子是WTL程序(简单的HTML帮助系统),结合Lua脚本作为参数和内容的定制。
定义的Lua函数如下:

 
   
-- # MessageBox

--------------------------------------------------------

-- int MessageBox(

-- string msg, |= Message to display

-- string capition |= Capition of Box

-- );

-- Return Value:

-- if 1 the user click in OK or user close the box





-- # ShowContentPainel

-----------------------------------------------------------------

-- void ShowContentPainel(

-- bool bShow |= If true, the painel start opened, if false not.

-- );



-- # SetWindowStartSize

-----------------------------------------------------------------

-- void SetWindowStartSize(

-- number w, |= Start W size of window

-- number h, |= Start H size of window

-- );

-- Remarks:

-- if this function is not called, the default size is 800 x 600



-- # SetMinWindowSize

-----------------------------------------------------------------

-- void SetMinWindowSize(

-- number w, |= Minimum W size of window

-- number h, |= Minimum H size of window

-- );



-- # SetTitle

-----------------------------------------------------------------

-- void SetTitle(

-- string title |= Text that be title of window.

-- );



-- # Navigate

-----------------------------------------------------------------

-- void Navigate(

-- string url |= Url

-- );



-- # InsertItemInPainel

-----------------------------------------------------------------

-- void InsertItemInPainel(

-- string title, |= Text displayed in tree

-- string url, |= Url

-- number icon, |= Icon of item, the possible values 

                           ---are: 0 = BOOK, 1 = FILE, 2 = NETFILE

-- number id, |= Id of item, this has to be unique and start in 1

-- number idp |= Parent item, this is a ID of a item that is 

                                ---the parent or '0' for root item.

-- );

-- sample:

-- ICON BOOK / ID 1 / In ROOT

-- InsertItemInPainel("Trinity Systems", 

            "http://www.novaamerica.net/trinitysystems/", 0, 1, 0); 

-- ICON NETFILE / ID 2 / In ID1 (Trinity Systems)

-- InsertItemInPainel("Orion", 

       "http://www.novaamerica.net/trinitysystems/Orion", 2, 2, 1); 



-- # ExpandItemInPainel

------------------------------------------------------------------

-- void ExpandItemInPainel(

-- string id |= Id of item

-- );

-- Remarks:

-- This function need to be called after InsertItemInPainel's


现在我将展示如何使用Lua/C++创建这些函数。

代码

首先要做的是创建含Lua的DLL

在工程里做如下链接:

 
   
//

// For sample:

//

//---------------------------------------------

// Library Linkage

//---------------------------------------------

//-

#if defined (_DEBUG)

#pragma comment( lib, "lua.lib" ) // Lua Support

#else

#pragma comment( lib, "lua.lib" ) // Lua Support

#endif

//-


记得:要更改项目的属性Project Property -> linker -> general -> additional library directory
到lua lib所在目录。

添加Lua包含文件:

 
   
extern "C" 

{

#include "lua.h"

}


记得:要更改项目的属性 Project Property -> C/C++ -> general -> additional include directories
到lua include所在目录。
注意:lua lib里的所有文件保持"c"的扩展名,因为Lua采用ANSI C编写。

现在我们需要按如下方式启动Lua VM

 
   
LRESULT OnCreate(UINT 

/*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& 

/*bHandled*/)

{

//...

// Lua

//

lua_State *luaVM = lua_open(); /* 

Open Lua */

//

luaopen_base(luaVM );     /* opens the 

basic library */

luaopen_table(luaVM );    /* opens the table 

library */

luaopen_io(luaVM );       /* opens 

the I/O library */

luaopen_string(luaVM );   /* opens the string 

lib. */

luaopen_math(luaVM );     /* opens the math lib. 

*/

if (NULL == 

luaVM)

{

         

MessageBox("Error Initializing lua\n");

}

//...

// Do things with lua 

code.

// see below

//...

lua_close(luaVM); /* Close Lua */

//

// 

End

//...

}


现在我们写Lua 和 C/C++ 结合的函数

Lua API函数可以这样写:

lua_register(s, n, g)

这里:
s:是lua_State
n:暴露给Lua的函数名称
g: C/C++中的结合函数

请看下面例子:

 
   
//...

// Do things with 

lua code.

lua_register( luaVM, "SetHome", l_SetHome );

//...

// 

-------------------------------------------

// 

#Lua Functions

// 

------------------------------------------

//

static 

int l_SetTitle( lua_State* 

luaVM)

{

         const char* 

title = luaL_checkstring(luaVM, 

1);

         

theMainFrame->SetWindowText(title);

         

return 0;

}


现在我们需要加载并执行Lua脚本

 
   
//...

// Do things with lua 

code.

lua_register( luaVM, "SetHome", l_SetHome );

//more glue 

functions

lua_dofile(luaVM, "hrconf.lua");

//...


执行Lua API函数可以这样:

lua_dofile(s, p)

这里:
s: 是lua_State
p: 是Lua脚本文件

为了完全理解Lua API,可以参考Lua 参考手册http://www.lua.org/manual/5.1/manual.html

兴趣点:
Lua是免费软件free software
下载Lua:http://www.lua.org/download.html
手册:http://www.lua.org/manual/5.1/
参考:http://www.lua.org/
VC知识库:http://www.vckbase.com/

你可能感兴趣的:(lua)