출처 : http://www.codegurus.be/codegurus/Programming/luaembedding_en.htm

 

Embedding LUA in Visual C++ (Custom Functions)

Introduction

If you want to know more about how to start using LUA and Visual C++, then please follow the link at the left.  The code below is made with LUA 5.0.2.

 

Calling C/C++ functions from LUA

In order to embed a C++ function in LUA you have to register it to the LUA state, this is done by using the lua_register macro.  This macro takes 3 parameters :

#define lua_register(L,n,f)

The 1st parameter is the LUA state pointer, the 2nd one is the LUA function name and the 3th one is the C function name.

For this example we'll create an LUA function that will allow a script to show a message box to the user.  We'll call this function alert (inspired by a very very similar JavaScript function).

We first create our internal function, this function is so basic that I don't want to add comments to it :

Then we create our glue function, this is the function that takes care of the link between the LUA engine and your C/C++ program :

This function may require some explanation.  The first part is to verify that the stack contains enough arguments, LUA doesn't know how many parameters your C/C++ function will take so it will push all the parameters to the stack.  If the scripter has used no parameters than the stack will be empty.

Then we get the first argument from the stack and we verify that it isn't NULL as we only want to work with valid strings :-).

After verifying the string we call the internal alert function (defined above), we foresee two different function calls, the first one is called when there is only one parameter and the second one will include the message box title.  If the function has been called with more than two parameters then the extra ones will be ignored.

At the same time the result of our alert function will be pushed as a number.  The function will exit with the value of 1 and this tells the LUA engine that we have pushed one value the stack that is used as a return value.  Remember, LUA communicates with the stack so return value are pushed on the stack and so the return value of the C function is only to tell LUA how many values the function will return to the script.

Now that we have our glue function, we can register it LUA :

And now that we're done, let's create a new example script that will use our alert function :

 

You can download the project file or executable for this example below.  Enjoy.

 

Calling LUA functions from C/C++

With LUA it is also possible to call LUA functions from C/C++.  This technique can for example be used to allow execution of events by user-defined scripts.

Doing it is pretty simple and requires you only to get the function name and to push the values on the stack.  Our alert function can be called as follows :

And this is how we can call the double function (see our example script above) :

This technique is also demonstrated in the download below.

 

Download

You can download the above test project here (compressed ZIP files) :

The source code of LUA Demo 2
The executable version of LUA Demo 2

NOTE: All the required files are included in the source code file in order to compile succesfully

 

Contact

If you have questions or remarks about this article or its contents, then feel free to contact me at <fibergeek @ codegurus.be>.  Don't forget to remove the white spaces or the e-mail won't arrive.

 

External links

LUA: the official site

 

안정적인 DNS서비스 DNSEver DNS server, DNS service
Posted by 키르히아이스

댓글을 달아 주세요