How to access Package Name from JNI/NDK?

来源:互联网 发布:美国 网络战 话语权 编辑:程序博客网 时间:2024/05/16 00:39
#include <jni.h>#include <string>#include <unistd.h>#include <sys/types.h>#include <sys/file.h>#include <dirent.h>#include <stdlib.h>#include <ctype.h>#include <fcntl.h>extern "C"jstringJava_xl_com_myapplication_MainActivity_stringFromJNI(        JNIEnv *env,        jobject /* this */) {    std::string hello = "Hello from C++";    //getuid    char args[4096], path[4096];    pid_t pid = getpid();    snprintf(path, sizeof(path), "/proc/%u/cmdline", pid);    int fd = open(path, O_RDONLY);    if (fd < 0) {        return NULL;    }    ssize_t len = read(fd, args, sizeof(args));    close(fd);    if (len < 0 || len == sizeof(args)) {        return NULL;    }    printf("The package name is %s\n", args);    hello = args;    return env->NewStringUTF(hello.c_str());}

http://man7.org/linux/man-pages/man2/getpid.2.html
https://android.googlesource.com/platform/system/core.git/+/donut-release/toolbox/ps.c

0 0
原创粉丝点击