Click here to Skip to main content
15,867,488 members
Articles / Desktop Programming / WTL
Article

Using Lua to control your application

Rate me:
Please Sign up or sign in to vote.
4.21/5 (14 votes)
6 May 2005CPOL2 min read 70.6K   3.5K   77   5
Way to integrating Lua in your application.

Image 1

*Lua script (below) to customize preferences and content of UI (above).

Introduction

This article shows an easy way to integrate Lua in your application.

What is LUA?

Lua is an extension programming language designed to support general procedural programming with data description facilities. It also offers good support for object-oriented programming, functional programming, and data-driven programming. Lua is intended to be used as a powerful, light-weight configuration language for any program that needs one. Lua is implemented as a library, written in clean C (that is, in the common subset of ANSI C and C++).

Sample of Lua Syntax (for loop):

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

Background

This sample is a WTL application (simple HTML help systems) that integrates Lua scripts to customize preferences and content.

It defines these Lua functions:

-- # 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:
-- InsertItemInPainel("Trinity Systems", 
       "http://www.novaamerica.net/trinitysystems/", 0, 1, 0);
        -- ICON BOOK / ID 1 / In ROOT
-- InsertItemInPainel("Orion", 
       "http://www.novaamerica.net/trinitysystems/Orion", 2, 2, 1); 
       -- ICON NETFILE / ID 2 / In ID1 (Trinity Systems)

-- # ExpandItemInPainel
-----------------------------------------------------------------
-- void ExpandItemInPainel(
-- string id |= Id of item
-- );
-- Remarks:
-- This function need to be called after InsertItemInPainel's

...and now I will show how to create these functions in Lua/C++.

Code

  1. The first thing to do is build the DLL that contains Lua. (Download Lua DLL demo project - 224 Kb)
  2. Link this in your 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: You need to change your Project Property -> Linker -> General -> additional library directory: Lua lib directory.

  3. Add Lua include files:
    extern "C" 
    {
    #include "lua.h"
    }

    Remember: You need to change your Project Property -> C/C++ -> General -> additional include directories: Lua include directory.

    Notice that all the files in the Lua lib need stay in "c" extension, because Lua was written to be ANSI C compliant.

  4. Now we need to start the 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
      //...
    }
  5. Now we make a glue function between Lua and C/C++.

    The Lua API function to do this is:

    lua_register(s, n, g)

    where:

    • s: lua_State to register the function.
    • n: Name of the function exposed to Lua.
    • g: C/C++ glue function.

    See sample:

    //...
    // 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. Now we need load and execute the Lua script.
    //...
    // Do things with lua code.
    lua_register( luaVM, "SetHome", l_SetHome );
    //more glue functions
    lua_dofile(luaVM, "hrconf.lua");

    The Lua API function to do this is:

    lua_dofile(s, p)

    where:

    • s: lua_State to register the function.
    • p: Path to Lua script file.

    See script.

For a complete understanding of the Lua API, see this link.

Points of Interest

Lua is free software.

History

  • 29 April 2005: First release.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
NewsLUA as Plugin Engine Pin
WillianBR13-Nov-12 14:31
WillianBR13-Nov-12 14:31 
Muita boa a linguagem que a galera da PUC RIO desenvolveu. Eu uso para tornar minhas ferramentas mais flexíveis! Uso o http://www.swig.org/ para tornar a integração mais fácil, principalmente se estiver criando extensões para usar em outras linguagens de script (Perl, Python). Excelente quando combinada com o projeto Kepler (http://www.keplerproject.org/en/Kepler[^]). Smile | :)
___________________________________
Willian Silva Rodrigues
http://www.williansilva.com.br
http://br.linkedin.com/in/willianbr
--- --- --- ---
Radio(NXTL):55*112*109193
BBM PIN: 238d4349

GeneralMy vote of 3 Pin
WillianBR13-Nov-12 14:28
WillianBR13-Nov-12 14:28 
QuestionSame article? Pin
sanpee22-Sep-05 23:21
sanpee22-Sep-05 23:21 
GeneralLuabind Pin
agladysh210-May-05 22:08
agladysh210-May-05 22:08 
GeneralRe: Luabind Pin
Renato Tegon Forti11-May-05 1:39
Renato Tegon Forti11-May-05 1:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.