码迷,mamicode.com
首页 > 编程语言 > 详细

C++ Lua 交互练习!!!!!!!!

时间:2016-01-20 19:03:30      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:

// text.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
 

extern "C"
{    
#include "lua.h"  
#include "lualib.h"
#include "lauxlib.h" 
}
#pragma comment(lib,"lualib.lib")
lua_State * L;
LUALIB_API int textC(lua_State *L)
{
    if(L == nullptr)
    {
        return 0;
    }
    cout<<"This msg from C++"<<endl;
    return 0;
}
LUALIB_API int textC1(lua_State *L)
{
    if(L == nullptr)
    {
        return 0;
    }
    int ax = luaL_checkint(L,-2);
    int bx = luaL_checkint(L,-1);
    cout<<"This msg from C++ ax="<<ax<<"  bx="<<bx<<endl;
    lua_pushnumber(L,ax-1);
    lua_pushnumber(L,bx-1);
    return 2;
}
void text1()
{
    //----------------------------------------------------------------
    cout<<"LUA调用C++函数,带参数"<<endl;
    lua_pushcfunction(L,textC1);
    lua_setglobal(L,"textC1");
    char* str = "E:\\A.lua";
    if (luaL_loadfile(L,str)|| lua_pcall(L,0,0,0))
    {
        const char* error = lua_tostring(L,-1);
        cout<< string(error)<<endl;
        return;
    }
}
void text()
{
//----------------------------------------------------------------
    cout<<"LUA调用C++函数"<<endl;
    lua_pushcfunction(L,textC);
    lua_setglobal(L,"textC");

//----------------------------------------------------------------
    char* str = "E:\\A.lua";
    if (luaL_loadfile(L,str)|| lua_pcall(L,0,0,0))
    {
        const char* error = lua_tostring(L,-1);
        cout<< string(error)<<endl;
        return;
    }
    cout<<"读取LUA中值"<<endl;
    int a = 0;
    int b = 0;
    lua_getglobal(L,"a");
    if (!lua_isnumber(L, -1))
    {
        cout << "-2 error" << lua_isnumber(L, -1) << lua_isnumber(L, -1) << endl;
        return ;
    }
    a = lua_tonumber(L,-1);
    lua_getglobal(L,"b");
    if (!lua_isnumber(L, -1))
    {
        cout << "-2 error" << lua_isnumber(L, -1) << lua_isnumber(L, -1) << endl;
        return ;
    }
    b = lua_tonumber(L,-1);
    cout<<a<<endl;
    cout<<b<<endl;

//------------------------------------------------------------------
    cout<<"读取Table中值"<<endl;
    lua_getglobal(L,"table");
    lua_pushstring(L,"a");
    lua_gettable(L,-2);    //取Table 命令 压入 -2位置 
    if(lua_isnumber(L,-1))
    {
        cout<<"table.a = "<<lua_tonumber(L,-1)<<endl;
    }


//----------------------------------------------------------------
    cout<<"调取LUA中函数"<<endl;
    lua_getglobal(L,"textlua");
    if(!lua_isfunction(L,-1))
    {
        const char* error = lua_tostring(L,-1);
        cout<<string(error)<<endl;
    }
    if(lua_pcall(L,0,0,0))
    {
        const char* error = lua_tostring(L,-1);
        return;
    }

//----------------------------------------------------------------
    cout<<"传一个参数"<<endl;
    lua_getglobal(L,"textlua1");
    if(!lua_isfunction(L,-1))
    {
        const char* error = lua_tostring(L,-1);
        cout<<string(error)<<endl;
    }
    lua_pushstring(L,"111");
    if(lua_pcall(L,1,0,0))
    {
        const char* error = lua_tostring(L,-1);
        cout<<string(error)<<endl;
        return;
    }

//----------------------------------------------------------------
    cout<<"传一个参数,得到两个返回值"<<endl;
        lua_getglobal(L,"textlua1");
    if(!lua_isfunction(L,-1))
    {
        const char* error = lua_tostring(L,-1);
        cout<<string(error)<<endl;
    }
    lua_pushstring(L,"111");
    if(lua_pcall(L,1,2,0))
    {
        const char* error = lua_tostring(L,-1);
        cout<<string(error)<<endl;
        return;
    }
    if(lua_isnumber(L,-1) && lua_isnumber(L,-2))
    {
        const int a = lua_tonumber(L,-2);
        const int b = lua_tonumber(L,-1);
        cout<<"the result is "<<a<<","<<b<<endl;
    }

    return;
}
int main ( int argc, char *argv[] )
{    
    cout<<"读取一行LUA"<<endl;
    char *lua = "print(‘Hello world!‘)";        //创建一个指向lua解释器的指针    
    L = luaL_newstate();        //加载lua标准库
    luaL_openlibs(L);    //加载脚本    
    luaL_loadbuffer(L,lua,strlen(lua),"line");    //运行脚本
    text1();
    text();
    if (LUA_OK!=lua_pcall(L,0,0,0))    {        //调用失败,添加处理    
        }    //关闭并释放资源    
    lua_close(L);    //系统命令,暂停    
    system("pause");
    return 0;
}
--测试代码
if textC then
    textC()
else
 ax,bx = textC1(100,1)
print("ax= " .. ax .."bx= " .. bx)
end
a = 10
b = 11
function textlua()
    print("this is a lua function")
end
function textlua1(value)
    print("this is a lua function,value = " .. value )
    a = 100
    b = 200
    return a,b
end
table = {
        a = 123,
        b = 456
    }

 

C++ Lua 交互练习!!!!!!!!

标签:

原文地址:http://www.cnblogs.com/Black-Crow/p/5145843.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!