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


  • 相关阅读:
    数组索引,内容交换
    查找两个等长升序线性表的中位数
    MarkDown使用小结
    java元注解
    LeetCode OJ:Valid Number
    LeetCode OJ:Insert Interval
    关于博客的格式
    9.Hive Metastore Administration
    3.控制hive map reduce个数
    6.capacity scheduler
  • 原文地址:https://www.cnblogs.com/byfei/p/3112133.html
Copyright © 2011-2022 走看看