本文介绍了显式链接到DLL中的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个当前在.lib文件中的类:

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();
        }

};

我可以将此lib文件添加到我的项目中,并且可以毫无问题地创建此类的实例。
我的问题是,如何将其移动到DLL并动态加载它,并创建此类的实例。我想要做的就是将一些常用功能移入该dll,并在多个项目中共享它。

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.

我希望能够在进行更改并需要项目使用最新版本时重新编译dll;到目前为止,使用lib,我必须重新编译dll后才能重新编译每个项目,以进行更改,并且由于该系统最初的设计方式,有100多个项目将使用此lib / dll,而我不想每次进行更改都重新编译100个不同的项目。

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.

我将在项目中使用dll,如下所示:

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;
}


推荐答案

您可以这样做COM做到了:

You can do this the way COM does it:


  1. 导出CreateInstance()函数。

  2. 传递一个void **并

  3. 您的消耗DLL调用库DLL上的LoadLibrary()并调用CreateInstance()。

  4. CreateInstance()可以新的ReportData并以void **参数将其返回。

  1. Export a CreateInstance() function.
  2. Pass a void** and unique identifier to CreateInstance().
  3. Your consuming DLL calls LoadLibrary() on the library DLL and calls CreateInstance().
  4. CreateInstance() does the new ReportData and returns it in the void** out param.

编辑:

类似的东西:

extern "C"
BOOL CreateObject(REFCLSID rclsid, void** ppv) {
    BOOL success = false;
    *ppv = NULL;
    if (rclsid == CLSID_ReportData) {
        ReportData* report_data = new ReportData();
        if (report_data) {
            *ppv = report_data;
            success = true;
        }
    } else if (...) {
        ... other objects ...
    }
    return success;
}

当然,现在您必须担心谁会释放对象,确保不会卸载DLL等。

Of course, now you have to worry about things like who will free the object, making sure the DLL doesn't get unloaded, etc.

另请参见,,等等。

这篇关于显式链接到DLL中的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 13:12