Forum Stats

  • 3,837,113 Users
  • 2,262,228 Discussions
  • 7,900,208 Comments

Discussions

JNI and call by reference

860915
860915 Member Posts: 1
edited May 11, 2011 6:11AM in Java Native Interface (JNI)
Hello,

In my Java - code I have to use native C - code. The native C - function I use has got the reference parameter “read”.

DAQmxBaseReadAnalogF64(...,&read,...);

I do need the information the reference points on on the java layer.

How do I pass the reference via JNI to Java – code?

I am already returning data.

Here is my code:

JNIEXPORT jfloatArray JNICALL Java_JavaWrapper_readAnalog(JNIEnv *env, jobject obj, jfloat timeout, jint arraySizeInSamps)
{

int32 read;
float64 dataArray[1000]; //Größe sollte ausreichen!

DAQmxBaseReadAnalogF64(taskHandle, arraySizeInSamps, timeout, DAQmx_Val_GroupByScanNumber,dataArray,arraySizeInSamps*2,&read,NULL);

int i=0;
jfloatArray ret = env->NewFloatArray(1000);
jfloat* returnArray = env->GetFloatArrayElements(ret,NULL);


for(i=0;i<1000;i++){
returnArray[i] = dataArray;
}
env->ReleaseFloatArrayElements(ret,returnArray,0);

return ret;

}

Edited by: 857912 on 10.05.2011 12:59

Answers

  • EJP
    EJP Member Posts: 32,920 Gold Crown
    MutableInteger. I think it's an Apache class somewhere, anyway not the JDK.
  • 802316
    802316 Member Posts: 532
    edited May 11, 2011 6:11AM
    In C you often need to pass an array and the length used separately. However in Java the arrays length is part of the array itself.

    It would appear that `read` contains the actual amount of data obtained. If this is the case, you don't need to return it, just return an array which has the right length. (This would also be more efficient)

    Perhaps the code could look like this
    int32 read;
    float64 dataArray[1000]; //Größe sollte ausreichen!
    
    DAQmxBaseReadAnalogF64(taskHandle, arraySizeInSamps, timeout, DAQmx_Val_GroupByScanNumber,dataArray,arraySizeInSamps*2,&read,NULL);
    
    jfloatArray ret = env->NewFloatArray(read);
    jfloat* returnArray = env->GetFloatArrayElements(ret,NULL);
    
    for(int i=0;i<read;i++){
        returnArray[i] = dataArray<em>;<br />
    }<br />
    env->ReleaseFloatArrayElements(ret,returnArray,0);<br />
    <br />
    return ret;
    <pre class="jive-pre"><code class="jive-code">                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
This discussion has been closed.