显式链接到 DLL 中的类
我有一个类当前位于 .lib 文件中:
class __declspec(dllexport) ReportData {
public:
list<FileData *> ReportFileData;
list<SupressionData *> ReportSupressionData;
static char *ClientName;
static char *DataRecieved;
std::string GenFileConfTemplate();
~ReportData()
{
ReportFileData.clear();
ReportSupressionData.clear();
}
};
我可以将此 lib 文件添加到我的项目中并创建此类的实例,没有问题。 我的问题是,我如何将其移动到 DLL 并动态加载它,并创建此类的实例。 我想要做的是将一些常见功能移至此 dll 中并在多个项目之间共享。
我希望在进行更改时能够根据需要重新编译 dll,并让项目使用必须最新的版本; 截至目前,使用 lib,我必须在重新编译 dll 后重新编译每个项目才能发生更改,并且由于该系统最初设计的方式,有 100 多个项目将使用此 lib/dll,并且我不想每次进行更改时都重新编译 100 个不同的项目。
因此,请就我应该如何去做这件事提供您的专家意见。
我将在我的项目中使用 dll,如下所示:
ReportData *rd = new ReportData();
ReportData::ClientName = "test";
rd->ReportFileData.push_back(new FileData("testing", 10, 1));
rd->ReportFileData.push_back(new FileData("testing 2", 20, 1));
std::cout << rd->GenFileConfTemplate();
delete rd;
我最终得到了这样的结果:
typedef Foo* (__stdcall *CreateFunc)();
int main()
{
HMODULE dll (LoadLibrary ("..\\LLFileConfirmDLL\\LLFileConfirmDLL.dll"));
if (!dll) {
cerr << "LoadLibrary: Failed!" << endl;
std::cin.get();
return 1;
}
CreateFunc create (reinterpret_cast<CreateFunc>(GetProcAddress (dll, "create")));
if (!create) {
cerr << "GetProcAddress: Failed!" << endl;
std::cin.get();
return 1;
}
Foo *f = create();
cerr << f->Test();
FreeLibrary (dll);
std::cin.get();
return 0;
}
struct FOOAPI Foo
{
Foo();
virtual ~Foo();
virtual int Test();
};
Foo::Foo()
{
}
Foo::~Foo()
{
}
int Foo::Test()
{
return 5;
}
extern "C" __declspec(dllexport) Foo* __stdcall create()
{
return new Foo;
}
I have a class that is currently in a .lib file:
class __declspec(dllexport) ReportData {
public:
list<FileData *> ReportFileData;
list<SupressionData *> ReportSupressionData;
static char *ClientName;
static char *DataRecieved;
std::string GenFileConfTemplate();
~ReportData()
{
ReportFileData.clear();
ReportSupressionData.clear();
}
};
I can add this lib file to my project and create instances of this class no problem.
My question is, how can i move this to a DLL and dynamically load it, and create instances of this class. What i'm wanting to do is move some common functionality into this dll and share it across multiple projects.
I want to be able to re-compile the dll if needed when changes are made and have the projects use the must recent version; as of now, using a lib, I have to recompile every project after I recompile the dll for the changes to take place, and because the way this system was originally designed, there are 100+ projects that will use this lib/dll and I don't want to recompile 100 different projects each time a change is made.
So, please give me your expert opinions on how i should go about doing this.
I'll be using the dll inside of my projects like so:
ReportData *rd = new ReportData();
ReportData::ClientName = "test";
rd->ReportFileData.push_back(new FileData("testing", 10, 1));
rd->ReportFileData.push_back(new FileData("testing 2", 20, 1));
std::cout << rd->GenFileConfTemplate();
delete rd;
I ended up with something like this:
typedef Foo* (__stdcall *CreateFunc)();
int main()
{
HMODULE dll (LoadLibrary ("..\\LLFileConfirmDLL\\LLFileConfirmDLL.dll"));
if (!dll) {
cerr << "LoadLibrary: Failed!" << endl;
std::cin.get();
return 1;
}
CreateFunc create (reinterpret_cast<CreateFunc>(GetProcAddress (dll, "create")));
if (!create) {
cerr << "GetProcAddress: Failed!" << endl;
std::cin.get();
return 1;
}
Foo *f = create();
cerr << f->Test();
FreeLibrary (dll);
std::cin.get();
return 0;
}
struct FOOAPI Foo
{
Foo();
virtual ~Foo();
virtual int Test();
};
Foo::Foo()
{
}
Foo::~Foo()
{
}
int Foo::Test()
{
return 5;
}
extern "C" __declspec(dllexport) Foo* __stdcall create()
{
return new Foo;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以按照 COM 的方式执行此操作:
编辑:
类似:
当然,现在您必须担心诸如谁将释放对象、确保 DLL 不会被卸载等问题。
另请参阅 DllGetClassObject,DllCanUnloadNow 等
You can do this the way COM does it:
Edit:
Something like:
Of course, now you have to worry about things like who will free the object, making sure the DLL doesn't get unloaded, etc.
See also DllGetClassObject, DllCanUnloadNow, etc.