测试环境 c4droid 懒人精灵 雷电模拟器 PC DNK-build
DNK-build 编译文件目录
SRC目录保存 要编译的.C文件 luabib目录保存 LUA支持库以及 需要的.SO文件
下面展示 MyLib.c代码
。
// An highlighted block
#include <math.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <unistd.h>//pwrite64 头文件
#include <stdlib.h>//atof 头文件
#include <ctype.h>// tolower toupper 头文件
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PATH 1024
#include <sys/ptrace.h>
static int PagGamePID = 0;
unsigned long hexToDec(const char* addr) {
unsigned long num;
sscanf(addr, "%lx", &num);//4294967296
return num;
}
char* strlowr(char* str)
{
char* orign = str;
for (; *str != '\0'; str++)
*str = tolower(*str);
return orign;
}
char* strupr(char* str)
{
char* orign = str;
for (; *str != '\0'; str++)
*str = toupper(*str);
return orign;
}
int FindPidByProcessName(const char* process_name) {
int ProcessDirID = 0;
pid_t pid = -3;
FILE* fp = NULL;
char filename[MAX_PATH] = { 0 };
char cmdline[MAX_PATH] = { 0 };
struct dirent* entry = NULL;
if (process_name == NULL) {
return -1;
}
DIR* dir = opendir("/proc");
if (dir == NULL) {
return -2;
}
while ((entry = readdir(dir)) != NULL) {
ProcessDirID = atoi(entry->d_name);//将数字文件名转换为int ,转换失败的话返回0;
if (ProcessDirID != 0) {
snprintf(filename, MAX_PATH, "/proc/%d/cmdline", ProcessDirID);// 文件/proc/<pid>/cmdline 为进程的启动命令行。安卓平台的为app包名;
fp = fopen(filename, "r");
if (fp)
{
fgets(cmdline, sizeof(cmdline), fp);
fclose(fp);
if (strncmp(process_name, cmdline, strlen(process_name)) == 0)
{
pid = ProcessDirID;
break;
}
}
}
}
closedir(dir);
return pid;
}
static int Add(lua_State* L) {
double d1 = luaL_checknumber(L, 1);
double d2 = luaL_checknumber(L, 2);
lua_pushnumber(L, d1 + d2);
return 1;//1个结果
}
static int Adr(lua_State* L) {
double d1 = luaL_checknumber(L, 1);
double d2 = luaL_checknumber(L, 2);
lua_pushnumber(L, d1 - d2);