2011-01-14 21 views
2

.dllライブラリのコールバック関数をPythonでctypesに登録しようとしています。しかし、構造体/フィールドにコールバック関数が必要です。それは動作しません(エラーはありませんが、コールバック関数は何もしていません)ので、私は間違っていると思います。誰かが、私を助けてくれますか?Pythonのフィールド型のコールバック関数

うまくいけば、私が何をしようとしています何を説明するコードがあります:

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) 
関連する問題