中級會員
- 資源幣
- 3
- 積分
- 232
- 貢獻
- 0
- 在線時間
- 60 小時
- 注冊時間
- 2020-3-20
- 最后登錄
- 2023-1-20
|
5資源幣
本帖最后由 wangda38 于 2020-3-24 19:46 編輯
///根據(jù)包名查找游戲進程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;
}
|
|