Solved

Calling a Java method from a C++ dll

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert from a json string array to a Java object 3 81
java example issue 5 68
Pass multiple values or string arrays in java as a parameter 3 74
Java regex 5 22
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
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…
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:
Suggested Courses

710 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