仅使用 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库来访问导出的类。
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(或等效项)。
相关文章:
- 可视化C++:发布模式的运行时库作为'Multi-threaded Debug DLL'
- 在运行时加载C++ DLL
- C++ DLL 运行时错误"abc.dll is not a valid WIN32 application" 。请帮助解决这个问题
- 如何在运行时从 c++ 中的 DLL 文件创建对象?
- MSVCP140.dll尽管设置了 /MT 运行时库
- 在程序运行时更改 DLL
- .NET CORE 2 DLL引用带有C 代码运行时错误
- C DLL通过C#UWP应用程序中的Win运行时组件适用于ARM,但对于X86/X64不适用于
- 如何在运行时构建DLL
- 通过功能指针通过DLL进行回调功能,运行时检查失败#0错误
- 在运行时用c++创建dll
- VBA 中的 Qt5 Dll,错误运行时错误 453
- 当应用程序在不同系统上运行时,无法加载DLL异常
- 如何在运行时像可执行文件一样"启动"DLL?
- 在VS中编译程序,无需外部运行时DLL
- 在运行时加载 DLL
- 如何在运行时链接期间从我的 DLL 调用函数
- 使用 DLL 运行时,按钮上不显示位图
- 当从DLL运行时,c++ cdialgex丢失ParentWnd
- MSVCR100D.dll运行时错误