單獨(dú)用lua是不夠的,這里我簡(jiǎn)單推出cbuilder版本和lua的結(jié)合運(yùn)用例子一篇。
這個(gè)例子是初步實(shí)現(xiàn)了在cbuilder中調(diào)用lua實(shí)現(xiàn)的函數(shù)的例子,同時(shí),也演示了lua中如何調(diào)用cbuilder中的函數(shù)例子。閑話少說(shuō),把例子帖上來(lái)。
有幾個(gè)方面要注意,在cbuilder中創(chuàng)建一個(gè)工程,把lua中的src導(dǎo)入進(jìn)來(lái),其中,要在單元。h文件中做如下聲明
extern "c"{
#include <lua.h>
#include
<lauxlib.h>
#include <lualib.h>
}//end extern "c"
否則編譯時(shí),cbuilder是不認(rèn)
################################
#include <vcl.h>
#pragma hdrstop
#include
"uni2.h"
//---------------------------------------------------------------------------
#pragma
package(smart_init)
#pragma resource "*.dfm"
tform1
*form1;
//---------------------------------------------------------------------------
__fastcall
tform1::tform1(tcomponent* owner)
: tform(owner)
{
}
/*
提供給lua調(diào)用,有函數(shù)格式要求
在cbuilder中通過(guò)堆棧來(lái)獲得lua的參數(shù),經(jīng)過(guò)該函數(shù)處理后,
也將按棧方式返回,按左序壓入
通過(guò)lua_gettop獲得調(diào)用函數(shù)的傳入?yún)?shù)個(gè)數(shù)。
第一個(gè)參數(shù)在索引
1 的地方,最后一個(gè)參數(shù)在索引 lua_gettop(l)
處。
返回一個(gè)結(jié)果時(shí),也是通過(guò)左序壓到堆棧上(第一個(gè)返回值最先壓入),然后返回這些返回值的個(gè)數(shù)。
在這些返回值之下的,堆棧上的東西都會(huì)被
lua 丟掉。
*/
int mytest_call(lua_state *l)
{
int n =
lua_gettop(l);
//typedef double lua_number;
//lua 中數(shù)字的類型。確省是
double ,可以在luaconf.h 中修改它
lua_number sum = 0;
int i;
for (i = 1; i <= n; i++)
{
//lua調(diào)用該函數(shù)參數(shù)時(shí)是從棧上索引為1開(kāi)始
sum +=
lua_tonumber(l, i); // 保證傳入的參數(shù)是數(shù)值類型,否則會(huì)報(bào)錯(cuò)。
}
lua_pushnumber(l, sum);
//返回值壓入棧傳回
return
1;
//返回參數(shù)數(shù)量值
}
/*
調(diào)用lua 中的函數(shù)模式
1.使用lua_getglobal()來(lái)獲得函數(shù),
2.將參數(shù)壓入堆棧,
3.調(diào)用
lua_pcall(),
4.然后處理結(jié)果。
*/
void __fastcall
tform1::button1click(tobject *sender)
{
//加載后要用lua_resume運(yùn)行.
lua_resume(l,0);
//調(diào)用腳本中函數(shù)
lua_getglobal(l, "lua_func1");
//傳給lua_func1參數(shù)1,參數(shù)2
lua_pushnumber(l,
21);
lua_pushnumber(l, 23);
//調(diào)用lua中函數(shù),傳入個(gè)參數(shù),有一個(gè)返回值 ,看lua_call
和lua_pcall區(qū)別
lua_pcall(l, 2, 1, 0);
//取值
lua_number retsum;
//注意返回值是通過(guò)-1來(lái)獲取
retsum = lua_tonumber(l,
-1);
label1->caption =
inttostr((int)retsum);
}
void __fastcall tform1::formclose(tobject *sender, tcloseaction
&action)
{
lua_close(l);
}
void __fastcall tform1::formcreate(tobject
*sender)
{
l =
lua_open();
luaopen_base(l);
luaopen_string(l);
//將主程序中的mytest_call函數(shù)注冊(cè)到lua引擎中,
//腳本用mytest回調(diào)宿主程序
lua_register(l, "mytest",
mytest_call);
//加載腳本
string sfilename =
"lua_test1.lua";
lual_dofile(l,
sfilename.c_str());
}
/*
測(cè)試返回字符串
*/
void __fastcall tform1::button2click(tobject
*sender)
{
//用lua_resume運(yùn)行.
// lua_resume(l,0);
lua_getglobal(l,"lua_fun3");
lua_pushnumber(l,
100);
lua_pcall(l, 1, 1,
0);
label2->caption = lua_tostring(l,
-1);
//測(cè)試調(diào)用lua中全局變量,
lua_getglobal(l,
"retstr");
label3->caption =
lua_tostring(l, -1);
}
############
其中l(wèi)ua_test1.lua測(cè)試文件如下:
retstr="123"
function lua_fun3(ss)
ss
= "aaaaaaaa"
retstr = ss
return ss
end
function lua_func2(val1, val2)
sum = val1+val2
return
sum
end
function lua_func1(val1, val2)
--調(diào)用cbuilder中函數(shù)
val1 =
mytest(val1, val2)
return val1
end
運(yùn)行效果圖:
實(shí)踐源碼見(jiàn):
http://256617.tomore.com/1/44360.html