Results 1 to 20 of 20
  1. #1
    raedbenz is offline Member
    Join Date
    Jun 2012
    Posts
    9
    Rep Power
    0

    Default Intercept native methods

    hello,

    I want to change the data type returns, Is it possible ?
    Can I do this by intercepting a native method ?? if yes please shed some light.

    Thanks

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Intercept native methods

    What do you mean by 'change the data type returns'?
    You could wrap the existing native calls and do whatever you want.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    raedbenz is offline Member
    Join Date
    Jun 2012
    Posts
    9
    Rep Power
    0

    Default Re: Intercept native methods

    Quote Originally Posted by Tolls View Post
    What do you mean by 'change the data type returns'?
    You could wrap the existing native calls and do whatever you want.
    one of the native functions returns a string for a stream of bytes. If a zero exists the returned string will be incomplete. So i am thinking to make return type char[] or byte[].
    So how can i get around this issue ?
    How do i do the wrapping ?
    Thanks

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Intercept native methods

    Why is your native function returning a String if it should be a byte[]?
    Has the Java side been mis-translated?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    raedbenz is offline Member
    Join Date
    Jun 2012
    Posts
    9
    Rep Power
    0

    Default Re: Intercept native methods

    Quote Originally Posted by Tolls View Post
    Why is your native function returning a String if it should be a byte[]?
    Has the Java side been mis-translated?
    i dont know..it is a 3rd party package i am dealing with.
    Any ideas how to get round it. ?
    Thanks

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Intercept native methods

    So all you can do is wrap the call in something that'll translate it.
    If the 3rd party stuff returns a String and you want a byte[] then:
    Java Code:
    public byte[] wrappedMethod() {
       String result = nativeMethodCall();
       return result.getBytes(<whatever character set the String is in>);
    }
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    raedbenz is offline Member
    Join Date
    Jun 2012
    Posts
    9
    Rep Power
    0

    Default Re: Intercept native methods

    Quote Originally Posted by Tolls View Post
    Java Code:
       String result = nativeMethodCall();
    Thanks for the reply.
    The nativeMethodCall() above in first place returns a String which means the byte stream will be incomplete if a 0x00 is met. is that right ? this is the problem i am trying to solve. hints?
    I do have access to the binary file and i can tell the bytes returned are incomplete.

    Thanks

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Intercept native methods

    If the native method returns a String wirth 0x00 in it then that is what you will receive.
    Just look at the bytes in Java, as above.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    raedbenz is offline Member
    Join Date
    Jun 2012
    Posts
    9
    Rep Power
    0

    Default Re: Intercept native methods

    Quote Originally Posted by Tolls View Post
    If the native method returns a String wirth 0x00 in it then that is what you will receive.
    Just look at the bytes in Java, as above.
    Hello

    I tried the byte[] but no joy.
    My initial thinking was that: Since the String in Java wrapper classes correspond to a char* pointer in native functions then I believe there should be a way to get the full string pointed to.
    What do you think ?
    Thanks

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Intercept native methods

    Which is what is contained in the String.
    It wouldn't work otherwise.
    What is the value you are getting back in the String, when printing out the bytes?

    What is happening when you try and get the byte[]?
    You haven't said.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    raedbenz is offline Member
    Join Date
    Jun 2012
    Posts
    9
    Rep Power
    0

    Default Re: Intercept native methods

    Quote Originally Posted by Tolls View Post
    Which is what is contained in the String.
    It wouldn't work otherwise.
    What is the value you are getting back in the String, when printing out the bytes?

    What is happening when you try and get the byte[]?
    You haven't said.
    Hello,

    I did manage to do the bytes by changing the return value of the native method from string to byte[];
    But the value is the again the incomplete string e.g. 11 22 33 00 where it should be 11 22 33 00 AA FF.
    This means that the native function is returning a string (zero terminated).
    Hints?
    Thanks

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Intercept native methods

    So, complain to whoever wrote that code.
    You are calling a native method which is returning the wrong thing, at least according to you.
    The only solution we can give to that is to get someone to rewrite that method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    raedbenz is offline Member
    Join Date
    Jun 2012
    Posts
    9
    Rep Power
    0

    Default Re: Intercept native methods

    Will this library be beneficial in my case ?
    Thanks for your effort

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Intercept native methods

    "Pure Java agent to intercept native method calls to handle their implementation in Java code rather than native code."
    That says it is for redirecting calls to a native method so that they go to a Java method instead.
    So unless you want to rewrite what this 3rd party package does then this is not what you want.

    Honestly, you need to talk to whoever wrote this, or see what the documentation says about its interaction with Java.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    raedbenz is offline Member
    Join Date
    Jun 2012
    Posts
    9
    Rep Power
    0

    Default Re: Intercept native methods

    Hello,

    I want to double check what i am doing is correct.
    If in a Java package there is a TestImp.java and has the method:
    Java Code:
    public native String getTestValue()
     throws Exception;
    and in the dll files there is an exported function called package_imp_getTestValue.
    So how the java method is mapped to the dll exported function ? is there a wrapper in between ?


    Thanks

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Intercept native methods

    You can jump around the original native method all you want, but it returns whatever it returns and if it's a zero terminated String then that's what it returns, no matter how you call the type of the returned value. If you want zeros in your String (or char array or whatever) you have to change the original native method. There's no magic involved.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    raedbenz is offline Member
    Join Date
    Jun 2012
    Posts
    9
    Rep Power
    0

    Default Re: Intercept native methods

    Ok thanks ;)
    could you please answer me this
    So how the java method is mapped to the dll exported function ? is there a wrapper in between ?
    Thanks

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Intercept native methods

    Quote Originally Posted by raedbenz View Post
    Ok thanks ;)
    could you please answer me this
    Thanks
    If you're using JNI, most likely there's a small wrapper involve that trampolines to the final native code method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Intercept native methods

    You've definitely got a jumping metaphor going at the moment, Jos.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Intercept native methods

    Quote Originally Posted by Tolls View Post
    You've definitely got a jumping metaphor going at the moment, Jos.
    Don't jump to conclusions ...

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. intercept a c++ IO pipeline??
    By blackfox in forum Threads and Synchronization
    Replies: 7
    Last Post: 01-07-2012, 10:51 AM
  2. intercept ! SET library
    By nigamsir in forum Networking
    Replies: 0
    Last Post: 03-20-2010, 06:34 AM
  3. Intercept key type
    By carderne in forum New To Java
    Replies: 3
    Last Post: 07-23-2009, 03:57 PM
  4. Intercept CVS/SVN team commit
    By dilipg003 in forum Eclipse
    Replies: 2
    Last Post: 05-12-2009, 07:17 PM
  5. Loading a library (for native methods)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-23-2007, 08:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •