[英]Modify Java object field from JNI
i want to declare an int a = 5 in java (android) and modify it using ndk with c/c++ , and change the value of int a in jni , basically its accessing that segment of ram which variable is declared , but i dont know how to do that ?我想在 java (android) 中声明一个 int a = 5 并使用 ndk 和 c/c++ 修改它,并在 jni 中更改 int a 的值,基本上它访问声明了哪个变量的 ram 段,但我不知道怎么做 ?
public class dataclass {
int a = 5;
int b = 5;
static {
System.loadLibrary("native-lib");
}
public native void changeValue(dataclass mclass);
} }
Assuming you declared your changeValue
as a static
function in Java, your native code will receive three parameters: a JNIEnv * env
, a jclass cls
, and jobject obj
.假设您在 Java
changeValue
声明为static
函数,您的本机代码将接收三个参数:一个JNIEnv * env
、一个jclass cls
和jobject obj
。 The latter is the instance of dataclass
you want to manipulate.后者是您要操作的
dataclass
的实例。
The approach is then standard:该方法是标准的:
dataclass
class using env->FindClass("dataclass")
or env->GetObjectClass(obj)
env->FindClass("dataclass")
或env->GetObjectClass(obj)
获取对dataclass
类的引用env->GetFieldID(dataClass, "a", "I")
.env->GetFieldID(dataClass, "a", "I")
修改的字段的句柄。 The I
here is the primitive type associated with int
.I
是与int
关联的原始类型。env->SetIntField(obj, fieldId, new_value)
env->SetIntField(obj, fieldId, new_value)
进行更改
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.