如何在Python中将指针发送到C++ DLL后打印地址内容?
//这个dll里的函数就是在把一个值写入那个地址:
BOOL test_sizeKey(unsigned short *sizeKey)
{
BOOL rc = TRUE;
*sizeKey = 150;
return rc;
}
我用来加载这个dll的python程序如下:
import ctypes
my_dll = ctypes.WinDLL("C:/CFiles/test_dll/SimpleArg/x64/Debug/SimpleArg.dll")
USHORT = ctypes.c_ushort
func6 = my_dll.test_sizeKey
sizeKey = USHORT()
sizeKey = ctypes.byref(sizeKey)
func6.argtypes = [
ctypes.POINTER(USHORT) # sizeKey (pointer to USHORT)
]
func6.restype = ctypes.c_bool
success6 = func6(
sizeKey
)
print(sizeKey)
当我打印最后一个变量时,得到的输出是:
<cparam 'P' (0x0000020403CF8498)>
1 个回答
0
import ctypes as ct
import ctypes.wintypes as w # for BOOL and USHORT
# WinDLL is for __stdcall calling convention.
# Won't matter on 64-bit as __stdcall and __cdecl are the same,
# but matters on 32-bit OSes. Use the correct one for portability.
dll = ct.CDLL("C:/CFiles/test_dll/SimpleArg/x64/Debug/SimpleArg.dll")
test_sizeKey = dll.test_sizeKey
test_sizeKey.argtypes = ct.POINTER(ct.c_ushort),
test_sizeKey.restype = w.BOOL # BOOL is typically "typedef int BOOL;". c_bool is byte-sized.
sizeKey = ct.c_ushort() # instance of C unsigned short
result = test_sizeKey(ct.byref(sizeKey)) # pass by reference
print(sizeKey.value) # read the value
当然可以!请把你想要翻译的内容发给我,我会帮你用简单易懂的语言解释清楚。