2011-01-14 3 views
2

파이썬에서 .dll 라이브러리의 콜백 함수를 ctypes로 등록하려고합니다. 하지만 구조체/필드에 콜백 함수가 필요합니다. 그것은 작동하지 않기 때문에 (오류는 없지만 콜백 함수는 아무 것도하지 않습니다) 제가 틀렸다고 가정합니다. 누구 좀 도와 주시겠습니까?파이썬 ctypes 필드의 콜백 함수

는 코드가 잘하면 내가 뭘하려고 오전 설명이있다 : 이것은 당신이 사용하고있는 전체 코드의 경우, 사용자가 정의한,

import ctypes 

firsttype = CFUNCTYPE(c_void_p, c_int) 
secondtype = CFUNCTYPE(c_void_p, c_int) 

@firsttype 
def OnFirst(i): 
    print "OnFirst" 

@secondtype 
def OnSecond(i): 
    print "OnSecond" 

class tHandlerStructure(Structure): 
    `_fields_` = [ 
    ("firstCallback",firsttype), 
    ("secondCallback",secondtype) 
    ] 

stHandlerStructure = tHandlerStructure() 

ctypes.cdll.myDll.Initialize.argtypes = [POINTER(tHandlerStructure)] 
ctypes.cdll.myDll.Initialize.restype = c_void_p 

ctypes.cdll.myDll.Initialize(stHandleStructure) 

답변

1

당신은 tHandlerStructure를 초기화 할 수 있습니다

stHandlerStructure = tHandlerStructure(OnFirst,OnSecond) 

코드에서 다른 구문 오류가 있습니다. 코드를 잘라내어 붙여 넣는 것이 가장 좋으며 오류를 줄뿐만 아니라 추적 코드도 제공하십시오. 작동 범위 :

from ctypes import * 

firsttype = CFUNCTYPE(c_void_p, c_int) 
secondtype = CFUNCTYPE(c_void_p, c_int) 

@firsttype 
def OnFirst(i): 
    print "OnFirst" 

@secondtype 
def OnSecond(i): 
    print "OnSecond" 

class tHandlerStructure(Structure): 
    _fields_ = [ 
    ("firstCallback",firsttype), 
    ("secondCallback",secondtype) 
    ] 

stHandlerStructure = tHandlerStructure(OnFirst,OnSecond) 

cdll.myDll.Initialize.argtypes = [POINTER(tHandlerStructure)] 
cdll.myDll.Initialize.restype = c_void_p 

cdll.myDll.Initialize(stHandlerStructure) 
+0

대단히 감사합니다. 지금은 작동합니다. –

0

및 구조를 인스턴스화, 실제로 넣어 결코 당신의 콜백.

stHandlerStructure = tHandlerStructure(OnFirst, OnSecond)