关闭

How to access Package Name from JNI/NDK?

标签: c语言android
166人阅读 评论(0) 收藏 举报
分类:
#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"
jstring
Java_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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:80117次
    • 积分:1122
    • 等级:
    • 排名:千里之外
    • 原创:31篇
    • 转载:26篇
    • 译文:4篇
    • 评论:11条
    文章分类
    最新评论