0

I do have a tutorial here about calling functions from a c/c++ dll, the example is written here from the official tutorial.

WINUSERAPI int WINAPI
MessageBoxA(
    HWND hWnd,
    LPCSTR lpText,
    LPCSTR lpCaption,
    UINT uType);

Here is the wrapping with ctypes:

>>>
>>> from ctypes import c_int, WINFUNCTYPE, windll
>>> from ctypes.wintypes import HWND, LPCSTR, UINT
>>> prototype = WINFUNCTYPE(c_int, HWND, LPCSTR, LPCSTR, UINT)
>>> paramflags = (1, "hwnd", 0), (1, "text", "Hi"), (1, "caption", None), (1, "flags", 0)
>>> MessageBox = prototype(("MessageBoxA", windll.user32), paramflags)
>>>
The MessageBox foreign function can now be called in these ways:

>>>
>>> MessageBox()
>>> MessageBox(text="Spam, spam, spam")
>>> MessageBox(flags=2, text="foo bar")
>>>
A second example demonstrates output parameters. The win32 GetWindowRect function retrieves the dimensions of a specified window by copying them into RECT structure that the caller has to supply. Here is the C declaration:

WINUSERAPI BOOL WINAPI
GetWindowRect(
     HWND hWnd,
     LPRECT lpRect);
Here is the wrapping with ctypes:

>>>
>>> from ctypes import POINTER, WINFUNCTYPE, windll, WinError
>>> from ctypes.wintypes import BOOL, HWND, RECT
>>> prototype = WINFUNCTYPE(BOOL, HWND, POINTER(RECT))
>>> paramflags = (1, "hwnd"), (2, "lprect")
>>> GetWindowRect = prototype(("GetWindowRect", windll.user32), paramflags)
>>>

This example works when the function is external, however, let's assume I have a reference to an object, and I want to call a function from that object with params, how do I do that?

I did saw the log of all function signatures from 'dumpbin -exports', and I tried using the full name of the function, and still it didn't work.

Any other ideas would be blessed.

Ori Frish
  • 409
  • 7
  • 21
  • How do you know it didn't work? – Peter Wood Aug 17 '16 at 13:57
  • I tried activating it the same way above as the tutorial, just replacing the name with a signature from the exports, and I still received an error from python log, says it can't call the function, 'tuple' something... I'm not working on the same computer I'm typing it, so it's all from memory, if you need any specific data, tell me – Ori Frish Aug 17 '16 at 14:00
  • I think we need details; we're troubleshooting here. – Peter Wood Aug 17 '16 at 14:03
  • Possible duplicate of [Extending Python with C/C++](http://stackoverflow.com/questions/1076300/extending-python-with-c-c) – Peter Wood Aug 17 '16 at 14:06
  • I saw a tool called SWIG, can it do what I need? to call methods from an object reference – Ori Frish Aug 17 '16 at 14:35

1 Answers1

0

Unfortunately, you can not do that easily and in portable way with ctypes.

ctypes is designed to call functions in DLLs with C compatible data types.

Since there is no standard binary interface for C++, you should know how the compiler which generates the DLL, generates the code (i.e class layout ... ).

A better solution is to create a new DLL which uses the current DLL and wraps the methods as plain c functions. See boost.python or SWIG for more details.

napuzba
  • 6,033
  • 3
  • 21
  • 32
  • Is there any solution for calling methods in DLLs? – Ori Frish Aug 17 '16 at 14:10
  • You can wrap the methods calls with plain c functions calls or use the tools in the answer. – napuzba Aug 17 '16 at 14:21
  • thought about that one.. I also saw a tool called SWIG, can it do what I need? to call methods from an object reference – Ori Frish Aug 17 '16 at 14:25
  • Unlike `ctypes` which loads the DLL and call its functions in runtime - with those tools you create a new DLL which wraps the methods. you will be able to use the new DLL from python. – napuzba Aug 17 '16 at 15:14