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:
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:
#include <jni.h>
#ifndef _Included_com_xyz_NativeTest
#define _Included_com_xyz_NativeTest
#ifdef __cplusplus
extern "C" {
#endif
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:
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!