Solved

Calling a Java method from a C++ dll

Posted on 2004-08-04
6
366 Views
Last Modified: 2013-11-23
Hi,
I have gone through a number of JNI tutorials, but none of them have good enough samples of how to call java Methods from a C or C++ dll. I am running Windows XP, with j2sdk 1.4.2_05 and VC++ 6.0. Can somebody please give me a good example of how to do this, with step by step instructions.
Thanks and best regards,
Advait
0
Comment
Question by:advait75
  • 2
6 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 168 total points
ID: 11714006
0
 
LVL 35

Expert Comment

by:girionis
ID: 11714037
0
 
LVL 3

Assisted Solution

by:TILL
TILL earned 166 total points
ID: 11714064
JNI is basically a nice way to include in you java application calls to routines written in C++, which is a OS dependent language. Once you make a call, you can pass a certain Java Object as a function argument. This object can be cached on the c++ dll, and later be used to perform reverse callings. There are a couple of JNI functions  that you have to use in order to perform this reverse function call. If you look into the url provided by girionis, you'll find a section called "Calling Java Methods". Here you get this short sample:

Java_Callbacks_nativeMethod(JNIEnv *env, jobject obj, jint depth)
{
    jclass cls = (*env)->GetObjectClass(env, obj);
    jmethodID mid = (*env)->GetMethodID(env, cls, "callback", "(I)V");
    if (mid == 0) {
        return;
    }
    printf("In C, depth = %d, about to enter Java\n", depth);
    (*env)->CallVoidMethod(env, obj, mid, depth);
    printf("In C, depth = %d, back from Java\n", depth);
}


This samples shows only how to call a java method from a native method of the same object(Class). If you add a seccond parameter to the native method, which would be another object, you can call a method of that object, or you can store the method/object reference for later use.

If you need more directions with that, please feeel free to ask.
Regards,
TILL.
0
 

Assisted Solution

by:3v0luti0n
3v0luti0n earned 166 total points
ID: 11731248
To do this, you have to use the Invocation API, which is kind of like the other end of JNI.  TILL's right about how to call a JNI method, but doesn't explain where you get that pesky "env" variable in the first place.  You do this by creating a Java Virtual Machine, nabbing its environmnent, and then passing that as the "env" variable to call the functions just like mentioned above.  The URL girionis listed is good, but here's a more specific one for how to call methods (take particular note of the part on how to get method signatures):

http://java.sun.com/docs/books/tutorial/native1.1/implementing/method.html

Here's a a simple example of how to create the JVM (in C++):

void main(int argc, char* argv[])
{
    JNIEnv_ *env;
    JavaVM *jvm;
    JDK1_1InitArgs vm_args;
    jint res, ret = 0;

    jclass cls = NULL;
    jmethodID cid = NULL;
    jobject obj = NULL;

    JNI_GetDefaultJavaVMInitArgs(&vm_args);

    //Specifies JDK 1.4.
    vm_args.version = 0x00010004;

    //Create the Java VM
    res = JNI_CreateJavaVM(&jvm, (void**) &env, &vm_args);
    if (res < 0) {
        cout << "Can't create Java VM\n" << res;
            cin >> res;
        return;
    }

/*
 * Remainder of code here.
 */

    jvm->DestroyJavaVM();
}


I don't know much about manipulating the vm_args variable, but I know you can set system properties and other goodies, and I can find out more if you're curious.
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

Suggested Solutions

Title # Comments Views Activity
Systems talking to each other 5 123
Problem to start Neon 20 82
Java Server Faces parameter pass? 6 37
eclipse shortcuts 9 45
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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 …

947 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

22 Experts available now in Live!

Get 1:1 Help Now