Solved

JNI: Access a class declared in C++

Posted on 2002-06-17
6
234 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

746 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

15 Experts available now in Live!

Get 1:1 Help Now