WTLVisual C++ 7.1Visual Studio .NET 2003Windows 2003Windows 2000Windows XPC中级开发Visual StudioWindowsC++
在您的应用程序中使用 Lua






4.58/5 (18投票s)
2005年5月5日
2分钟阅读

73047

4037
将 Lua 集成到你的应用程序中的方法。
*Lua 脚本(如下)用于自定义 UI 的偏好和内容(如上)。
引言
本文展示了将 Lua 集成到你的应用程序中的一种方法。
什么是 Lua?
Lua 是一种扩展编程语言,旨在支持使用数据描述工具进行通用程序编程。它还为面向对象编程、函数式编程和数据驱动编程提供良好的支持。 Lua 旨在用作任何需要配置语言的程序的强大、轻量级配置语言。 Lua 以库的形式实现,用纯 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
- 有关完整描述,请参阅:关于。
背景
此示例是一个 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。(下载 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 //-
记住: 要更改你的项目属性 -> 链接器 -> 常规 -> 附加库目录:lua lib 目录。
- 添加 Lua 包含文件
extern "C" { #include "lua.h" }
记住: 要更改你的项目属性 -> C/C++ -> 常规 -> 附加包含目录: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 5.0 参考手册。
关注点
- Lua 是一个 免费软件。
- 下载 Lua: 下载。
- 手册:Lua 5.0 参考手册。
- 参考:Lua。
历史
- 2005 年 4 月 29 日:首次发布。