//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)>
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