Lua编程示例(五): C语言对Lua表的读取和添加(lua调用C#)深度揭秘

随心笔谈2年前发布 admin
183 0 0

文章摘要

本文介绍了一种使用Lua和Windows API进行反编译的技术,旨在提取目标DLL的属性值。文章通过C语言实现了一个Lua解释器,用于实现反编译功能。核心内容包括:使用`load_lua`函数加载DLL文件、`getfield`函数获取属性值以及`setfield`函数设置属性值。此外,文章还定义了一个`mycolor`结构体,用于存储颜色信息。通过堆栈操作和堆栈管理,实现了对DLL对象属性的动态读写功能。


#include “stdafx.h”

lua_State *L;

void load_lua(char *filename){
L=luaL_newstate();
luaL_openlibs(L);
if((luaL_loadfile(L,filename) || lua_pcall(L,0,0,0))!=0){
luaL_error(L,”loadfile error! \n %s”,lua_tostring(L,-1));
}
}

double getfield(lua_State *L,char * key){
double res;
//默认栈顶是table,将key入栈
lua_pushstring(L,key);
lua_gettable(L,-2); //查找键值为key的元素,置于栈顶
if(!lua_isnumber(L,-1)){
luaL_error(L,”num get error! %s\n”,lua_tostring(L,-1));
}
res=lua_tonumber(L,-1);
lua_pop(L,1); //删掉产生的查找结果
return res;
}
void setfield(lua_State *L,char *key,double value){
//默认栈顶是table
lua_pushstring(L,key);
lua_pushnumber(L,value);
lua_settable(L,-3); //将这一对键值设成元素
}

struct mycolor{
char *name;
unsigned char red,green,blue;
}Color[]={

© 版权声明

相关文章