YOU CAN CODE!

 

With The Case Of UCanCode.net  Release The Power OF  Visual C++ !   Home Products | Purchase Support | Downloads  
View in English
View in Japanese
View in
참고
View in Français
View in Italiano
View in 中文(繁體)
Download Evaluation
Pricing & Purchase?
E-XD++Visual C++/ MFC Products
Overview
Features Tour 
Electronic Form Solution
Visualization & HMI Solution
Power system HMI Solution
CAD Drawing and Printing Solution

Bar code labeling Solution
Workflow Solution

Coal industry HMI Solution
Instrumentation Gauge Solution

Report Printing Solution
Graphical modeling Solution
GIS mapping solution

Visio graphics solution
Industrial control SCADA &HMI Solution
BPM business process Solution

Industrial monitoring Solution
Flowchart and diagramming Solution
Organization Diagram Solution

Graphic editor Source Code
UML drawing editor Source Code
Map Diagramming Solution

Architectural Graphic Drawing Solution
Request Evaluation
Purchase
VX++ Cross-Platform C/C++
Overview
Download
Purchase
ActiveX COM Products
Overview
Download
Purchase
Technical Support
  General Q & A
Discussion Board
Contact Us

Links

Get Ready to Unleash the Power of UCanCode .NET

Call Lua Script in Visual C++ / MFC Sample

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

1

This article shows a way to integrate Lua in your application.

2

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, a 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
  • For full description see: About.

3

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

It defines the Lua function:

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

...and now I'll show you how to create these functions in Lua/C++.

4

  1. The first thing to do is to build the DLL that contains Lua. (download Lua DLL demo project)
  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: To change your Project Property -> linker -> general -> additional library directory: lua lib directory.

  3. Add Lua include files:
    extern "C" 
    {
    #include <span class="code-string">"lua.h"
    </span>
    }

    Remember: To change your Project Property -> C/C++ -> general -> additional include directories: lua include directory.

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

  4. 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
    //...
    }
  5. Now we make a glue function of Lua and C/C++.

    The Lua API function to do this is:

    lua_register(s, n, g)

    where

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

    See the 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 the load and execute Lua scripts:
    //...
    // 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: is the lua_State to register the function.
    • p: path for Lua script file.

    See the demo script.

For a complete understanding of the Lua API, see: Lua 5.0 Reference Manual.

4

Lua_Lib.zip

News:

1 UCanCode Advance E-XD++ CAD Drawing and Printing Solution Source Code Solution for C/C++, .NET V2024 is released!

2 UCanCode Advance E-XD++ HMI & SCADA Source Code Solution for C/C++, .NET V2024 is released!

3 UCanCode Advance E-XD++ GIS SVG Drawing and Printing Solution Source Code Solution for C/C++, .NET V2024 is released!

 


Contact UCanCode Software

To buy the source code or learn more about with:

Next--> Promotional personalized database document printing Solution

Ask any questions by MSN: ucancode@hotmail.com Yahoo: ucan_code@yahoo.com


 

Copyright ?1998-2024 UCanCode.Net Software , all rights reserved.
Other product and company names herein may be the trademarks of their respective owners.

Please direct your questions or comments to webmaster@ucancode.net