How to pass parameters between JNI and DLL files implemented by C++
the following code is declared in JAVA
private native static Vector< Double > computeSimXML(Vector < String > vDocsPath);
I want to pass the parameter vDocsPath ( defined as Vector < String >) into C++, the code generated by JNI is as follows:(I have implemented some)
/*
* Class: SimXMLModule
* Method: computeSimXML
* Signature: (Ljava/util/Vector;)Ljava/util/Vector;
*/
JNIEX开发者_如何学CPORT jobject JNICALL Java_SimXMLModule_computeSimXML
(JNIEnv *, jclass, jobject)
{
vector<double> dist;
dist.push_back(5.0);
dist.push_back(6.0);
}
How can I get the value of vDocsPath
through jobject
, and return dist ( defined as Vector< Double >
in JAVA) to JAVA?
This isn't a good function for starting with JNI - implementing it correctly will take some work.
- The
Vector<String>
parameter is a plain non-genericVector
runtime. - The
Vector<Double>
return value is a plain non-genericVector
runtime - You need to retrieve class and method IDs, and call
Vector
functions to get data out of the parameter - You need to construct a series of
Double
objects, set the internaldouble
and callVector
methods to setup the return value.
I'd recommend you changed the function signature to:
private native static double[] computeSimXML(String[] vDocsPath);
The JNI interface for working with arrays is a lot simpler than what you're trying to do. You can iterate over parameters with GetArrayLength
and GetObjectArrayElement
, and you can create and manipulate the return value with NewDoubleArray
, GetArrayElements
and ReleaseArrayElements
精彩评论