VC and LUA Mixed development VC program call LUA script function

Source: Internet
Author: User

Http://www.cnblogs.com/clever101/archive/2010/04/14/1712207.html

Zhu Jinchan

Source: http://www.cnblogs.com/clever101/

The LUA language is widely used in the gaming world for its excellent extensibility, simplicity, efficiency, and platform independence. It took some time today to learn how VC and LUA mixed development, to write a small routine, and to share this experience with you.

First download the latest version of Lua in http://www.lua.org/ftp/: lua-5.1.4. Then use the VS C + + 2005 + SP1 to build a Win32 Static library project: Lua, add all the relevant C files and H files. In order to export the LUA API interface, note the addition of a preprocessing macro: Lua_core (or Lua_lib), and the runtime Library with multithreaded DLLs.

First write a LUA script function with the following code:

function add (x, y)
return x + y;
End

Save this code as Add.lua.

Then we create a new MFC dialog box project: Calc. This program implements simple integer addition operations.

First, define a LUA virtual machine pointer and the variables required for the addition operation in the dialog class:


/**
* \brief LUA virtual machine pointer.
*/
Lua_state *m_plua;
/**
* \brief the left operand of the addition operation.
*/
int m_nleftnum;
/**
* \brief the left operand of the addition operation.
*/
int m_nrightnum;
/**
* \brief The result of the addition operation.
*/
Long M_nresult;

In the dialog initialization function Ccalcdlg::oninitdialog calls the LUA virtual machine initialization function initluastate. The code for the Initluastate function is as follows:

/*!
* \brief initializes the LUA virtual machine.
*
* \return None.
*/
void Ccalcdlg::initluastate ()
{
M_plua = Lua_open ();
if (Null!=m_plua)
{
Luaopen_base (M_plua);
Luaopen_table (M_plua);
Luaopen_string (M_plua);
Luaopen_math (M_plua);
Luaopen_debug (M_plua);
}
}

Close the LUA virtual machine in the Destroy message response function in the dialog box:

void Ccalcdlg::ondestroy ()
{
Cdialog::ondestroy ();

TODO: Add Message Handler code here
Lua_close (M_plua);
}

Add the following code in the message function that executes the addition button:

void Ccalcdlg::onbnclickedok ()
{
TODO: Add control notification Handler code here
UpdateData (TRUE);

Stackdump (M_plua);
String strluafile = _t ("");
Getluafile (Strluafile); Get Lua script file full path
Lual_dofile (M_plua,strluafile.c_str ()); Interpreting and analyzing LUA files
Stackdump (M_plua);
Lua_getglobal (m_plua,_t ("add")); Takes a global label add, takes the Add function and pushes the stack
Stackdump (M_plua);
Lua_pushnumber (M_plua,m_nleftnum); Press the first parameter into the stack.
Stackdump (M_plua);
Lua_pushnumber (M_plua,m_nrightnum); The second parameter presses the stack
Stackdump (M_plua);
if (Lua_pcall (m_plua,2,1,0)! = 0)//execute the Add function
{
AfxMessageBox (_t ("Call Lua script function failed"));
Return
}
Stackdump (M_plua);
M_nresult = (int) lua_tonumber (M_plua,-1); After the function is executed, the execution result is stacked, so the top number is the result value, -1 is the value of the top of the stack.
Stackdump (M_plua);
Lua_pop (m_plua,1); Clears the value from the Stack, POPs (POPs) a value
Stackdump (M_plua);
UpdateData (FALSE);
}

The compilation environment is: WinXp + SP3, VS c++2005 + SP1, the program run interface is as follows:

Related source download here: VC Program calls LUA script function program source code download.

Reference documents:

1. function calls between LUA and C

2. LUA integration into MFC code

VC and LUA Mixed development VC program call LUA script function

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.