I have a very complicated and big DLL written in C++ for Win32 by developers from other departments, which need to be used by my java-processes. What I have:
Restrictions: I'm not allowed (though I have a .sol file) to make any changes to that DLL.
Goal: I want my java-processes to be able to call some methods from this DLL.
Instruments: First I want to try it with JNA (second witn JNI).
Problem:
I read some general information about JNA and understood, that first you build prototypes of methods you want to call in the interface. Good, the question now is following: methods, which I want to call, take self-defined objects from DLL project as parameters. For example I have a function in DLL like:
__declspec(dllexport) BOOL WINAPI NiceFunction(
Dummy_State *ModuleState, Dummy_Handle Handle, Dummy_Exception &Exception,
LPCTSTR name, DWORD timer);
Where all Dummy_
objects are of classes, defined in this DLL.
Question 1: how to call with JNA such functions in my java process?
Question 2: is it possible to use those objects somehow direct in my java process? Maybe to import somehow the constructor? However the constructors of those objects require often to call constructors of parent class(es).
Question 3: last but not least, my DLL also called two additional DLLs and widely used a data from them (some of Dummy_
objects). Do I need also to import (somehow) them in my java process via JNA or not?
JNA only works with C functions, not C++. The only C++ functions you'll be able to map must have been externalized with extern "C"
. If they are not, and you are unable to change the DLL, you may have to write your own DLL wrapper which has extern "C"
versions.
How to call with JNA such functions:
You must map them to a Java interface. For example:
public interface ComplicatedAndBigLibrary {
// Create an instance to access the mapped functions with
// the String in quotes is the filename, minus .dll
ComplicatedAndBigLibrary INSTANCE =
(ComplicatedAndBigLibrary) Native.load("compbiglib", W32APIOptions.DEFAULT_OPTIONS);
// Map the C structures or typedefs
// Use "extends" as needed for parents
@FieldOrder({"field1", "field2"})
class Dummy_State extends Structure {
// map structure fields here
public int field1;
public byte field2;
}
class Dummy_Handle extends WinNT.HANDLE {
// any overrides you need
}
// Map your functions (that were externalized)
boolean NiceFunction(
Dummy_State ModuleState, Dummy_Handle Handle, Dummy_Exception ExceptionPointer,
LPCTSTR name, DWORD timer);
}
Take a look at the JNA source code in the com.sun.jna.platform.win32
package for many types of examples based on Windows DLLs. You're essentially doing the same thing for your DLL.
Use those objects somehow direct in my java process
ComplicatedAndBigLibrary CABL = ComplicatedAndBigLibrary.INSTANCE;
CABL.NiceFunction(foo, bar, ...)
Also called two additional DLLs and widely used a data from them
You'll need to map the other DLL's similarly (create an interface, use Native.load()
and map functions/constants/etc.)