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.

5
  • How do you know it didn't work? Commented Aug 17, 2016 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 Commented Aug 17, 2016 at 14:00
  • I think we need details; we're troubleshooting here. Commented Aug 17, 2016 at 14:03
  • Possible duplicate of Extending Python with C/C++ Commented Aug 17, 2016 at 14:06
  • I saw a tool called SWIG, can it do what I need? to call methods from an object reference Commented Aug 17, 2016 at 14:35

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Is there any solution for calling methods in DLLs?
You can wrap the methods calls with plain c functions calls or use the tools in the answer.
thought about that one.. I also saw a tool called SWIG, can it do what I need? to call methods from an object reference
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.