Browse Prior Art Database

Method For Allowing Java Programs Invoked Through a C/C++ JNI Interface to Execute C/C++Function Pointers.

IP.com Disclosure Number: IPCOM000020418D
Original Publication Date: 2003-Nov-20
Included in the Prior Art Database: 2003-Nov-20
Document File: 3 page(s) / 8K

Publishing Venue

IBM

Abstract

Method for allowing Java programs invoked through a C/C++ JNI interface to execute C/C++ function pointers.

This text was extracted from a PDF file.
This is the abbreviated version, containing approximately 53% of the total text.

Page 1 of 3

Method For Allowing Java Programs Invoked Through a C/C++ JNI Interface to Execute C/C++Function Pointers.

The Java Native Interface allows the Java and C languages to communicate with each other. It lets Java classes to invoke functions in C libraries. It also provides complete Java support for C programs. However, JNI does not provide a means for C function pointers to be used by Java classes created by C programs. This publication will describe how to enable Java classes to use C function pointers.

C function pointers are good to use when implementing a callback. For example, a C API will use function pointers as arguments to allow the client of the API to specify a pointer to a logging function. If the API uses JNI to invoke a method in a Java class with a logging class as a argument, there is no way to pass the function pointer to the method to continue logging. The solution requires a special Java callback class to be used instead of the C function pointer. When the C API creates a Java class and invokes a method with a logging class as a argument, the API will create and pass in the callback class. A method in the callback class will mimic the function pointer's arguments and return value. When the method in the callback class is invoked, it will use JNI to call back to the C API and invoke the function pointer from there.

Here is an example C API with a function that has a function pointer argument: <work_api.cpp>

{

// cached function pointer log_function cached_log_function;

// API function unsigned int performWork(log_function client_log_function) {

// cache the function pointer for the Java callback class cached_log_function = client_log_function; // create a Java class with a method that takes a logging class // create the Java callback class // invoke the method with the Java callback class as an argument return 0;

}

    // this method signature will be generated by Java JNIEXPORT jint JNICALL Java_LogCallback_JNIwriteLogMessage (JNIEnv *env, jobject jobj, jstring jmsg)

{

int rc = 0 // convert the Java string to a C string const char *tempString = env->GetStringUTFChars(jmsg, 0...