如何用Lua 实现C++指针 虚函数源码实例。

定义头文件 luahelp.h 如下~

  • 源码是某游戏的脚本脚本辅助。封装DLL注入,实现Lua脚本执行内存字符串的功能。慢慢领悟!

#pragma once
#include <iostream>
#include <Windows.h>
#define RD_M_D(x)   *((DWORD *)(x))
#define RD_M_BYTE(x)   *((BYTE *)(x))
#define RD_M_CHAR(x)  (CHAR*)(x)
#define RD_M_WCHAR(x)  (WCHAR*)(x)
#define RD_M_F(x)   *((FLOAT *)(x))
/************************************************************************/
/* 基质                                                                     */
/************************************************************************/
//#define Lua_Base  RD_M_D(RD_M_D(0x12DF3A0)) //lua_state*L
typedef int (*Glua_dostring)(int, const char *);
typedef void (*Glua_gettable)(int,int);
typedef int (*Gplua_call)(int,int,int,int);
typedef int (*Glua_pushstring)(int,const char *);
typedef int (*Glua_pushnumber)(int,double);
typedef double(*Glua_tonumber)(int,double);
typedef const char*(*Glua_tostring)(int,int);
typedef int(*Glua_isnumber)(int,int);
typedef int(*Glua_toboolen)(int,int);
typedef int(*Glua_settop)(int,int);
typedef int (*Glua_CFunction) (int);
typedef int(*Glua_pushcclosure)(int,int,int);
typedef void(*Glua_settable)(int,int);
typedef int(*Glua_loadfile)(int,const char*);
typedef void(*Glua_remove)(int,int);
typedef void (*Glua_pushvalue)(int,int);
std::string ReadStringFromFile(std::string buf);
class GLua
{
public:
GLua::GLua();
virtual GLua::~GLua();
int lua_dostring (const char *);
void SetLua(int);
bool lua_register(const char *,int);
void lua_getglobal(const char*);
void lua_gettable(int);
int lua_loadfile(const char*);
const char*lua_tostring(int);
int lua_isnumber(int);
void lua_pcall(int,int,int);
void lua_dofile(const char*);
double lua_tonumber(int);
void lua_pop(int);
void lua_pushstring(const char*);
void lua_pushnumber(double);
bool lua_toboolean(int);
void lua_remove(int);
void lua_pushvalue(int);
private:
int GetLua();
Glua_dostring GLua_Dostring;
Glua_gettable GLua_Gettable;
Gplua_call   GpLua_Call;
Glua_pushstring GLua_Pushstring;
Glua_tonumber GLua_Tonumber;
Glua_settop   GLua_Settop;
Glua_settable GLua_Settable;
Glua_pushcclosure GLua_Pushcclosure;
Glua_tostring GLua_Tostring;
Glua_isnumber GLua_Isnumber;
Glua_loadfile GLua_Loadfile;
Glua_toboolen GLua_Toboolen;
Glua_pushnumber GLua_Pushnumber;
Glua_remove  GLua_Remove;
Glua_pushvalue GLua_Pushvalue;
void  HookDobuffer();
private:
DWORD L;
};

接下来是定义源文件。Luahelp.c

#include “luahelp.h”

GLua::GLua()
{
L=0;
HookDobuffer();
GLua_Dostring = (Glua_dostring)GetProcAddress(GetModuleHandle(L”LuaPlus.dll”),”lua_dostring”);
GLua_Gettable = (Glua_gettable)GetProcAddress(GetModuleHandle(L”LuaPlus.dll”),”lua_gettable”);
GpLua_Call=(Gplua_call)GetProcAddress(GetModuleHandle(L”LuaPlus.dll”),”lua_pcall”);
GLua_Pushstring=(Glua_pushstring)GetProcAddress(GetModuleHandle(L”LuaPlus.dll”),”lua_pushstring”);
GLua_Tonumber=(Glua_tonumber)GetProcAddress(GetModuleHandle(L”LuaPlus.dll”),”lua_tonumber”);
GLua_Tostring=(Glua_tostring)GetProcAddress(GetModuleHandle(L”LuaPlus.dll”),”lua_tostring”);
GLua_Settop=(Glua_settop)GetProcAddress(GetModuleHandle(L”LuaPlus.dll”),”lua_settop”);
GLua_Settable=(Glua_settable)GetProcAddress(GetModuleHandle(L”LuaPlus.dll”),”lua_settable”);
GLua_Pushcclosure=(Glua_pushcclosure)GetProcAddress(GetModuleHandle(L”LuaPlus.dll”),”lua_pushcclosure”);
GLua_Isnumber=(Glua_isnumber)GetProcAddress(GetModuleHandle(L”LuaPlus.dll”),”lua_isnumber”);
GLua_Loadfile=(Glua_loadfile)GetProcAddress(GetModuleHandle(L”LuaPlus.dll”),”luaL_loadfile”);
GLua_Toboolen=(Glua_toboolen)GetProcAddress(GetModuleHandle(L”LuaPlus.dll”),”lua_toboolean”);
GLua_Pushnumber=(Glua_pushnumber)GetProcAddress(GetModuleHandle(L”LuaPlus.dll”),”lua_pushnumber”);
GLua_Remove=(Glua_remove)GetProcAddress(GetModuleHandle(L”LuaPlus.dll”),”lua_remove”);
GLua_Pushvalue=(Glua_pushvalue)GetProcAddress(GetModuleHandle(L”LuaPlus.dll”),”lua_pushvalue”);
}
GLua::~GLua()
{

}
void GLua::lua_pushvalue(int n)
{
GLua_Pushvalue(GetLua(),n);
}
void GLua::lua_gettable(int n)
{
GLua_Gettable(GetLua(),n);
}
void GLua::lua_remove(int n)
{
GLua_Remove(GetLua(),n);
}
double GLua::lua_tonumber(int n)
{
return GLua_Tonumber(GetLua(),n);
}
std::string ReadStringFromFile(std::string buf)
{
DWORD RSize;
HANDLE hFile=CreateFileA(buf.c_str(),GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD filesize = GetFileSize(hFile,NULL)+1;
const CHAR *pBuffer = (const CHAR*) malloc(filesize);
ZeroMemory((void*)pBuffer,filesize);
ReadFile(hFile, (void*)pBuffer, filesize, &RSize, NULL);
CloseHandle(hFile);
buf=pBuffer;
free((void*)pBuffer);
return buf;
}
void GLua::lua_dofile(const char*f)
{
lua_dostring(ReadStringFromFile(f).c_str());
}
void GLua::lua_pushstring(const char*n)
{
GLua_Pushstring(GetLua(),n);
}
void GLua::lua_pushnumber(double i)
{
GLua_Pushnumber(GetLua(),i);
}
void GLua::lua_pop(int n)
{
GLua_Settop(GetLua(),-(n)-1);
}
int GLua::GetLua()
{
return L;
}
void GLua::SetLua(int l)
{do
{
__try{
L=RD_M_D(RD_M_D(l));
}__except(1){Sleep(200);OutputDebugStringA(“W”);}
} while (0==L);

}
int GLua::lua_isnumber(int n)
{
return GLua_Isnumber(GetLua(),n);
}
bool GLua::lua_register(const char *FuncName,int pFun)
{
int L=GetLua();
if (L!=0)
{
__try{
GLua_Pushstring(L,FuncName);
GLua_Pushcclosure(L,pFun,0);
GLua_Settable(L,-10001);
return true;
}__except(1){OutputDebugStringA(“lua_Register”);return false;}
}
return false;
}
void GLua::lua_getglobal(const char*name)
{
GLua_Pushstring(GetLua(),name);
GLua_Gettable(GetLua(),-10001);
}
bool GLua::lua_toboolean(int n)
{
int r=0;
if(0!=GetLua()){
r= GLua_Toboolen(GetLua(),n);
if (r==0)
return false;
else
return true;
}
return false;
}
const char*GLua::lua_tostring(int n)
{
__try{
return GLua_Tostring(GetLua(),n);}
__except(1){
return “ERROR :lua_Tostring”;
OutputDebugStringA(“ERROR :lua_Tostring”);
}
}
int GLua::lua_dostring (const char *buf)
{

__try{
if (GLua_Dostring == NULL)
{
return -1;
}
GLua_Dostring(GetLua(),buf);
return 0;
}__except(1){OutputDebugStringA(buf);return -1;}
}
int GLua::lua_loadfile(const char*file)
{

if(GLua_Loadfile(GetLua(),file)|| GpLua_Call(GetLua(), 0, 0, 0))
{
return 1;
}
return -1;
}
void GLua::lua_pcall(int c,int r,int n)
{
GpLua_Call(GetLua(),c,r,n);
}
void GLua:: HookDobuffer()
{
DWORD lua_dowbuffer=(DWORD)GetProcAddress(GetModuleHandle(L”LuaPlus.dll”),”lua_dowbuffer”);//lua_dostring 内部就是调用的 lua_dowbuffer函数
if (lua_dowbuffer)
{
DWORD dwOldProctect;
VirtualProtect((PVOID)(lua_dowbuffer+0x48), 5,PAGE_EXECUTE_READWRITE,&dwOldProctect);
*(BYTE*)(lua_dowbuffer+0x48)=0x90;
*(DWORD*)(lua_dowbuffer+0x48+1)=0x90909090;//90==nopDWORD lua_dowbuffer=(DWORD)GetProcAddress(LuaPlus,”lua_dowbuffer”);//lua_dostring 内部就是调用的 lua_dowbuffer函数
if (lua_dowbuffer)
{
DWORD dwOldProctect;
VirtualProtect((PVOID)(lua_dowbuffer+0x48), 5,PAGE_EXECUTE_READWRITE,&dwOldProctect);
*(BYTE*)(lua_dowbuffer+0x48)=0x90;
*(DWORD*)(lua_dowbuffer+0x48+1)=0x90909090;//90==nop
}
}
}

转载于:https://www.cnblogs.com/mugu/p/6618218.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值