How to return data through native methods parameters?

Discussion in 'Java' started by HappyHippy, Sep 7, 2006.

  1. HappyHippy

    HappyHippy Guest

    Hi,

    I have a native method which gets an Integer object as a parameter. I
    want to create a new Integer object in the method's C/C++ implementation
    and return it through the same parameter.

    The code I wrote (it actually fails to return data) looks like this:

    JNIEXPORT jboolean JNICALL
    Java_Java2NativeEngineInterface_startRecognitionNB(JNIEnv *env, jclass,
    jobject nHandle)
    {
    int hResHndl;

    if(sr_startRecognition_nb(&hResHndl) != 0)
    return false;

    jclass objClass = env->GetObjectClass(nHandle);
    jmethodID methodId = env->GetMethodID(objClass, "<init>", "(I)V");

    if(methodId == 0)
    return false;

    //This doesn't work... nHandle does not get assigned to a newly
    created object
    nHandle = env->NewObject(objClass, methodId, hResHndl);

    return true;
    }

    Is there any way to make nHandle point to a new object?

    Thank you!
     
    HappyHippy, Sep 7, 2006
    #1
    1. Advertising

  2. HappyHippy

    Chris Uppal Guest

    HappyHippy wrote:

    > I have a native method which gets an Integer object as a parameter. I
    > want to create a new Integer object in the method's C/C++ implementation
    > and return it through the same parameter.


    You can't. It's that simple.

    The solution is exactly the same as it would be in Java -- i.e. hack it or
    redesign it.

    -- chris
     
    Chris Uppal, Sep 7, 2006
    #2
    1. Advertising

  3. On Thu, 07 Sep 2006 03:07:41 -0400, HappyHippy wrote:
    > I have a native method which gets an Integer object as a parameter.
    > I want to create a new Integer object in the method's C/C++
    > implementation and return it through the same parameter.


    > //This doesn't work... nHandle does not get assigned to a newly created object
    > nHandle = env->NewObject(objClass, methodId, hResHndl);


    Actually it does assign nHandle, but parameters in C are equivalent to
    local variables, so changing what nHandle refers to doesn't have any
    effect on what it refers to in the caller.

    It's the same as if you'd done this:

    void baz(int a) {
    a = a+1;
    System.out.println(a);
    }

    void foo() {
    int a = 10;
    System.out.println(a); /* prints 10 */
    baz(a); /* prints 11 */
    System.out.println(a); /* prints 10 again */
    }

    The value of a is indeed modified by the operation, but it has no
    effect after leaving the method. It's not the *same* a in both places,
    the called method gets a copy.


    > Is there any way to make nHandle point to a new object?


    Not in the caller's scope. The logical choice is to either *return*
    the value, i.e.:

    return env->NewObject(...);

    or pass a container object that the native function can update
    instead.

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 7, 2006
    #3
  4. On Thu, 07 Sep 2006 03:07:41 -0400, HappyHippy wrote:
    > I have a native method which gets an Integer object as a parameter.
    > I want to create a new Integer object in the method's C/C++
    > implementation and return it through the same parameter.


    After looking at this again, I wonder why you don't simply return the
    int (not Integer) you get from sr_startRecognition_nb(), and let the
    Java part of your application deal with creating and assigning the new
    Integer object?

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 7, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Duke
    Replies:
    1
    Views:
    6,212
    Gordon Beaton
    Jun 30, 2003
  2. BARBARA

    native methods from java

    BARBARA, Oct 7, 2003, in forum: Java
    Replies:
    1
    Views:
    397
    Gordon Beaton
    Oct 7, 2003
  3. Greenhorn
    Replies:
    15
    Views:
    839
    Keith Thompson
    Mar 6, 2005
  4. Jason
    Replies:
    2
    Views:
    521
    Jonathan Mcdougall
    May 13, 2006
  5. Kenneth McDonald
    Replies:
    5
    Views:
    329
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page