Solved

JNI: Access a class declared in C++

Posted on 2002-06-17
6
236 Views
Last Modified: 2012-05-05
Dear all,

I have a class declared in C++ and I want to access it in Java code. The problem is I need several instances of the class, so I can't make a global reference to it.

Is there any method that I can pass a pointer of C++ to the Java code? And I need to do something with the class in Java code.

Thank you.

pigpig
0
Comment
Question by:pigpig
  • 3
  • 3
6 Comments
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 7086189
Hi,

You need to write JNI wrapper for your C++ class. Usually C++ pointer fits into java int, so your java class would look like:

class JavaWrapper{
    private int cppClass;
    public JavaWrapper(){
        init();
    }
    public native void doSomething();
    private native void init();
}

On JNI side, create instance of C++ class in init() implementation and store it in the cppClass field. In other methods you can get cppClass fiels and cast it back to C++ pointer type.

Possibly you will need to take care of memory issues--and it's non-trivial. In java, you know when you need new instance, but don't know when you don't need it anymore. You can use finalize() or weak references to clean up your C++ memory.

Regards,
Igor Bazarny,
Brainbench MVP for Java 1
 
0
 

Author Comment

by:pigpig
ID: 7097757
Hello bazarny,

Do you have some sample codes in doing the pointer conversion in C++? I have some trouble in it.

thx
pigpig
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 7098103

What kind of problem do you have? Is it access to java field or pure C++ problem? I think you just need to cast pointer to long and set value to java field. When you need it back, cast long to same pointer type (unless you are very strict about inheritance/virtual functions use, numeric values of different pointer types pointing to tha same instance may differ)

Regards,
Igor Bazarny


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:pigpig
ID: 7098184
Can u see my problem?
I have a piece of code like that in C++:

#include <iostream.h>

class A
{
public:
     A(int n) : no(n) {}
     int no;
};

void main()
{
     A *a = new A(9);
     
     int apt = (int) &a;

        A *b = (A *)&apt;
        cout << b->no << endl;

     delete a;
}
0
 
LVL 7

Accepted Solution

by:
Igor Bazarny earned 100 total points
ID: 7098249

> int apt = (int) &a;
you take address of a itself. It should be apt = (int)a;
> A *b = (A *)&apt;
Here again, you take address of apt and convert it into pointer. Write: A* b = (A*)apt;
0
 

Author Comment

by:pigpig
ID: 7109905
done !!!!!!!!
thx very much~~~~~~
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now