如何在python中加载C # / C++ dll?

How to load a C#/C++ dll in python?

本文关键字:C++ dll 加载 python      更新时间:2023-10-16

现在我只有一个用 C# 编写的 DLL 文件,C++混合(C# 代码调用里面的 C++ natvie 代码(我知道,我想用 Python(ctypes( 包装这个 DLL 文件,使用 refoctor(dnSpy( 来反转我找到名为 s_set_server_public_key 的函数的 DLL。

所以我的问题是如何使用 Python 调用这个函数? DLL附上了,你能给我一些建议吗?非常感谢!! 在此处输入图像描述

我认为这个答案对您很有用:

import ctypes
# Load DLL into memory.
hllDll = ctypes.WinDLL ("c:\PComm\ehlapi32.dll")
# Set up prototype and parameters for the desired function call.
# HLLAPI
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)
# This is how you can actually call the DLL function.
# Set up the variables and call the Python name with them.
p1 = ctypes.c_int (1)
p2 = ctypes.c_char_p (sessionVar)
p3 = ctypes.c_int (1)
p4 = ctypes.c_int (0)
hllApi (ctypes.byref (p1), p2, ctypes.byref (p3), ctypes.byref (p4))

如何使用 Python 中的 DLL 文件?