首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用Python调用C/C++ DLL中的方法

用Python调用C/C++ DLL中的方法
EN

Stack Overflow用户
提问于 2016-08-17 13:20:57
回答 1查看 2.9K关注 0票数 0

我这里有一个关于从c/c++ dll调用函数的教程,这个例子是从官方教程编写的。

代码语言:javascript
运行
复制
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‘的所有函数签名的日志,我尝试使用函数的全名,但是它仍然没有工作。

任何其他的想法都是有福的。

EN

回答 1

Stack Overflow用户

发布于 2016-08-17 13:48:15

不幸的是,您不能轻松地以可移植的方式使用ctypes

ctypes被设计为在DLL中调用具有C兼容的数据类型的函数

由于没有用于标准二进制接口的C++,所以您应该知道生成DLL的编译器是如何生成代码的(即类布局. )。

一个更好的解决方案是创建一个新的DLL,它使用当前的DLL,并将方法包装为普通的c++函数。有关详细信息,请参阅boost.python大口

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38997898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档