How to access Package Name from JNI/NDK?

原创 2017年01月03日 15:39:20
#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

相关文章推荐

NDK/JNI Develop Guide (4) How to use Array

NDK/JNI Develop Guide (4) How to use ArrayJNI中的数组分为基本类型数组和对象数组,它们的处理方式是不一样的,基本类型数组中的所有元素都是JNI 的基本数据...

ndk-build JNI头文件错误 error: unknown type name 'JNIEXPORT'

在用 ndk-build编译生成.so文件的时候报一下错误,记录一下: G:\支付宝_支付\WS_APP_PAY_SDK_BASE_2.0\WS_APP_PAY_SDK_BASE_2.0\客户端De...

How to build OTA Package

  • 2017年08月04日 19:35
  • 102KB
  • 下载

matlabr: a Package to Calling MATLAB from R with system

In my research, I primarily use R, but I try to use existing code if available. In neuroimaging and ...

windows下python如何安装模块或包? How to install package or module in windows OS when using PYTHON?

摘要:本文介绍了在windows下利用cmd安装第三方模块module或包package的方法。

[R] How to install RMySQL package on Window

一 Installing RMySQL package under Window 二 Playing with RMySQL一. Installing RMySQL package under Win...
  • COMEYAN
  • COMEYAN
  • 2016年03月09日 04:13
  • 412

How to Create Android Update Zip Package

There are several ways to install applications or  library files to an Android Phone. You can us...
  • hnmsky
  • hnmsky
  • 2012年04月09日 18:03
  • 859

How to download the installation package by ZOL Downer

Recently, Zhongguancun Online distributed theZOL Downer for users to download the resources from ZOL...

How to create an RPM package

How to create an RPM package From FedoraProject Jump to: navigation, search In other la...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:How to access Package Name from JNI/NDK?
举报原因:
原因补充:

(最多只允许输入30个字)