【发布时间】:2016-08-17 13:20:57
【问题描述】:
我这里有一个关于从 c/c++ dll 调用函数的教程,示例是从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)
>>>
这个例子在函数是外部的时候有效,但是,假设我有一个对象的引用,我想用参数从那个对象调用一个函数,我该怎么做?
我确实从 'dumpbin -exports' 中看到了所有函数签名的日志,我尝试使用函数的全名,但它仍然不起作用。
任何其他想法都会受到祝福。
【问题讨论】:
-
你怎么知道它不起作用?
-
我尝试以与教程相同的方式激活它,只是将名称替换为导出中的签名,但我仍然收到来自 python 日志的错误,说它无法调用该函数, 'tuple' 的东西...我不在同一台计算机上工作,我正在输入它,所以这一切都来自记忆,如果您需要任何特定数据,请告诉我
-
我认为我们需要详细信息;我们正在这里进行故障排除。
-
我看到了一个叫 SWIG 的工具,它可以做我需要的吗?从对象引用中调用方法