I have an array of objects which is initialized in Java as shown below:
Record[] pRecords = new Record[5];
ret = GetRecord(pRecords);
I passed this array to the JNI, from JNI it will call CPP, and finally the array will get filled.
JNIEXPORT jint JNICALL Java_GetRecord
(JNIEnv *jEnv, jobject ObjApp, jobjectArray jRecords)
{
Record *pRecords = (Record *)malloc(5*sizeof(Record ));
ret = Get_Record(pRecords); // call to CPP
if(SUCCESS == ret)
{
jclass c = (jEnv)->GetObjectClass(jRecords);
jfieldID fid = (jEnv)->GetFieldID(c, "m_session", "I");
(jEnv)->SetIntField (jRecords, fid, pRecords [0].value);
}
}
I am getting fid NULL. How to assign pRecords[0].value to 0th array of jRecords ?
A jobjectArray is not a pointer to the first element of the array. Remember that Java arrays are themselves first-class objects.
fid is 0 because you're looking for a member m_session in the class that represents an array of the Java class Record; of course the array class has no such member. You need to do a FindClass() to get the Record class, and then look for the member in there.
Then you proceed to try to set that member. If it's actually a member of the Record class, I'd imagine you want to set the value of that member in each array element in a loop, yes? Certainly not in the array itself, as you're trying to do. For each array element you need to call the appropriate method to get the object in that array position, then operate on that object.
Related
I have a jclass and I need to find out the name of the corresponding Java class. There is a well-received answer on SO for a similar question, however, it requires an object that's an instance of this class: https://stackoverflow.com/a/12730789/634821
I don't always have an instance where having the name of a jclass would be very useful, and I find it hard to believe that Java has no way to find a class's name, which is obviously a static property, with only static methods. So, is it doable, and how?
A jclass is a jobject subclass that references a java Class object.
So, cut the first few lines off the answer you found:
jclass input = ...; // Class<T>
jclass cls_Class = env->GetObjectClass(input); // Class<Class>
// Find the getName() method on the class object
mid = env->GetMethodID(cls_Class, "getName", "()Ljava/lang/String;");
// Call the getName() to get a jstring object back
jstring strObj = (jstring)env->CallObjectMethod(input, mid);
// Now get the c string from the java jstring object
const char* str = env->GetStringUTFChars(strObj, NULL);
// Print the class name
printf("\nCalling class is: %s\n", str);
// Release the memory pinned char array
env->ReleaseStringUTFChars(strObj, str);
I am trying to call a java method from cpp. I seem to have no problem using strings, int, etc. One problem I am having those is passing an int array parameter over. Can someone tell me what I did wrong? I apologize if it is a very small error and I just totally missed it.
JNIEXPORT void JNICALL
Java_basket_menu_MenusActivity_submitInfo(JNIEnv *, jclass){
int placement[2] = { 5, 4 };
jclass cls = env->FindClass("basket/menu/MenusActivity");
jmethodID mid2 = env->GetStaticMethodID(cls, "PlaceMe", "([I)V");
env->CallStaticVoidMethod(cls, mid2, placement);
}
You need to create a jintArray and copy the contents of placement to it:
jintArray arr = env->NewIntArray(2);
env->SetIntArrayRegion(arr, 0, 2, placement);
env->CallStaticVoidMethod(cls, mid2, arr);
Refer to the documentation for more information about these functions.
I'm a JNI navice, a issue about release memory has blocked me, the scenario like below:
If you have convert a ArrayList from Java to C, and put the ArrayList elements from String array like:
jclass arrlist_cls = (*env)->FindClass(env,"java/util/ArrayList");
if(NULL==arrlist_cls)return;
jmethodID m_get = (*env)->GetMethodID(env,arrlist_cls,"get","(I)Ljava/lang/Object");
jmethodID m_size = (*env)->GetMethodID(env,arrlist_cls,"size","()I");
//get the ArrayList field from object
jfieldID fidArrStr = (*env)->GetFieldID(env,helloObj,"arrStr",
"Ljava/util/ArrayList;");
jobject ArrObj = (*env)->GetObjectField(env,paramsObj,fidArrStr);
if(NULL==ArrObj)return;
int len = (*env)->CallIntMethod(env,ArrObj,size_method);
int i=0; const char **ArrStr;
for(i=0;i<len;i++){
jstring jstr = (jstring)(*env)->CallObjectMethod(env,ArrObj,get_method,i);
ArrStr[i]=jstr;
}
How can i release it like single String?you know, the single String could use the method of ReleaseStringUTFChars to release,
how can i release a array object memory? Because String is not primitive type, so i can't find ReleaseArrayElements method to use.
I mean how to release the ArrStr.
This code won't even work: it will get a SIGSEGV when you execute ArrStr[i] = ... So I don't know why you're worrying about termination yet.
You need to allocate ArrStr before you can use it. How you allocate it determines how you release it. If you allocate it with new you must release it with delete. If you allocate it as an array, i.e. char *ArrStr[len], it will disappear when the method exits: no release required.
If you're asking about how to deallocate the jstrings returned by CallObjectMethod(), again they are released automatically by JNI when you exit this JNI method. You can release them explicitly prior to that, if necessary, with DeleteLocalRef().
I need to make COM IntetrOp at runtime using reflections. My native COM Object's exposed methods have some parameters as pointers (DWORD*) and some double pointers (DWORD**) and some are user defined types(e.g SomeUDTType objSmeUDTType) and vice versa its pointer(i.e. SomeUDTType *pSomeUDTType).
Now for dynamic method invocation, we have single option for passing parameters as array of object i.e object[] and filling this array statically.
But I need to pass pointers and references and pointers to pointers. For now how can I be able to populate object array as mixed data of simple data types, pointers or references and pointers to pointers.
Working Example:
Native COM exposed method :
STDMETHODIMP MyCallableMethod(DWORD *value_1,BSTR *bstrName,WESContext **a_wesContext)
Translated by tlbimp.exe (COMInterop)
UDTINIDLLib.IRuntimeCalling.MyCallableMethod(ref uint, ref string, System.IntPtr)
Now calling these methods at runtime using reflection at runtime,
See here :
Assembly asembly = Assembly.LoadFrom("E:\\UDTInIDL\\Debug\\UDTINIDLLib.dll");
Type[] types = asembly.GetTypes();
Type type = null;
//foreach (Type oType in types)
{
try
{
type = asembly.GetType("UDTINIDLLib.RuntimeCallingClass");
}
catch (TypeLoadException e)
{
Console.WriteLine(e.Message);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
object parameters = new object[3];
Type CustomType = asembly.GetType("UDTINIDLLib.WESContext");
object oCustomType = Activator.CreateInstance(CustomType);
FieldInfo fieldInfo = CustomType.GetField("MachineName", BindingFlags.Public | BindingFlags.Instance);
string MachineName = "ss01-cpu-102";
string MachineIp = "127.0.0.1";
string Certificate = "UK/78T";
fieldInfo.SetValue(oCustomType, MachineName);
fieldInfo.SetValue(oCustomType, MachineIp);
fieldInfo.SetValue(oCustomType, Certificate);
object obj = Activator.CreateInstance(type);
MethodInfo mInfo = type.GetMethod("MyCallableMethod");
int lengthOfParams = mInfo.GetParameters().Length;
ParameterInfo [] oParamInfos = mInfo.GetParameters();
object[] a_params = new object[lengthOfParams];
int ValueType = 0;
for(int iCount = 0; iCount<lengthOfParams; iCount++)
{
a_params[iCount] = ???; //Now here this array should be populated with corresponding pointers and other objects (i.e WESContext's obj)
}
mInfo.Invoke(obj, a_params);
Hope code will clarifies ...If any any confusion do let me know I'll edit my question accordingly.
I am stuck here , I'll be obliged if you help me out.(I am confused about "dynamic" keyword might hope it solves the problem)
Is there any need to generate C++/CLI wrappers? and if in which context?
Regards
Usman
Just put the values of your arguments directly into the array. For out/ref parameters, the corresponding elements of the array will be replaced by new values returned by the function.
For the double pointer, by far the easiest approach is to use /unsafe and unmanaged pointers, like so (assuming the parameter is used by the method to return a value):
WESContext* pWesContext; // the returned pointer will end up here
IntPtr ppWesContext = (IntPtr)&pWesContext;
// direct call
MyCallableMethod(..., ppWesContext);
// reflection
a_params[3] = ppWesContext;
mInfo.Invoke(obj, a_params);
After you'll get the pointer to struct in pWesContext, you can use -> to access the members in C#. I'm not sure what memory management rules for your API are, though; it may be that you will, eventually, need to free that struct, but how exactly to do that should be described by the documentation of the API you're trying to call.
JNI tutorials, for instance this one, cover quite well how to access primitive fields within an object, as well as how to access arrays that are provided as explicit function arguments (i.e. as subclasses of jarray). But how to access Java (primitive) arrays that are fields within an jobject? For instance, I'd like to operate on the byte array of the following Java object:
class JavaClass {
...
int i;
byte[] a;
}
The main program could be something like this:
class Test {
public static void main(String[] args) {
JavaClass jc = new JavaClass();
jc.a = new byte[100];
...
process(jc);
}
public static native void process(JavaClass jc);
}
The corresponding C++ side would then be:
JNIEXPORT void JNICALL Java_Test_process(JNIEnv * env, jclass c, jobject jc) {
jclass jcClass = env->GetObjectClass(jc);
jfieldID iId = env->GetFieldID(jcClass, "i", "I");
// This way we can get and set the "i" field. Let's double it:
jint i = env->GetIntField(jc, iId);
env->SetIntField(jc, iId, i * 2);
// The jfieldID of the "a" field (byte array) can be got like this:
jfieldID aId = env->GetFieldID(jcClass, "a", "[B");
// But how do we operate on the array???
}
I was thinking to use GetByteArrayElements, but it wants an ArrayType as its argument. Obviously I'm missing something. Is there a way to to this?
I hope that will help you a little (check out the JNI Struct reference, too):
// Get the class
jclass mvclass = env->GetObjectClass( *cls );
// Get method ID for method getSomeDoubleArray that returns a double array
jmethodID mid = env->GetMethodID( mvclass, "getSomeDoubleArray", "()[D");
// Call the method, returns JObject (because Array is instance of Object)
jobject mvdata = env->CallObjectMethod( *base, mid);
// Cast it to a jdoublearray
jdoubleArray * arr = reinterpret_cast<jdoubleArray*>(&mvdata)
// Get the elements (you probably have to fetch the length of the array as well
double * data = env->GetDoubleArrayElements(*arr, NULL);
// Don't forget to release it
env->ReleaseDoubleArrayElements(*arr, data, 0);
Ok here I operate with a method instead of a field (I considered calling a Java getter cleaner) but you probably can rewrite it for the fields as well. Don't forget to release and as in the comment you'll probably still need to get the length.
Edit: Rewrite of your example to get it for a field. Basically replace CallObjectMethod by GetObjectField.
JNIEXPORT void JNICALL Java_Test_process(JNIEnv * env, jclass c, jobject jc) {
jclass jcClass = env->GetObjectClass(jc);
jfieldID iId = env->GetFieldID(jcClass, "i", "I");
// This way we can get and set the "i" field. Let's double it:
jint i = env->GetIntField(jc, iId);
env->SetIntField(jc, iId, i * 2);
// The jfieldID of the "a" field (byte array) can be got like this:
jfieldID aId = env->GetFieldID(jcClass, "a", "[B");
// Get the object field, returns JObject (because Array is instance of Object)
jobject mvdata = env->GetObjectField (jc, aID);
// Cast it to a jdoublearray
jdoubleArray * arr = reinterpret_cast<jdoubleArray*>(&mvdata)
// Get the elements (you probably have to fetch the length of the array as well
double * data = env->GetDoubleArrayElements(*arr, NULL);
// Don't forget to release it
env->ReleaseDoubleArrayElements(*arr, data, 0);
}
In gcc 6.3 I get a warning saying "dereferencing type-punned pointer will break strict-aliasing rules" from a line like this:
jdoubleArray arr = *reinterpret_cast<jdoubleArray*>(&mvdata);
But since jdoubleArray is itself a pointer to class _jdoubleArray, there's no need to get the address before casting, and this static_cast works without warnings:
jdoubleArray arr = static_cast<jdoubleArray>(mvdata);