lua <--> c++

来源:互联网 发布:如何申请淘宝图片保护 编辑:程序博客网 时间:2024/06/07 12:05

lua与c++互调

c++头文件:

#pragma once#include <windows.h>#include <tchar.h>#include <iostream>#include <list>extern "C"{#include <lauxlib.h>#include <lualib.h>#include <lua.h>}

导入库:

#include "import.h"#pragma comment( lib, "lua5.1.lib" )#pragma comment( lib, "lua51.lib" )

c++代码:

#include "import.h"/////////////////////////////////////////////////////////////////////////////////// int load_lua( const char * lpszFile, int * lpnWidth, int * lpnHeight );int call_c_func( lua_State * L );int main( int argc, const char * argv[] ){    using namespace std;    int nWidth = 0, nHeight = 0;    int nRet = 0;    nRet = load_lua( "d:\\lua\\test.lua", &nWidth, &nHeight );    if( 0 == nRet )    {        cout << "width  = " << nWidth << endl;        cout << "height = " << nHeight << endl;    }    else    {        cout << "failed = " << nRet << endl;    }    return 0;}int load_lua( const char * lpszFile, int * lpnWidth, int * lpnHeight ){    using namespace std;    int nRet = 0;    lua_State * L = lua_open();    if( ( 0 == nRet ) && !L )    {        nRet = 1;    }    if( 0 == nRet )    {        lua_pushcfunction( L, call_c_func );        lua_setglobal( L, "call_c_func" );        luaopen_base( L );        luaopen_string( L );        luaopen_math( L );    }    if( ( 0 == nRet ) && ( 0 != luaL_loadfile( L, lpszFile ) ) )    {        cout << "luaL_loadfile failed" << endl;        nRet = 1;    }    if( ( 0 == nRet ) && ( 0 != lua_pcall( L, 0, 0, 0 ) ) )    {        cout << "lua_pcall failed" << endl;        nRet = 1;    }    if( 0 == nRet )    {        lua_getglobal( L, "f" );        lua_pushnumber( L, 1 );        lua_pushnumber( L, 2 );        nRet = lua_pcall( L, 2, 1, 0 );        if( 0 != nRet )        {            cout << lua_tostring( L, -1 ) << endl;        }    }    if( ( 0 == nRet ) && !lua_isnumber( L, -1 ) )    {        cout << "ret is not a int" << endl;        nRet = 1;    }    if( 0 == nRet )    {        int ret = ( int )lua_tonumber( L, -1 );        lua_pop( L, 1 );        cout << "ret = " << ret << endl;    }    if( 0 == nRet )    {        lua_getglobal( L, "width" );        lua_getglobal( L, "height" );        cout << lua_tostring( L, -2 ) << endl;        cout << lua_tostring( L, -1 ) << endl;    }    if( ( 0 == nRet ) && !lua_isnumber( L, -2 ) )    {        cout << "width is not a int" << endl;        nRet = 1;    }    if( ( 0 == nRet ) && !lua_isnumber( L, -1 ) )    {        cout << "height is not a int = " << lua_tostring( L, -1 ) << endl;        nRet = 1;    }    if( 0 == nRet )    {        *lpnWidth = ( int )lua_tonumber( L, -2 );        *lpnHeight = ( int )lua_tonumber( L, -1 );    }    lua_close( L );    L = 0;    return nRet;}int call_c_func( lua_State * L ){    using namespace std;    double d = lua_tonumber( L, 1 );    double dd = d + d;    cout << "input  = " << d << endl;    cout << "output = " << dd << endl;    lua_pushnumber( L, dd );    return 1;}

lua代码:

-- this is a test-- this ssswidth= 30height = 40function f( x, y )local z = x + yprint( x, y, z )z = call_c_func( z )print( "call_c_func ret = ", z )return x + yend

输出结果:

1       2       3input  = 3output = 6call_c_func ret =       6ret = 33040width  = 30height = 40