I am trying to access the methods of a DLL file in Java.
I wanted to use ComfyJ to create a wrapper for the DLL file. ComfyJ has a wizard for this, however when I select the DLL file, it says that it cannot use this kind of DLL file.
So, next I tried to use the JNIWrapper wizard, but it requires a .h
file. The problem is, that I don't have a .h
file for this DLL.
(I have a commercial license for both these tools).
I am a bit surprised that there is no way to export or create a .h
file for any given DLL file. I searched the internet but couldn't find any tool capable of doing this. Which gives me the feeling that I'm still missing some piece of information, which is not in the DLL file, but will be necessary to create this .h
file.
Secondly, I do actually know all method names, parameters and return types. That is because the WinApiOverride tool can list them for me. I can even see which parameters are passed to the functions/methods of this DLL file, while applications are running.
Foo.dll|bool __cdecl ?FooAddBar@@YA_NHHH@Z(int,int,int)
So, I have the impression that I have all required information. Before I go in too deep ... My question: do you think it's possible that I create this .h
file manually using Notepad ?
Legal Note: this DLL file is not a pre-installed DLL. It's part of a commercial application for which I own a valid commercial license. The company says that I am free to use their DLL file for whatever I want, as long as I use it for personal use only.
EDIT:
I also tried to write the jniwrapper code myself. Result looks something like this:
Library lib = new Library("foo", Function.CDECL_CALLING_CONVENTION);
lib.load();
// works fine.
Function f = lib.getFunction("?FooAddBar@@YA_NHHH@Z");
// doesn't work.
f.invoke(null, new Int(), new Int(), new Int());
The result is a fancy exception:
Exception c0000005, at 157694FA
Access violation: attempting to read memory at address 00000078
Native function stack data: 0,0,0,0,b0dcbfe6,800fdc5,65637845,6f697470,3063206e,30303030,202c3530,31207461,39363735,a414634,65636341,76207373
Exception in thread "main" com.jniwrapper.FunctionExecutionException: c0000005
at com.jniwrapper.Function.invokeCFunc(Native Method)
at com.jniwrapper.FunctionCall.a(SourceFile:127)
at com.jniwrapper.FunctionCall.call(SourceFile:35)
at com.jniwrapper.Function.invoke(SourceFile:188)
at com.jniwrapper.Function.invoke(SourceFile:239)
That strange method name seems to be the correct form. The method name does look strange. I found out that this is because C++ "encodes" the file names inside dll files. ?FooAddBar@@YA_NHHH@Z
actually is bool FooAddBar (int, int, int)
.
Nevertheless, it looks like JNIWrapper prefers the encoded name. Because when I try with the short name "FooAddBar"
or antything else, I already have an exception because it cannot find the method. On the other hand, when I use the encoded method name (i.e. "?FooAddBar@@YA_NHHH@Z"
), it does find the method, proving that it is the correct one.
However, during the invocaton, things do go wrong. So, I'm guessing that I use the wrong parameters or something. Clearly, I'm doing something wrong, and hoping that JniWrapper wizard can create a correct wrapper which fixes this. (However, in that case, I do need a .h
file).