2017-04-24 57 views
0

我正在使用由製造商提供的dll在python中使用硬件組件的包裝器。該DLL帶有頭文件和lib文件,因此很容易包含dll。 據我瞭解,compnent通過調用open函數來使用,給一些初始參數一個回調函數和一些額外的用戶數據,然後調用start方法。在下面,組件將通過調用回調函數來傳遞數據。C++:創建正確類型的回調函數

DLL的頭看起來是這樣的:

#ifndef COMPONENT_DLL_INCLUDED 
#define COMPONENT_DLL_INCLUDED 

#pragma once 

#ifndef DYNAMIC_COMPONENT_DLL_LINKAGE 

    // to allow include in C- and C++-code 
    #ifndef DLL_DECLSPEC 
    #ifdef __cplusplus 
    #define DLL_DECLSPEC extern "C" __declspec(dllimport) 
    #else 
    #define DLL_DECLSPEC __declspec(dllimport) 
    #endif 
    #endif 

typedef struct{ 
    eInformationType type; 
    eResultType  error; 
    ComponentInfo  info; 
}AsyncInfo; 

typedef struct{      
    BOOL   someParameter; 
    BOOL   someParameter2; 
} ParamSet1; 

typedef enum eType { 
    UndefinedType = 0x0,  
    Type1   = 0x1,   
    Type2   = 0x2   
} Param2; 


// exported type SendAsynInformation 
typedef void (CALLBACK *SendAsyncInformation)(const AsyncInfo&, void *userInfo); 

// exported functions 
    DLL_DECLSPEC eResultType COMPONENT_Open(const ParamSet1 Set1, const Param2 P2, SendAsyncInformation SendAsyncInfo, void *userInfo); 
    DLL_DECLSPEC eResultType COMPONENT_Start(void); 

我的問題是,如何我一定要回調函數是什麼樣子?我試過的東西像

void myCallback(AsyncInfo *Info, myGlobals *g) 
{ 
    ...something... 
} 

,然後通過這個回調open函數:

COMPONENT_Open(mySet1, myP2, myCallback, myVoidPtr); 

但我總是得到這樣的錯誤:

...cannot convert argument 3 from 'void (__cdecl *)(AsyncInfo *,myGlobals *)' to 'SendAsyncInformation' 

我是相當新的C++這樣最這可能是一個微不足道的問題。我嘗試了很多東西,但我不知道如何去做。那麼,我的錯誤是什麼?

+0

回答你的問題的重要信息不在這裏 - 應該爲其中一個頭文件中的COMPONENT_Open函數定義 - 回調函數的簽名(返回類型和參數類型)必須與庫相同正在期待。錯誤消息告訴你類型不匹配 - 它是你收到的確切錯誤信息,然後我猜測回調函數可能是'void SendAsynchInfo(void);. –

+0

使用std :: function mutex36

回答

1

您需要定義myCallback

void CALLBACK myCallback(const AsyncInfo&, void *userInfo) 
{ ... } 

並調用該函數原型COMPONENT_Open作爲

COMPONENT_Open(mySet1, myP2, (SendAsyncInformation)&myCallback, myVoidPtr); 

CALLBACK關鍵字(或宏實際上)決定什麼調用約定它假設編譯器使用,如果不匹配,可能會在堆棧幀清理時間期間發生異常。

由於COMPONENT_Open函數接受回調的類型SendAsyncInformation這是一個typedef,因此您需要在myCallback的地址強制轉換爲SendAsyncInformation

+0

就是這樣,謝謝! –

+0

'myCallback'與'SendAsyncInformation'具有相同的原型,那麼爲什麼你需要演員? – ssbssa