我这里有一个关于从c/c++ dll调用函数的教程,这个例子是从官方教程编写的。
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)
>>>
但是,当函数是外部函数时,假设我对一个对象有一个引用,并且我想用params从该对象调用一个函数,我如何做到这一点?
我确实看到了来自'dumpbin -exports‘的所有函数签名的日志,我尝试使用函数的全名,但是它仍然没有工作。
任何其他的想法都是有福的。
发布于 2016-08-17 13:48:15
不幸的是,您不能轻松地以可移植的方式使用ctypes
。
ctypes
被设计为在DLL中调用具有C兼容的数据类型的函数。
由于没有用于标准二进制接口的C++,所以您应该知道生成DLL的编译器是如何生成代码的(即类布局. )。
一个更好的解决方案是创建一个新的DLL,它使用当前的DLL,并将方法包装为普通的c++函数。有关详细信息,请参阅boost.python或大口。
https://stackoverflow.com/questions/38997898
复制相似问题