dcsimg
Passing Float Array Problem to JNI
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Friday, September 21, 2001 08:30 PM

Hi, I’m having a problem when trying to pass a float arrays from c++ to JNI in DLL. Whenever I try to do that, the results in the java test program(TestData.java) will appear as ZERO. My program structures are shown below. C++ side (project1.cpp) DLL Side (VisionBall.dll) Java Side (TestData.java) Function program A:project1.cpp calling a function in DLL then passed two float number in an array to DLL. . . extern "C" _declspec(dllexport) void SetXYData(float *XY); int main(int argc, char* argv[]) { . . SetXYData(xy); // call and send data to dll . . Function program B:Vis   More>>

Hi,
I’m having a problem when trying to pass a float arrays from c++ to JNI in DLL. Whenever I try to do that, the results in the java test program(TestData.java) will appear as ZERO. My program structures are shown below.

C++ side (project1.cpp)
DLL Side (VisionBall.dll)
Java Side (TestData.java)

Function program A:project1.cpp
calling a function in DLL then passed two float number in an array to DLL.

			
.
.
extern "C" _declspec(dllexport) void SetXYData(float *XY);

int main(int argc, char* argv[])
{
.
.

SetXYData(xy); // call and send data to dll
.
.



Function program B:VisionBall.dll
Has two part, part 1 is a function that will be called by c++ and stored the array number and part two which is a JNI function that will copy the array over.

			
#include "jni.h"

//#include "VisionBall.h"
#include
#include
#include "GlueClass.h"


using std::cout;
using std::endl;


static float GXYData[2];


extern "C" _declspec(dllexport) void SetXYData(float *XY)
{
GXYData[0]= XY[0];
GXYData[1]= XY[1];

//printf("Set : %f %f
",GXYData[0],GXYData[1]);
cout < <"X_Dll:" < <
cout < <"Y_Dll:" < <

}

JNIEXPORT void JNICALL Java_GlueClass_GetXYData(JNIEnv *env, jobject obj, jfloatArray jXYData)
{

float *pxydata = env->GetFloatArrayElements(jXYData, 0 );
pxydata=GXYData;
env->ReleaseFloatArrayElements(jXYData, pxydata, 0);
// printf("Get : %f %f
",GXYData[0],GXYData[1]);

cout < <"X_JNI:" < <
cout < <"Y_JNI:" < <

}



Function program C:TestData.java
To display the content of an array. Which will always shown ZERO.

			
import java.io.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.util.Random;
import java.util.Vector;

import com.arcsinc.hardwarehost.coff.ExtVariableSpec;
import com.arcsinc.hardwarehost.coff.SymbolParams;
import com.arcsinc.remotenode.*;

class GlueClass{
public native void GetXYData(float XYData[]);
static{
System.loadLibrary("VisionBall");
}
}

public class TestData extends AbsRemoteNodeServer implements Runnable
{
Thread TestThread;
int period;

public TestData () throws RemoteException
{
super ();
dataSpec = new Vector (2);
dataSpec.addElement (new ExtVariableSpec ("x", "Remote",
SymbolParams.T_FLOAT,
SymbolParams.DT_NON));
dataSpec.addElement (new ExtVariableSpec ("y", "Remote",
SymbolParams.T_FLOAT,
SymbolParams.DT_NON));
dataVector = new Vector (2);
dataVector.addElement (new Float (0.0));
dataVector.addElement (new Float (0.0));

period = 100;
TestThread = new Thread (this);
TestThread.setPriority ((Thread.currentThread ()).getPriority ()- 1);
TestThread.start ();
}

public void run ()
{
while (true)
{
try {
Thread.sleep (period); }
catch (InterruptedException ie) { }

float XYData[] = new float[2];
GlueClass ts=new GlueClass();
ts.GetXYData(XYData);
System.out.println(XYData[0] +" " +XYData[1]);
try{
dataVector.setElementAt (new Float(XYData[0]), 0);
dataVector.setElementAt (new Float(XYData[1]), 1);
updateAllNodes ();
}catch(Exception e){}
}
}

public Vector getAvailableDataItems () throws RemoteException {
return (Vector) dataSpec.clone (); }

public static void main (String args[])
{

System.setSecurityManager (new RMISecurityManager());
try
{
System.out.println ("Starting the registry...");
Registry reg = LocateRegistry.createRegistry (1099);

String remoteNodeName = new String ("TestNode");
TestData obj = new TestData ();
Naming.rebind(remoteNodeName, obj);
System.out.print ("This server object has been bound to");
System.out.println (" the registry: " + remoteNodeName);
}
catch (Exception e) {
e.printStackTrace(); }
}
}


Function program D: (GlueClass.java)

			
class GlueClass{
public native void GetXYData(float XYData[]);
static{
System.loadLibrary("VisionBall");
}
}



Please Help. It is urgent.
Thanks
haris    <<Less
About | Sitemap | Contact