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++创建这些函数。

代码

1. 首先要做的是创建含Lua的DLL
2. 在工程里做如下链接:

// // For sample: // //---------------------------------------------                                                                                                                                                                                     

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

3. 添加Lua包含文件:

extern "C" { #include "lua.h" }

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

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

LRESULT OnCreate(UINT , WPARAM , LPARAM , BOOL& ) { //... // Lua // lua_State *luaVM = lua_open(); // luaopen_base(luaVM ); luaopen_table(luaVM ); luaopen_io(luaVM ); luaopen_string(luaVM ); luaopen_math(luaVM ); if (NULL == luaVM) { MessageBox("Error Initializing lua\n"); } //... // Do things with lua code. // see below //... lua_close(luaVM); // // End //... }

5. 现在我们写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; }

6. 现在我们需要加载并执行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

原文地址:https://www.cnblogs.com/wonderKK/p/2240360.html