Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
i tried to get it from google but no good results.....
Posted
Updated 11-Aug-12 10:10am
v2
Comments
[no name] 11-Aug-12 16:28pm    
What do you mean "no good results"? I got over 20 MILLION results from a simple search. Are you telling us that none of them are any good?
TorstenH. 13-Aug-12 14:41pm    
A lot of JNI SUggestions - but I would like to know what you want to do. I guess there is a better, simpler way to archive the same goal.

A quick search yielded this Java Native Access[^].
Have a look and see if it helps.
 
Share this answer
 
Comments
ahmed tb 12-Aug-12 10:47am    
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
but, this statments make error!!!!
In addition to André's suggestion you may like to look into the Java Native Interface[^].
 
Share this answer
 
You can not use an arbitrary DLL directly from your java program. What you can do is that you write a native DLL as part of your project in C or C++ and then you can use kernel32.dll and other dlls from your native DLL, the native part of your program. Your java program must also has at least one native method that is implemented inside your native DLL. This way via the native method you can call into your native DLL you can easily access low-level stuff.
What you have to do:
Write your java program and create a native method in one of your classes:
NativeTest.java:
Java
package com.xyz;
public class NativeTest {
    public native void doSomeNativeWork();
}


The native method isn't implemented in java. You can compile this as it is. When its compiled run the javah program on your classes that contain native methods:
javah org.xyz.NativeTest

This command generates a C source header files that contains the declaration of the native method implementation:
com_xyz_NativeTest.h:
C++
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_xyz_NativeTest */

#ifndef _Included_com_xyz_NativeTest
#define _Included_com_xyz_NativeTest
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_xyz_NativeTest
 * Method:    doSomeNativeWork
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_xyz_NativeTest_doSomeNativeWork
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif
</jni.h>

Create your DLL project and implement the above function declaration and make sure that it is exported by your DLL as extern "C". Search for a JNI tutorial if you wan to know how to use the JNIEnv stuff and how to handle java strings and the like in your C function, or if you want to create some java strings/objects to return some results to the java side.

After this go back to your java code and load your native DLL:
NativeTest.java:
Java
package com.xyz;
public class NativeTest {
    public native void doSomeNativeWork();

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

The System.loadLibrary() loads your native DLL and binds any of its exported functions to matching native methods inside your java program.

From now you can call the native method from java and inside your native DLL function you can use any other DLLs and their functions too!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900