Link to home
Start Free TrialLog in
Avatar of lior_shapira
lior_shapira

asked on

access native(existing) dll's from java

I need to access an existing dll(i dont have the source) from java, and call a procedure "char[] dosomething(char[])", how do i do this through JNI (or otherwise) ?
Avatar of ovidiucraciun
ovidiucraciun
Flag of United States of America image

otherwise ;)
use ms.* microsoft java packages
Avatar of Jim Cakalic
I would _really_ recommend reading the JNI thread of the Java Tutorial. It is free and can be read online or downloaded. This will give you a decent overview. If you need more info, I'd suggest buying either the Addison-Wesley JNI Programmer's Guide or Essential JNI (can't remember the publisher).

To get you started, here is an extremely simplified explanation of what you need to do for JNI access to an existing dll.

First, write a Java class encapsulating the access from the Java side. This class will declare one or more native methods and (likely) have a static initialization block to load the library:

    // you can put it in any package you want
    package util.native;
    public class NativeAccess {
        public native char[] doSomething(char[]);

        static {
            System.loadLibrary("native.dll");
        }
    }

Compile the class. Run javah to produce a C/C++ header file. In this case, if you run it correctly, javah will produce util_native_NativeAccess.h.

Now write your C or C++ file to implement this native method.

    #include "util_native_NativeAccess.h"
    JNIEXPORT jcharArray JNICALL Java_util_native_NativeAccess_doSomething(
JNIEnv *env,
jobject java_this,
jcharArray param) {
    // method implementation
}

Now compile the native code as a shared library, put it in a directory named by your PATH variable, and you are ready to run.

There are special rules for using java arrays in native code. The Java Tutorial briefly addresses this and provides some sample code. I know its all a little confusing at first. Find some samples (in the tutorial, the books, or from searching the web) and study them. IMHO, this is the best way to see how it all fits together.

Best regards,
Jim Cakalic



Avatar of lior_shapira
lior_shapira

ASKER

as i said, i need to use an EXISTING dll, i dont need new native code
ASKER CERTIFIED SOLUTION
Avatar of Jim Cakalic
Jim Cakalic
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial