我正在Python中工作,并试图执行一个DLL函数。如果公开了函数,我可以设法使用它们的名称来执行它们:
# 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++中,我们可以这样做,例如:(如果我没有错):
FUNCPTR(MYDLL, myFuncName, DWORD __stdcall, (DWORD arg1, DWORD arg2, DWORD arg3), <ADDR>)有些人知道Python中这种字体的等价性吗?
谢谢!
发布于 2022-01-06 19:06:18
假设您以某种方式获得了该函数的地址:
// test.c
int func(int a, int b) { return a + b; }
__declspec(dllexport)
void* get_func() { return (void*)func; }然后:
# 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))输出:
5
5https://stackoverflow.com/questions/70609929
复制相似问题