zoukankan      html  css  js  c++  java
  • C++将结构体传给lua .

    C++代码

    void CProject1Dlg::OnBnClickedButton2()
    {
    	// 打开换为 luaL_newstate
    	lua_State *L = luaL_newstate() ; /* 打开 Lua */
    	luaL_openlibs(L); /* 加载 .lib 文件 */
    
    	// 加载脚本文件,需要放在程序目录
    	luaL_loadfile( L, "test.lua" );
    	lua_resume( L, 0 , 0);
    	
    	typedef struct
    	{
    		int a;
    		CString strTest;
    
    		struct test
    		{
    			int b;
    		}c;
    	}A;
    
    	A a;
    	a.a = 10;
    	a.c.b = 322;
    	a.strTest = _T("yes");
    	lua_getglobal(L,"structTest");	
    
    	lua_newtable(L);
    	lua_pushinteger(L,a.a);
    	lua_setfield(L,-2,"a");
    	lua_newtable(L);
    	lua_pushinteger(L,a.c.b);
    	lua_setfield(L,-2,"b");
    	lua_setfield(L,-2,"c");
    	lua_pushstring(L,CStringA(a.strTest).GetString());
    	lua_setfield(L,-2,"strTest");
    
    	lua_pcall(L,1,1,NULL);	// 
    
    	// 输出计算结果
    	CString c = CString(lua_tostring(L,-1)) ;
    	lua_pop(L,1) ;			// 清除堆栈 清除计算结果
    
    	// 调用结束
    	lua_close(L);
    
    	MessageBox(c);
    }

    Lua代码

    function structTest(a)
    
    	return string.format("做个测试 \r\n a.a = %d \n a.b = %d \n a.strTest = %s",
    	a.a,a.c.b,a.strTest);
    
    end


  • 相关阅读:
    6174问题
    阶乘的精确值
    小学生算术
    Primer回顾 数组和指针
    Primer回顾 标准库类型
    绪论
    字符串的存储
    条款39:明智而审慎的使用private继承
    条款34:区分接口继承和实现继承
    条款33:避免遮掩继承而来的名称
  • 原文地址:https://www.cnblogs.com/byfei/p/3112133.html
Copyright © 2011-2022 走看看