C++ 如何使用从DLL导出的类

C++ 如何使用从DLL导出的类,c++,windows,dll,dllimport,dllexport,C++,Windows,Dll,Dllimport,Dllexport,嘿,我正试图编写一个游戏引擎,我正试图在Dll中导出一个类,并试图在我的主代码中使用它。类似于使用loadlibrary()函数。我知道如何在Dll中导出和使用函数。但我想导出类,然后像使用函数一样使用它们。我不想为那个类包含然后使用它。我希望它是运行时的。我有一个非常简单的类的以下代码,我只是用它来做实验 #ifndef __DLL_EXP_ #define __DLL_EXP_ #include <iostream> #define DLL_EXPORT __declspec

嘿,我正试图编写一个游戏引擎,我正试图在Dll中导出一个类,并试图在我的主代码中使用它。类似于使用
loadlibrary()
函数。我知道如何在Dll中导出和使用函数。但我想导出类,然后像使用函数一样使用它们。我不想为那个类包含
然后使用它。我希望它是运行时的。我有一个非常简单的类的以下代码,我只是用它来做实验

#ifndef __DLL_EXP_
#define __DLL_EXP_

#include <iostream>

#define DLL_EXPORT __declspec(dllexport)

class ISid
{
public:
virtual void msg() = 0;
};

class Sid : public ISid
{
void msg()
{
    std::cout << "hkjghjgulhul..." << std::endl;
}
};

ISid DLL_EXPORT *Create()
{
return new Sid();
}

void DLL_EXPORT Destroy(ISid *instance)
{
   delete instance;
}

#endif
\ifndef\uuudll\uexp_
#定义DLL EXP_
#包括
#定义DLL\u导出\u declspec(dllexport)
ISid类
{
公众:
虚空msg()=0;
};
类别Sid:公共ISid
{
void msg()
{

std::cout如果我理解问题不是您不知道如何加载类,而是无法想象以后如何使用它?我无法帮助您理解语法,因为我习惯于共享对象动态加载,而不是dll,但用例如下:

// isid.h that gets included everywhere you want to use Sid instance
class ISid
{
public:
    virtual void msg() = 0;
};
如果你想使用动态加载的代码,你仍然需要知道它的接口。这就是为什么我建议你把接口移到一个普通的而不是dll头中

// sid.h
#ifndef __DLL_EXP_
#define __DLL_EXP_

#include <iostream>
#include "isid.h" // thus you do not know what kind of dll you are loading, but you are well aware of the interface

#define DLL_EXPORT __declspec(dllexport)
class Sid : public ISid
{
void msg()
{
    std::cout << "hkjghjgulhul..." << std::endl;
}
};

ISid DLL_EXPORT *Create()
{
    return new Sid();
}

void DLL_EXPORT Destroy(ISid *instance)
{
    delete instance;
}

#endif
//sid.h
#ifndef\uuudll\uexp_
#定义DLL EXP_
#包括
#包括“isid.h”//因此您不知道正在加载哪种类型的dll,但您很清楚该接口
#定义DLL\u导出\u declspec(dllexport)
类别Sid:公共ISid
{
void msg()
{

虽然上面的线程很好,但简而言之,您可以将接口类从dll中取出,放入单独的头中,然后通过分配给Create ed实例的基指针调用方法。其中的解决方案显示了如何从dll加载函数,我已经知道了。我无法理解如何加载类。我不确定如何加载加载Create函数,因为在为其创建typdef时,我需要返回类型,但我不想包含标题。@很抱歉,我没有真正理解您想要说的内容。您能详细说明一下吗?很抱歉,我对这一切都很陌生。在Windows中,您可以使用
LoadLibrary
动态加载库并获取广告通过
GetProcAddress
导出函数的外观。非常感谢这一点。我只是想找到一种方法,不必包含header
isid.h
。没有它,Guess无法完成。但无论如何,感谢它提供了帮助。
// main.cpp
#include <sid.h>
int main()
{
 // windows loading magic then something like where you load sid.dll
.....
typedef ISid* (*FactoryPtr)();
FactoryPtr maker = (FactoryPtr) dlsym(symHanlde, "Create");
ISid* instance = (*maker)();
instance->msg();
...
}