2014-01-23 31 views
0

我有一個DLL的一些功能。 頭爲例:在DLL中的功能

__declspec(dllexport) bool Test() 

; ,我有另外一個簡單的應用程序來使用這個函數:

typedef bool(CALLBACK* LPFNDLLFUNC1)(); 

HINSTANCE hDLL;    // Handle to DLL 
LPFNDLLFUNC1 lpfnDllFunc1; // Function pointer 
bool uReturnVal; 

hDLL = LoadLibrary(L"NAME.dll"); 
if (hDLL != NULL) 
{ 
    lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,"Test"); 
    if (!lpfnDllFunc1) 
    { 
     // handle the error 
     FreeLibrary(hDLL); 
     cout << "error"; 
    } 
    else 
    { 
     // call the function 
     uReturnVal = lpfnDllFunc1(); 
    } 
} 

但不起作用。該功能未找到。

+1

這是什麼意思?是DLL加載和功能找到?是lpfnDllFunc1不是null? – 4pie0

+0

發佈你的一些輸出會有幫助...我們不知道你的DLL是否被發現,或者如果嘗試加載它,還有其他問題。 –

+0

該DLL已加載,但未找到該功能。 – user2295277

回答

0

我的心靈感官告訴我,該功能沒有找到,因爲你忘記聲明它爲extern "C"

因爲在C++中,被放入DLL的導出表的實際函數名name mangling的不僅僅是Test如果函數有C++聯動更長,更胡言亂語-Y字符串。如果用C鏈接聲明它,那麼它將以預期的名稱導出,因此可以更容易地導入。

例如:

// Header file 
#ifdef __cplusplus 
// Declare all following functions with C linkage 
extern "C" 
{ 
#endif 

__declspec(dllexport) bool Test(); 

#ifdef __cplusplus 
} 
// End C linkage 
#endif 

// DLL source file 
extern "C" 
{ 

__declspec(dllexport) bool Test() 
{ 
    // Function body 
    ... 
} 

} // End C linkage 
+0

謝謝你解決我的問題。 – user2295277