中级会员
- 资源币
- 3
- 积分
- 232
- 贡献
- 0
- 在线时间
- 60 小时
- 注册时间
- 2020-3-20
- 最后登录
- 2023-1-20
|
5资源币
本帖最后由 wangda38 于 2020-3-24 19:46 编辑
///根据包名查找游戏进程pid
int getPID(const char *pack_name) {
int id = -1, pid = -1;
DIR *dir = 0;
FILE *file = 0;
char filename[32] = {0};
char cmdline[256] = {0};
struct dirent *entry = 0;
if (pack_name == NULL) {
return -1;
}
dir = opendir("/proc");
if (dir == NULL) {
return -1;
}
while ((entry = readdir(dir)) != NULL) {
id = atoi(entry->d_name);
if (id > 0) {
sprintf(filename, "/proc/%d/cmdline", id);
file = fopen(filename, "r");
if (file) {
fgets(cmdline, sizeof(cmdline), file);
fclose(file);
if (strcmp(pack_name, cmdline) == 0) {
pid = id;
break;
}
}
}
}
closedir(dir);
return pid;
}
extern "C"
jint Java_com_example_appc_aaa_searchMem(JNIEnv *env, jobject thiz) {
char *game = "com.bb.bb"; //包名
int pid = getPID(game);
return pid;
}
|
|