0

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.

ComfyJ wizard for COM

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.

JNIWrapper needs .h file

(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).

Community
  • 1
  • 1
bvdb
  • 22,839
  • 10
  • 110
  • 123
  • How do you know the DLL was created using C++? That's probably why you won't find (or very difficult to find) tools to create .h files from DLL's. – PaulMcKenzie Feb 08 '17 at 10:57
  • @PaulMcKenzie I first checked to make sure it was not a .NET dll (using ILSpy tool). Next I simply used a text editor and found the following text inside the DLL : `Copyright (c) 1992-2004 by P.J. Plauger, licensed by Dinkumware, Ltd. ALL RIGHTS RESERVED` , which refers to http://www.dinkumware.com/ , a company that trades in C and C++ libraries. – bvdb Feb 09 '17 at 20:36
  • Well, that DLL should have come with some sort of header in whatever toolkit, library, etc. that the DLL is part of. That header, unless it's a commercial product, should come free of charge for any C or C++ developer (else how would those programmers use the DLL). – PaulMcKenzie Feb 09 '17 at 20:39
  • @PaulMcKenzie , it's not really a toolkit. It's a commercial standalone program, made by a Japanese company. Under the hood are very clever calculations (inside this DLL file), which I would like to borrow for another program. So, no there's no .h file included because the initial intention of the program was not to release it like this. - You seem to incline that there's no way I can do this if there's no .h file supplied by this Japanese company, right ? - If so, why not ? – bvdb Feb 11 '17 at 14:20
  • @PaulMcKenzie just for the record, Dinkumware isn't the company that created this specific DLL files. Dinkumware on the other hand, is a company that creates C++ library files that are used by **compilers**. So, that just proves that the DLL file was compiled using a C++ compiler. – bvdb Feb 23 '17 at 14:10

0 Answers0