Python ctypes整数指针

时间:2014-01-06 13:00:36

标签: python ctypes

如何使用python ctypes

实现此功能
extern  int __stdcall GetRate(HANDLE hDev, int* pData)

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

1 个答案:

答案 0 :(得分:8)

如果要调用名为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