2

I have a compiled DLL (C/C++) and I have to use it in Java. So I want to use SWIG to create a wrapper . I followed all the tutorials like:

But in all the examples no precompiled DLL and/or Java and/or Visual Studio (2015) were used. When using an own cpp file for the implementation (and don't use an external DLL, implementing the header) everything works fine (compile to a single DLL and calling from Java).

So what do I want: Having a precompiled DLL (example.dll), the header for that (example.h) and the SWIG header file (example.i). Compile everything to a "wrapper DLL", load this one in Java and accessing the precompiled DLL (example.dll).

My steps in detail:

  1. New VS class library project
  2. Add the h and i file
  3. Add the custom build step for the i file (like in the example)
  4. Java files and the example_wrap.cxx file is generated
  5. Add the example_wrap.cxx, add Java includes and don't use precompiled headers
  6. Compile example_wrap.cxx to example.lib (Is that right?)
  7. Setting project settings like in the tutorials (no clr, add java-includes, no precompiled headers)
  8. Now creating the whole project fails with linking error LNK2019 in example_wrap.obj.

Can someone help me?

What I also don't get: The result will be "example.dll". I have to use this DLL in Java (with System.load/loadLibrary). How can this SWIG DLL access the "original" example.dll with all the logic inside?

Using Python instead of Java results in the same failure (LNK2019 - unresolved external symbol).

Community
  • 1
  • 1
Thaturne
  • 21
  • 5

2 Answers2

0

Basically you need to "forward" the calls to the external library. I.e. import symbols from there into the wrapper library and link with the external DLL lib file.

If you do not have such a lib file for the DLL, you need to either create it (e.g. using some tool the create lib from the DLL), or load the external library/import symbols at runtime (LoadLibrary/GetProcAddress).

Community
  • 1
  • 1
EmDroid
  • 5,918
  • 18
  • 18
0

The answer from axalis was absolutly right. Here some details:

  1. Add example.cpp
  2. Load the precompiled DLL (example.dll) with LoadLibrary and save a reference
  3. Load methods from there with GetProcAddress and save a reference for each method
  4. Write an "implemantation" for each method where you just call the method loaded before
  5. Compile and link everything to exampleWrap.dll and load this dll in the Java project
Thaturne
  • 21
  • 5