1

I'm creating a C++ library, and I want to port it to java. I have a method which gets a RAWINPUTDEVICELIST by calling GetRawInputDeviceList(), and scrolls through the array and converts each item to an instance of a class called Device and adds them to an array, in C++. I want to call this method and convert the contents of the list to an array of java classes, called something like Device, and assign the variables that it contains to the values of the C++ class.

EDIT: Pretend that the array I want to convert contains instances of a C++ class which looks like this:

class CplusplusExampleClass {
public:
    int variable;
};

I want to convert all those instances of that class which is contained in the C++ array to corresponding instances of a java class. Pretend that the java class looks like this:

public class JavaExampleClass {
    public int variable;
}

This is possible to do with the method described in the answer to the question suggested by @Gergely, to just convert the integer and then create a new instance, for each class, but pretend that the classes contains lots of stuff, like objects (class instances), other variables etc.

Is this possible, and then, how can I do it?

Daniel Kvist
  • 3,032
  • 5
  • 26
  • 51

1 Answers1

1

Isn't this what you're after? How to return an array from JNI to Java?

Here are some more detailed examples: https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html#zz-6.2

If you want to create the Java objects from JNI code, that's particularly painful. Something like this:

jclass cls = (*env)->FindClass(env, "...your class...");
jmethodID ctor = (*env)->GetMethodID(env, cls, "<init>", "...your ctor signature...");
jobject obj = (*env)->NewObject(env, cls, ctor, ...);

Plus you should null-check the return values of all these of course.

After that you can create the Java array and add the item to it:

jobjectArray results = (*env)->NewObjectArray(env, 100, cls, NULL);
(*env)->SetObjectArrayElement(env, results, 0, obj);
Community
  • 1
  • 1
Gergely
  • 365
  • 2
  • 7
  • Yes, but there is one problem though. That answer describes how to do it with `int` arrays, but what if I have an array of C++ class instances that I have created myself, and want to convert them to an array of java class instances, which I also have created myself, corresponding to the C++ class instances? EDIT: I'll clarify it in my question! – Daniel Kvist Jan 28 '15 at 17:10
  • I think it should be pretty much the same, except it's an Object array. It depends more on where you want to create it. In Java it's easy, you just call new and then pass down the reference to C++ via JNI. If you want to create the Java objects in C++, that's a lot more complicated, as you'll need to do something like: get a class pointer to your object, get a method pointer to the constructor, invoke the constructor, also create an object array and populate it with your objects - point is, each and every one of these steps can only be done by calling JNI's methods. – Gergely Jan 28 '15 at 17:21
  • What is a ctor signature? – Daniel Kvist Jan 28 '15 at 18:04
  • 1
    It's a string that specifies the arguments and return value of a method (or a constructor in this case - a constructor is just a special method which has the name ), in JNI this is the only way to tell the JVM which particular method you want to get a handler for. Have a look here: http://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/types.html#type_signatures – Gergely Jan 28 '15 at 18:34
  • So, lets say I have a variable, `v`, and I want to pass it to the constructor of a java class, it would go in the field saying "...your ctor signature..."? – Daniel Kvist Jan 30 '15 at 16:05