Forum Stats

  • 3,838,749 Users
  • 2,262,397 Discussions
  • 7,900,750 Comments

Discussions

Accessing a native method whose return type is an object of a Class

860480
860480 Member Posts: 8
edited May 13, 2011 3:21PM in Java Native Interface (JNI)
Hi Everyone,

Here goes my doubt in accessing of NATIVE FUNCTIONS in my JAVA CODE (using JNA)

if the method inside my DLL is declared as follows:
extern "C" __declspec(dllexport) int myMethod();

that means i just need to declare a native equivalent of 'myMethod' to use it in my java code like:

public native int myMethod();

Now i can call the function as follows: int var = myMethod();

//********************Now the MAIN part***********************\\

Inside the DLL, I also have a class along with the method which goes as follows:

class MyClass
{
public:
int var;
};

extern "C" __declspec(dllexport) MyClass __cdecl myMethod()
{
MyClass obj;
obj.var = 45;
return obj;
}
///////////////////////////DLL code ends\\\\\\\\\\\\\\\\\\\\\\\\

I hope you all noticed that now my return type is an object of a class rather than a normal int.

So, now the bigger question is, how I am going to use this native function (myMethod) inside my JAVA code? Previously the return type was just an integer, so it was easy. But now what or rather how?

It's very urgent. So, i would really be grateful if someone can provide me with any kind of solutions or helpful links?

Thanks in advance,

With regards,
Satya Prakash.
«1

Answers

  • 802316
    802316 Member Posts: 532
    You can only pass or return primitives or Java Objects.

    You can create a Java class which has the same fields as your C struct and copy all the fields.
    802316
  • 860480
    860480 Member Posts: 8
    Thanks Peter,

    But can you explain me a little more. As far as i can guess, you mean to say that, if the class inside DLL is :

    Class myClass
    {
    public:
    int var;
    int var1;
    }

    then, I have to create a similar class inside Java with similar fields and then return only the field values from the DLL (in this case the fields are 'var' and 'var1'.) So, I need to use 2 functions inside my DLL, which would be returning both the field values and then manually set them to the corresponding fields inside my Java Class?

    Is it so?
  • 802316
    802316 Member Posts: 532
    That is one way of doing it.

    You can have your plain C/C++ method which works the way it does and another which is actually called from Java which only deals in Java Object and Classes and data is copied back and forth as required before and after the call to the plain C/C++ method.
  • 860480
    860480 Member Posts: 8
    Sorry Peter, I am not able to understand by what you mean. Using an intermediate between my actual C/C++ method and Java code? It Would be great if you could give a small example.

    Thanking you,
  • jschellSomeoneStoleMyAlias
    jschellSomeoneStoleMyAlias Member Posts: 24,877 Gold Badge
    857477 wrote:
    Hi Everyone,

    Here goes my doubt in accessing of NATIVE FUNCTIONS in my JAVA CODE (using JNA)

    if the method inside my DLL is declared as follows:
    extern "C" __declspec(dllexport) int myMethod();

    that means i just need to declare a native equivalent of 'myMethod' to use it in my java code like:

    public native int myMethod();
    No that isn't what it means.

    What it means is that you write a JNI method in C and IT calls the method above.


    >
    So, now the bigger question is, how I am going to use this native function (myMethod) inside my JAVA code? Previously the return type was just an integer, so it was easy. But now what or rather how?
    What are you going to do with the C++ class instance?

    If you need access to the member inside java code then must map to a Java class.
    If you want to use the C++ class in other C/C++ methods and java just needs to pass it around then you can pass a pointer as a java long.
  • EJP
    EJP Member Posts: 32,920 Gold Crown
    What it means is that you write a JNI method in C and IT calls the method above.
    Is that still true in JNA?
  • jschellSomeoneStoleMyAlias
    jschellSomeoneStoleMyAlias Member Posts: 24,877 Gold Badge
    EJP wrote:
    What it means is that you write a JNI method in C and IT calls the method above.
    Is that still true in JNA?
    Nope missed that.
  • jschellSomeoneStoleMyAlias
    jschellSomeoneStoleMyAlias Member Posts: 24,877 Gold Badge
    857477 wrote:
    extern "C" __declspec(dllexport) MyClass __cdecl myMethod()
    {
    MyClass obj;
    obj.var = 45;
    return obj;
    }
    ///////////////////////////DLL code ends\\\\\\\\\\\\\\\\\\\\\\\\

    I hope you all noticed that now my return type is an object of a class rather than a normal int.
    Ah...JNA.

    You have an object on the stack. You return it.
    In C++ that ends up doing a copy.

    Make it easy do a pointer (heap) instead.

    For a pointer the JNA docs explain what to do.
  • 860480
    860480 Member Posts: 8
    jschell wrote:
    857477 wrote:
    Hi Everyone,

    Here goes my doubt in accessing of NATIVE FUNCTIONS in my JAVA CODE (using JNA)

    if the method inside my DLL is declared as follows:
    extern "C" __declspec(dllexport) int myMethod();

    that means i just need to declare a native equivalent of 'myMethod' to use it in my java code like:

    public native int myMethod();
    No that isn't what it means.
    I am sorry jschell, but that is what it means.
    >
    What it means is that you write a JNI method in C and IT calls the method above.
    Native access by JNI was hectic as it involved the complex procedure of creating a header file and so on. In order to avoid it, you can do the same thing by the use of JNA. And i have already mentioned in my doubt how we can call the native method in Java by using JNA.
  • 860480
    860480 Member Posts: 8
    jschell wrote:
    >

    Ah...JNA.

    You have an object on the stack. You return it.
    In C++ that ends up doing a copy.

    Make it easy do a pointer (heap) instead.

    For a pointer the JNA docs explain what to do.
    I am sorry, but i couldn't get you. I mean if the method inside my DLL is:
    __declspec(dllexport) int __cdecl init();

    then my corresponding code in java is:

    Native.register(Path to my DLL);
    public native int init();

    Here the return type is int, so it's like this. But if the return type is an OBJECT of a class rather than a simple int, then my DLL code is:

    class myClass{
    public:
    int x;
    int y; };

    __declspec(dllexport) myClass* __cdecl init(); /////return type is object of the class myClass

    But then how would the corresponding code in java be? (like previously it was "public native int init();")
    This is my doubt.

    One more thing, I also have an equivalent class called myClass in java, but then the same doubt again.

    Waiting for a reply,

    Thanks all.
This discussion has been closed.