仅使用 DLL *.h 头文件构建(compile.link)应用程序代码,并在运行时加载 DLL 实现(显式链接)

build(compile.link) application code with only DLL *.h header file and load DLL implementation in run-time (explicit linking)

本文关键字:DLL 运行时 加载 链接 实现 代码 link 文件 compile 构建 应用程序      更新时间:2023-10-16

我有一个应用程序代码,它调用具有显式链接(或运行时链接)的DLL库来访问导出的类。

DLL.h

#ifdef DLL_EXPORT
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif 

FooDLL.h

#include "DLL.h"
class DLL_API Foo
{
public:
    void doSomeThing();     
};
extern "C" DLL_API Foo* _getInstance() {
   return new Foo();
}
typedef Foo* (*getInstanceFactory)();
Foo* getInstance() {
    HINSTANCE dllHandle = LoadLibraryA("Foo.dll");
    getInstanceFactory factory_func = (getInstanceFactory)GetProcAddress(dllHandle, "_getInstance");
    return factory_func();
}

FooDLL

.cpp
#include "FooDLL.h"
Foo::doSomething() {
 // .......
}

应用程序.cpp(调用 DLL)

#include "FooDLL.h"
Foo* obj = getInstance();
obj->doSomething(); // XXX this line can be compiled and linked only when DLL is already in path

只有当 DLL 文件包含在 lib 路径中时,才能构建(例如编译和链接)上述代码。否则我得到未解决的外部符号错误

error LNK2001: unresolved external symbol "__declspec(dllimport) public: void __thiscall Foo::doSomething()" .....

构建过程中,是否可以仅使用 DLL 头文件(即 FooDLL.h)而不使用 DLL/LIB 文件构建应用程序代码?(附言类实现必须位于 cpp 文件中。

谢谢!

带有虚函数。

class Foo
{
public:
    void virtual doSomeThing();     
};

是的,这是可能的。如果您没有导出类,则根本不需要头文件。我不确定为什么您在头文件中调用 LoadLibrary。由于您要导出类,因此必须让编译器知道类型。此外,您不必导出整个类,您可以仅导出要公开的类的特定成员函数要在 dll 和 exe 项目中使用的 dll 标头应包括以下内容(我使用了自己的名字):

#ifdef WIN32DLL_EXPORTS
#define WIN32DLL_API __declspec(dllexport)
#else
#define WIN32DLL_API __declspec(dllimport)
#endif
class CWin32DLL 
{
public:
    CWin32DLL();
    int WIN32DLL_API GetInt();
};

实现:

#include "stdafx.h"
#include "Win32DLL.h"
extern "C" WIN32DLL_API CWin32DLL* _getInstance() 
{ 
    return new CWin32DLL(); 
} 
// This is the constructor of a class that has been exported.
// see Win32DLL.h for the class definition
CWin32DLL::CWin32DLL()
{
}
int CWin32DLL::GetInt()
{
    return 42;
}

您的 DLL 使用者:

#include "Win32DLL.h"
#include "SomeOther.h"
typedef CWin32DLL* (*getInstanceFactory)();
HINSTANCE dllHandle = LoadLibrary(_T("Win32DLL.dll")); 
getInstanceFactory factory_func = (getInstanceFactory)GetProcAddress(dllHandle, "_getInstance"); 
CWin32DLL* pWin32 = factory_func();
int iRet = pWin32->GetInt();

不要忘记在项目属性、C++、预处理器、dll 的预处理器定义中定义WIN32DLL_EXPORTS(或等效项)。