功能在DLL中

Functions in a DLL

本文关键字:DLL 功能      更新时间:2023-10-16

我具有一些功能的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();
    }
}

但不工作。找不到该功能。

我的心理感官告诉我,没有找到该功能,因为您忘了将其声明为extern "C"

由于c 中的名称杂交,如果函数具有C 链接,则将被放入DLL导出表中的实际函数名称比Test更长,更Gibberish-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