VC++与LUA的交互

add ,f 和 p 为Lua的函数,print 是vc++为lua定义的函数接口,
lua 的版本:lua-5.1.1 库文件源代码  可以到ttp://www.lua.org/ 下载

LUA文件test.lua内容如下

function add ( x, y )
return x + y
end
function f (x, y )
return x * y
end

function p(x, y )
for i=2,10 do
   print("fg"+i)
end
print(x,y,"asdasd","你好啊")
end
 

VC++代码如下


#include <windows.h>
#include <CONIO.H>


extern "C"
{
    #include 
"srclua.h"
    #include 
"srclualib.h"
    #include 
"srclauxlib.h"
}
;

lua_State 
*= lua_open();   

int luaadd (char *fname, int x, int y )
{
    
int sum;
    
/* the function name */
    lua_getglobal(L, fname);

    
/* the first argument */
    lua_pushnumber(L, x);
    
/* the second argument */
    lua_pushnumber(L, y);
    
/* call the function with 2
        arguments, return 1 result 
*/

    lua_call(L, 
21);
    
/* get the result */
    sum 
= (int)lua_tonumber(L, -1);
    lua_pop(L, 
1);

    
return sum;
}



void luak (char *fname, char *x, char *y)
{
    
/* the function name */
    lua_getglobal(L, fname);

    
/* the first argument */
    lua_pushstring(L, x);
    
/* the second argument */
    lua_pushstring(L, y);
    
/* call the function with 2
        arguments, return 1 result 
*/

    lua_call(L, 
21);
    
/* get the result */
    
//sum = (int)lua_tonumber(L, -1);
    lua_pop(L, 1);

    
return;
}



static int average(lua_State *L)
{
/* get number of arguments */

    
int n = lua_gettop(L);
    
double sum = 0;
    
int i;

    
/* loop through each argument */
    
for (i = 1; i <= n; i++)
    
{
    
/* total the arguments */
    sum 
+= lua_tonumber(L, i);
    }



    
/* push the average */
    lua_pushnumber(L, sum 
/ n);

    
/* push the sum */
    lua_pushnumber(L, sum);

    
/* return the number of results */
    
return 2;
}


static int p(lua_State *L)
{
/* get number of arguments */

    
int n = lua_gettop(L);
    
double sum = 0;
    
int i;

    
/* loop through each argument */
    
for (i = 1; i <= n; i++)
    
{
        unsigned 
int k;
        
char l[100]={0};
    
/* total the arguments */
        printf(
"%s ",lua_tolstring(L, i,&k));
    }


    
/* return the number of results */
    
return 0;
}



int main(int argc, char* argv[])
{

    
int sum;


    
/* initialize Lua */
    L 
= lua_open();
    
    luaopen_base(L);
    luaopen_table(L);
    
//luaopen_io(L);
    luaopen_string(L);
    luaopen_math(L);
    

    lua_register(L, 
"print", p);
     
/* load the script */
    luaL_dofile(L, 
"test.lua");

    
/* call the add function */
    sum 
= luaadd("add"1015 );
        
    
/* print the result */
    printf( 
"The sum is %d ", sum );

    sum 
= luaadd("f"1015 );

    
/* print the result */
    printf( 
"The sum is %d ", sum );

    
    luak(
"p""asd""ddd");

    
/* cleanup Lua */
    lua_close(L);
    
    getch();  
    
return 0
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

红火吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值