哪位大佬帮我看一下代码,运行后获得的进程id是错误的?(安卓上运行的程序)
本帖最后由 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;
}
wangda38 发表于 2020-3-25 12:19
你好,代码运行到
if (strcmp(process_name, cmdline) == 0) {
...
我这边正常啊,没毛病啊 int find_pid_of(const char *process_name)
{
int id;
pid_t pid = -1;
DIR* dir;
FILE *fp;
char filename;
char cmdline;
struct dirent * entry;
if (process_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);
fp = fopen(filename, "r");
if (fp) {
fgets(cmdline, sizeof(cmdline), fp);
fclose(fp);
if (strcmp(process_name, cmdline) == 0) {
/* process found */
pid = id;
break;
}
}
}
}
closedir(dir);
return pid;
}
extern "C"
jint Java_com_example_appc_aaa_searchMem(JNIEnv *env, jobject thiz) {
pid_t target_pid;
target_pid = find_pid_of("com.bb.bb");
return pid;
} admin 发表于 2020-3-24 23:02
int find_pid_of(const char *process_name)
{
int id;
你好,代码运行到
if (strcmp(process_name, cmdline) == 0) {
/* process found */
pid = id;
break;
}
这里的包名匹配不对返回得不到id,
我调试后看到的cmdline得到的包名是当前本程序在进程中的包名,
无法得到其他的包名。
大佬请问这个怎么解决 admin 发表于 2020-3-25 17:53
我这边正常啊,没毛病啊
请问你的root权限用c是怎么实现的?我这好像是权限不对
页:
[1]