我如何使用python ctypes实现此功能

extern  int __stdcall GetRate(HANDLE hDev, int* pData)

如何设置数据类型,以便我可以打印pData值

最佳答案

如果要调用名为GetRate的函数,则可以按以下方式进行操作:

from ctypes import *
from ctypes.wintypes import *

GetRate = windll.YOURLIB.GetRate
GetRate.restype = c_int
GetRate.argtypes = [HANDLE, POINTER(c_int)]

# now call GetRate as something like:
#
# hDev = ... # handle
# Data = c_int()
#
# GetRate(hDev, byref(Data)) # GetRate(hDev, &Data)
# print Data

但是,如果您尝试声明回调函数指针,则可以按以下方式进行操作(我认为您正在寻找第一个):
from ctypes import *
from ctypes.wintypes import *

def GetRate(hDev, pDate):
    # Your implementation
    return 0

# you'll need GETRATE to pass it in the argtypes to the target function
GETRATE = WINFUNCTYPE(c_int, HANDLE, POINTER(c_int))
pGetRate = GETRATE(GetRate)

# now you can pass pGetRate as a callback to another function

关于Python CTypes整数指针,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20950612/

10-13 00:47