Lua调用C++带参数的方法

C++代码:

// LuaAndC.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"


#include <iostream>  
#include <string>  
using namespace std;  

extern "C"  
{  
    #include "lua.h"  
    #include "lauxlib.h"  
    #include "lualib.h"  
}  


int sayHello(lua_State *L)
{
	string name=luaL_checkstring(L,-1);
	lua_pushnumber(L,name.length());

	return 1; //result count
}

int _tmain(int argc, _TCHAR* argv[])
{
	 //1.创建一个state  
    lua_State *L = luaL_newstate();  
	
	luaL_openlibs(L);
	
	lua_register(L,"sayHello",sayHello);
	luaL_dofile(L,"Hello.lua");

    lua_close(L);  
	
	int i;
	cin>>i;
    return 0 ;  
}


 

Lua代码:

print(sayHello("Hunter"))


 

版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/ggzone/p/4786413.html