0

I have a C++ DLL that is distributed as part of a free utility. While I have found the source code, it is not distributed publicly, and I have not tried building it (not familiar with C++, code is legacy and sits in an old VCS, etc.). Ideally, I would like to use this DLL from Python without relying on header or source files.

Is this possible using Boost.Python or SWIG (or something else)? If so, can you point me to an example or resources that shows how? I initially tried using ctypes, but I learned that it cannot bind C++ classes. Some Googling suggested these alternatives, but I have yet to see how to do it without having to compile the C++ source from their tutorials.

Thank you!

deepyaman
  • 538
  • 5
  • 16
  • 1
    `ctypes` _can_ wrap C++ classes, it's just very ugly. You have to mangle the names manually (or using the appropriate platform API), call constructors and destructors and operators manually, etc. – abarnert May 30 '18 at 23:23
  • Anyway: hopefully the DLL comes with a header file? If so: With a bit of work, you can use cffi to parse the headers and access the DLL directly, but it's probably not worth trying that, especially if you don't know C++ and don't know cffi. SWIG may or may not work. If you already know SWIG, just try it. But if it fails, I wouldn't try too hard to debug it; I'd fall back to a different solution. – abarnert May 30 '18 at 23:27
  • 2
    A lot simpler, you can just write your own trivial wrapper around the DLL in C++, and then use Boost.Python or PyCXX or whatever you like best to export a Python module that binds that wrapper, instead of one that binds the original library directly. Or, maybe even simpler, write the wrapper in Cython, which feels a lot more like Python, which you already know. – abarnert May 30 '18 at 23:28
  • With SWIG my answer to: https://stackoverflow.com/a/10531844/168175 pretty much applies here, even though you're talking Python not Java, because the tricky bit is working out what to write in the .i file. – Flexo Jun 01 '18 at 21:39
  • @Flexo I haven't looked at SWIG in depth yet, but I don't see reconstructing the headers as being a challenge. At least for my DLL, Dependency Walker looks like it does a great job of unmangling function signatures. – deepyaman Jun 01 '18 at 21:59

0 Answers0