Solved

Calling a Java method from a C++ dll

Posted on 2004-08-04
6
365 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bigHeights  challenge 13 55
Books that can get me started on JAVA 2 54
java set up 1 46
What is the latest versions eclipse neon 2 120
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

758 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