2015-11-15 48 views
1

這裏是一個例子再現返回指針MSG與導出的成員函數 - DLL

// in a header from a dll 

class Window{ 
public: 
MSG _declspec(dllexport) *getMessage(); //compiles 
MSG* _declspec(dllexport) getMessage(); //fails 
} 

定義在一個cpp文件設定。

在應用程序中,我無法訪問成員函數,爲什麼?我從一個實例訪問過程中的功能窗口,如:

Window App; 
func_with_parameters(param,App.getMessage(),0,0); // not found ! 
+1

對於後者的問題,一個'class'塊的默認的可見性是'private'。 – andlabs

+0

Sry,忘記在這個問題中補充說。在代碼中,它是公開的。 –

回答

1

好了,第一關。

__declspec(dllexport) MSG* GetMessage(); 

函數定義應該是正確的方法。

其次,__declspec(dllexport)將需要__declspec(dllimport)在項目的頭文件消耗dll或它不會導入。這通常是通過像這樣的宏來處理的。

#ifdef _WINDLL // Defined by Visual Studio when building a Dll 
    #define DLL_API __declspec(dllexport) 
    #else 
    #define DLL_API __declspec(dllimport) 
    #endif 

第三,在類級而不是函數級導出可能更好,例如,

class __declspec(dllexport) Window 

或與宏

class DLL_API Window