2014-01-06 103 views

回答

6

如果要調用一個函數命名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 
+0

感謝您的答覆我一直在尋找的第一個,但是當我打印數據的答覆是這樣的'c_long(104)'爲什麼它不打印實際價值 – ganeshredcobra

+1

@ganeshredcobra我不知道哪裏是從哪裏來的,但'c_long(104)'是一個對象,你可以將它的內容打印爲'c_int(104).value'。 – 2014-01-07 05:48:18

+1

@ ganeshredcobra:'c_int'在32位平臺或Win64上是'c_long',但是作爲一個結果得到一個ctypes數據類型是不可思議的。簡單類型的getter將Python對象作爲'restype'返回。 – eryksun