Lua C++互传结构体实例
转自:http://bbs.csdn.net/topics/350261649
=====main.cpp=======
#include "stdio.h" extern "C"
{
#include "lua/lua.h"
#include "lua/lualib.h"
#include "lua/lauxlib.h"
}; typedef struct
{
int wChairID;
int iHeroID;
int iChosenHeros[];
}
Player; /* LUA接口声明*/
lua_State* L; void Operate(Player &obj)
{
int i; lua_getglobal(L, "PlayOperate"); lua_newtable(L);
lua_pushstring(L, "wChairID");
lua_pushnumber(L, obj.wChairID);
lua_settable(L, -);
lua_pushstring(L, "iHeroID");
lua_pushnumber(L, obj.iHeroID);
lua_settable(L, -); lua_pushstring(L, "iChosenHeros");
lua_newtable(L);
for (i=;i<;++i)
{
lua_pushnumber(L, i);
lua_pushnumber(L, obj.iChosenHeros[i]);
lua_settable(L, -);
}
lua_settable(L, -); lua_call(L, , ); lua_pushstring(L, "wChairID");
int n=lua_gettop(L);
lua_gettable(L, -);
obj.wChairID = (int)lua_tonumber(L, -);
lua_pop(L, );
lua_pushstring(L, "iHeroID");
lua_gettable(L, -);
obj.iHeroID = (int)lua_tonumber(L, -);
lua_pop(L, );
lua_pushstring(L, "iChosenHeros");
lua_gettable(L, -);
for (i=;i<;++i)
{
lua_pushnumber(L, i);
lua_gettable(L, -);
obj.iChosenHeros[i]=(int)lua_tonumber(L, -);
lua_pop(L, );
} } int main(int argc, char *argv[])
{
int i;
Player obj; obj.wChairID = ;
obj.iHeroID = ; for(i=; i<; ++i)
obj.iChosenHeros[i]=; //print initial value
printf( "The origin is blow:\n");
printf( "obj.wChairID = %d\n", obj.wChairID);
printf( "obj.iHeroID = %d\n", obj.iHeroID);
for(i=; i<; ++i)
printf( "obj.iChosenHeros[%d] = %d\n", i, obj.iChosenHeros[i]); /* initialize Lua */
L = lua_open();
if (NULL == L)
{
return -;
}
/* load Lua base libraries */
luaL_openlibs(L); /* load the script */
luaL_dofile(L, "e:\\aaa.lua"); //这里指定aaa.lua文件的位置 /* call function */
Operate(obj); /* print the result */
printf( "The result is blow:\n");
printf( "obj.wChairID = %d\n", obj.wChairID);
printf( "obj.iHeroID = %d\n", obj.iHeroID);
for(i=; i<; ++i)
printf( "obj.iChosenHeros[%d] = %d\n", i, obj.iChosenHeros[i]); /* cleanup Lua */
lua_close(L); return ; } =============aaa.lua==========
function PlayOperate(x)
x.wChairID = x.wChairID+
x.iHeroID = x.iHeroID+
x.iChosenHeros[]=
x.iChosenHeros[]= return x
end
Lua C++互传结构体实例的更多相关文章
- ctypes 操作 python 与 c++ dll 互传结构体指针
CMakeLists.txt # project(工程名) project(blog-3123958139-1) # add_library(链接库名称 SHARED 链接库代码) add_libra ...
- C#调用C++系列二:传结构体
这一篇记录下C#调用C++的结构体的方式来使用OpenCV的数据格式,这里会有两种方式,第一种是C#传一个结构体和图像的路径给C++,然后C++将图像加载进来,再把传进来的结构体填满即可,第二种是C# ...
- C#中使用反射获取结构体实例
一般用反射获取类对象的实例比较简单,只要类有一个无参构造函数或没有显示声明带参的构造函数即可使用如下代码 static void Main(string[] args) { Type type = t ...
- Qt socket中怎么传结构体?
直接发送和接收结构体,例如:struct A {...};struct A objectA; 发送的时候: tcpSocket->write((char *)&objectA, size ...
- C语言结构体实例-创建兔子
参考裸编程思想. #include <stdio.h> //#include "ycjobject.h" // 颜色定义 #define CL_BLACK 0 #def ...
- C结构体中数据的内存对齐问题
转自:http://www.cnblogs.com/qwcbeyond/archive/2012/05/08/2490897.html 32位机一般默认4字节对齐(32位机机器字长4字节),64位机一 ...
- 转载 C#结构体(struct)和类(class)的区别
转载原地址: http://dotnet.9sssd.com/csbase/art/8 C#结构体和类的区别问题:在C#编程语言中,类属于引用类型的数据类型,结构体属于值类型的数据类型,这两种数据类型 ...
- 深入理解C指针之六:指针和结构体
原文:深入理解C指针之六:指针和结构体 C的结构体可以用来表示数据结构的元素,比如链表的节点,指针是把这些元素连接到一起的纽带. 结构体增强了数组等集合的实用性,每个结构体可以包含多个字段.如果不用结 ...
- Swift 结构体的使用
Swift 结构体是构建代码所用的一种通用且灵活的构造体. 我们可以为结构体定义属性(常量.变量)和添加方法,从而扩展结构体的功能. 与 C 和 Objective C 不同的是: 结构体不需要包含实 ...
随机推荐
- 《JAVA----day01和day02》
1,(在有符号的情况下)若一个二进制数都是1,则对应的十进制数是-1 2,在定义数值类型时,若无特别说明,整数默认:int 小数默认:double 在内存中 占字节数 取值范围 (1)byte:1个 ...
- mysql 易忽略点
- 如何用移动硬盘安装win7 系统
身边没有U盘和光盘,就只有一个移动硬盘.移动硬盘安装系统是怎么进行的.在这里小毛孩来给大家上一课. 前期准备: 1.移动硬盘. 2.win7 32位的操作系统(*.iso). 3.有系统且可开机的电脑 ...
- Unity3D安卓程序中提示窗与常用静态方法封装
Unity3D/安卓封装SDK常用方法 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 1 IO -- - ...
- 51Nod 1072:威佐夫游戏 (威佐夫博奕)
1072 威佐夫游戏 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 有2堆石子.A B两个人轮流拿,A先拿.每次可以从一堆中取任意个或从2堆中取相同数 ...
- File、Directory、Path
File.Directory.Path https://blog.csdn.net/xiaouncle/article/details/52050577 File.Directory.Path是实际开 ...
- CTF之ROT加解密
常见的ROT加密包括ROT5,ROT13,ROT18,ROT47 ROT5:只是对数字进行编码.用当前数字往后数的第五个数字替换当前数字: 例:123sb——>678sb ROT13:只是对字母 ...
- LG3812 【模板】线性基
题意 给定n个整数(数字可能重复),求在这些数中选取任意个,使得他们的异或和最大. \(1≤n≤50,0≤S_i≤2^{50}\) 分析 模板题. 推荐一篇好博客 现在我来证明一下线性基的性质. 性质 ...
- WPF中使用BitmapImage处理图片文件(转)
(1)图片从文件导入 BitmapImage image = new BitmapImage(); image.BeginInit(); image.UriSource = new Uri(filen ...
- 实习第二天-String对象的不可变性-未解决
public class Reverse { public static void main(String[] args) { String c1=new String("abc" ...