首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Python中的DLL中调用非导出函数。

在Python中的DLL中调用非导出函数。
EN

Stack Overflow用户
提问于 2022-01-06 16:00:31
回答 1查看 158关注 0票数 1

我正在Python中工作,并试图执行一个DLL函数。如果公开了函数,我可以设法使用它们的名称来执行它们:

代码语言:javascript
复制
# Load DLL into memory.

hllDll = ctypes.WinDLL ("mydll.dll")

# Set up prototype and parameters for the desired function call.

hllApiProto = ctypes.WINFUNCTYPE (
    ctypes.c_int,      # Return type.
    ctypes.c_void_p,   # Parameters 1 ...
    ctypes.c_void_p,
    ctypes.c_void_p,
    ctypes.c_void_p)   # ... thru 4.
hllApiParams = (1, "p1", 0), (1, "p2", 0), (1, "p3",0), (1, "p4",0),

# Actually map the call ("HLLAPI(...)") to a Python name.

hllApi = hllApiProto (("HLLAPI", hllDll), hllApiParams)

hllApi(p1, p2, p3, p4)

但是我想要为一个非导出函数的地址创建一个函式。

在C++中,我们可以这样做,例如:(如果我没有错):

代码语言:javascript
复制
FUNCPTR(MYDLL, myFuncName, DWORD __stdcall, (DWORD arg1, DWORD arg2, DWORD arg3), <ADDR>)

有些人知道Python中这种字体的等价性吗?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2022-01-06 19:06:18

假设您以某种方式获得了该函数的地址:

代码语言:javascript
复制
// test.c
int func(int a, int b) { return a + b; }

__declspec(dllexport)
void* get_func() { return (void*)func; }

然后:

代码语言:javascript
复制
# test.py
import ctypes as ct

dll = ct.CDLL('./test')
dll.get_func.argtypes = ()
dll.get_func.restype = ct.c_void_p

addr = dll.get_func()

ADD = ct.CFUNCTYPE(ct.c_int,ct.c_int,ct.c_int) # function prototype
add = ADD(addr)                                # function instance
print(add(2,3))

# In this case, can also just set the return type to the function prototype...
dll.get_func.restype = ADD
add = get_func()
print(add(2,3))

输出:

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

https://stackoverflow.com/questions/70609929

复制
相关文章

相似问题

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