C callback Lua reference, Lua callback c Function

Source: Internet
Author: User

C code:

# Include "windows. H "<br/> # include <iostream. h> </P> <p> extern "C" {<br/> # include "Lua. H "<br/> # include" lualib. H "<br/> # include" lauxlib. H "<br/>}</P> <p> # pragma comment (Lib," lua5.1.lib ") </P> <p> lua_state * l; </P> <p> static int Foo (lua_state * l) {<br/> const char * buff = lual_checkstring (L,-1 ); <br/> printf (buff); <br/> return 0; /* Number of Results */<br/>}</P> <p> static int cLib (lua_state * l )// The C function called by Lua must be defined as static int XXX (lua_state * l) <br/>{< br/> const char * buff = lual_checkstring (L,-1 ); <br/> // a value is displayed from the top of the stack. <br/> lua_pop (L, 1); <br/> // lua_pop (L,-1 ); // clear the stack </P> <p> // create an index and pop up the object from the top of the stack <br/> int ref = lual_ref (L, lua_registryindex ); <br/> // obtain the Lua object based on the index and press the stack <br/> lua_rawgeti (L, lua_registryindex, ref); <br/> If (! Lua_isfunction (L,-1) <br/>{< br/> printf ("call back function is not valid: % d", ref ); <br/> return 0; <br/>}< br/> // press-in parameter <br/> lua_pushstring (L, buff); <br/> lua_pushcfunction (L, (lua_cfunction) Foo); </P> <p> // run <br/> lua_pcall (l, 2, 0, 0 ); <br/> const char * err = lual_checkstring (L, 1); <br/> printf ("% d, err: % s/n", lua_gettop (L ), err); <br/> lual_unref (L, lua_registryindex, ref); <br/> return 0; // Why is 1 returned? This is justified. The function pushes the result to the stack, lua calls this function to <br/> // obtain 1 result from the stack */<br/>}</P> <p> static const lual_reg lengine [] = {<br/> {"cLib ", cLib}, <br/> {null, null}, <br/>}; </P> <p> int main () <br/>{< br/> // create a pointer to the Lua interpreter <br/> L = lual_newstate (); <br/> // load the Lua standard library <br/> lual_openlibs (l); <br/> // register the C ++ function <br/> lua_register (L, "cLib", cLib); <br/> // load the script <br/> lual_register (L, "lengine", lengine); <br/> lual_dofile (L, "1.lua"); <br/> // call the function <br/> // lua_getglobal (L," run "); <br/> // run the function and push the result to the stack <br/> // lua_pcall (L, 0 ); <br/> // close and release resources <br/> lua_close (l); <br/> return 0; <br/>}

 

 

Lua code:

Local B ={}< br/> function B. A (STR, cfunc) <br/> Print ("OK") <br/> Print ("Lua ".. str) <br/> cfunc ("This is C function/N") <br/> end </P> <p> lengine. cLib (B. a, "test") <br/>

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.