Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

JNI: Calling Java From C (with Long and integer as parameters)

Thread view: 
Daniel.Boegelein@gmx.de - 17 Apr 2007 10:34 GMT
Hi,

after i've finally solved my last jni problems with stirngs, i've ran
into another one. But now my problem should be a little bit easier ;)

What i want to do ist to call a Java Function From C. The Java
Function expects an int value and a long value as parameters. My
Problem is how to set the long and the int value. I never get the
right values in java. Must i convert the long and int into jlongs /
jint???

Thanks for every tip/help/hint in advance.

My code is:

C:

struct RxParams
{
    XLhandle eventHandle;
    long portHandle;
    const char *callBackName;
    jobject caller;
    JNIEnv *env;
    jstring callBackOrg;
}typedef RXPARAMS;

struct javaObject
{
        jobject ref; //reference to the java object
        jclass jcls; //class id of the java obj
}typedef JAVAOBJECT;

JNIEXPORT jlong JNICALL Java_VectorXLDriverLib_setNotifier(JNIEnv
*env, jclass jcls, jobject portHandle, jint queueLevel , jobject
caller, jstring callBackName)
{
     ..........

     //reserve and set the structure which is passed to the listener
thread
     RXPARAMS prxParams;

      prxParams.eventHandle = eventHandle;
      prxParams.portHandle = ph;
      prxParams.caller = caller;
      prxParams.env = env;
      prxParams.callBackName = (*env)-
>GetStringUTFChars(env,callBackName, 0);
      prxParams.callBackOrg = callBackName;

    listenCanTest(prxParams);
......
}

void listenCanTest(RXPARAMS par)
{
    writeLog("In RxThread\n");
    unsigned int    msgsrx = RECEIVE_EVENT_SIZE;
       jsize count;
    jmethodID jmid;
    jclass icls;
    jboolean continueListening = 1;

    char string[50];
    JAVAOBJECT eventObj; //object used for callBack to java

    RXPARAMS prxParams = par;

    JNIEnv *env = par.env;

     writeLog("Before get Structure XLCANEvent\n");
    // Klasse des aufrufenden Java Objektes ermitteln:
    icls = (*env)->GetObjectClass(env,  prxParams.caller);

    writeLog("before getMID: icls: %02x\n", icls);
       jmid = (*env)->GetMethodID(env, icls,
"internalTestCallBack","(IJ)Z");

    writeLog("after getMID\n");
    if (jmid == 0){
        writeLog("jmid == NULL\n");
    }

      int i = 10;
     int l = 15;

  jint test = i;
  jlong testlong = l;

continueListening = (*env)->CallBooleanMethod(env, prxParams.caller,
jmid, eventObj.ref, &test, &testlong);

#ifdef DEBUG
   writeLog("Listener has been terminated");
#endif

}

Java:

public class VectorCanLib  implements Runnable{

       //Returns the Id of the gen
       public synchronized long
registerListener(VectorXlLibraryNotifier
callBack)
       {

               closeNotify = false;

               if(notifyThread == null){
                       notifyThread = new Thread(this);
                       notifyThread.start();
               }

               listeners.add(callBack);

               return 0;
       }

       public void run() {

               VectorXLDriverLib.setNotifier(portHandle, queueLevel,
this,
"internalNotifyCallback");

               System.out.println("Internal notifier closed");
       }

        private synchronized boolean internalTestCallBack(int i, long l)
    {
        System.out.println("I: " + i + "L: " + l);

        return true;
    }

}

public abstract class VectorXLDriverLib{

       [.....]

       public static native long setNotifier(XLPortHandle portHandle,
int
queueLevel, Object caller, String callBackName);

       static {
       try{
               System.load(System.getProperty("user.dir") + "\\" +
"MyDll.dll");
       }
       catch(Exception e)      {
               e.printStackTrace();
       }

}
Gordon Beaton - 17 Apr 2007 11:40 GMT
>    jint test = i;
>    jlong testlong = l;
>
>  continueListening = (*env)->CallBooleanMethod(env, prxParams.caller,
> jmid, eventObj.ref, &test, &testlong);

Why are you passing the *address* of test and testlong? Just pass the
values.

/gordon

--
Daniel.Boegelein@gmx.de - 17 Apr 2007 12:01 GMT
> >    jint test = i;
> >    jlong testlong = l;
[quoted text clipped - 8 lines]
>
> --

Thx again for your help gordon. I made the changes you suggested.
Unfortunatelly i still receive the wrong values in java. Any ideas?
Gordon Beaton - 17 Apr 2007 12:14 GMT
>  Thx again for your help gordon. I made the changes you suggested.
> Unfortunatelly i still receive the wrong values in java. Any ideas?

And (I missed this in my previos response) you seem once again to be
passing more arguments than the method is expecting. What is
eventObj.ref doing there?

/gordon

--


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.