gpt4 book ai didi

Python ctypes整数指针

转载 作者:太空狗 更新时间:2023-10-30 00:50:32 26 4
gpt4 key购买 nike

我如何使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com