Embed Lua into your own program

Source: Internet
Author: User
Document directory
  • Lua example (FOR loop)
  • Background
  • The first thing to do is to create a DLL containing Lua.
  • Add Lua inclusion file:
  • Now we need to start Lua VM as follows
  • Now we write functions that combine Lua and C/C ++.
  • Now we need to load and execute the Lua script
What is Lua?

Lua is an extended programming language (Dynamic parsing language) with simple data descriptions ). It provides very good object-oriented programming, functional programming (functional programming), and data-driven programming (data-driven programming). It can be used as a powerful and lightweight scripting language, for any program. Lua is provided as a library written in clean C. (The so-called Clean C Refers to a subset of ansi c and C ++)

Lua example (FOR loop)
for i=1,10 do    -- the first program in every language    io.write("Hello world, from ",_VERSION,"!\n")end

For more information, see Lua website http://www.lua.org/about.html.

Background

This example is a WTL Program (simple HTML Help System), which is customized with Lua script as parameters and content.
The defined Lua function is as follows:

-- # 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

Now I will show you how to use Lua/C ++ to create these functions.

The first thing the code needs to do is to create a DLL containing Lua.

Do the following link in the project:

//// For sample:////---------------------------------------------// Library Linkage//---------------------------------------------//-#if defined (_DEBUG)#pragma comment( lib, "lua.lib" ) // Lua Support#else#pragma comment( lib, "lua.lib" ) // Lua Support#endif//-

Remember: to change the properties of a Project, Project Property-> linker-> general-> additional library directory
To the directory where lua lib is located.

Add Lua inclusion file:
extern "C" {#include "lua.h"}

Remember: to change the properties of a Project, Project Property-> C/C ++-> general-> additional include directories
To the directory where lua include is located.
Note: All files in lua lib must have the "c" extension, because Lua is written in ansi c.

Now we need to start Lua VM as follows
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//...}

Now we write functions that combine Lua and C/C ++.

The Lua API function can be written as follows:

lua_register(s, n, g)

Here:
S: lua_State
N: name of the function exposed to Lua
Combined functions in g: C/C ++

See the following example:

//...// 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;}

Now we need to load and execute the Lua script
//...// Do things with lua code.lua_register( luaVM, "SetHome", l_SetHome );//more glue functionslua_dofile(luaVM, "hrconf.lua");//...

You can execute the Lua API function as follows:

lua_dofile(s, p)

Here:
S: lua_State
P: The Lua script file.

To fully understand the Lua API, refer to the Lua reference manual http://www.lua.org/manual/5.1/manual.html

Points of interest:
Lua is a free software
Download Lua: http://www.lua.org/download.html
Manual: http://www.lua.org/manual/5.1/
Reference: http://www.lua.org/
VC Knowledge Base: http://www.vckbase.com/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.