Solved

Calling a Java method from a C++ dll

Posted on 2004-08-04
6
369 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 85
servlet web applications   metadata-complete="true" or false 3 191
CSV file parsing thru Java 13 39
American Express @Work site and Java 4 64
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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:
The viewer will learn how to implement Singleton Design Pattern in Java.

756 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