C++ 如何在 DLL 中使用类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4555961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use a class in DLL?
提问by r1cebank
Can I put a class inside a DLL? The class i wrote is this:
我可以将类放入 DLL 中吗?我写的课是这样的:
class SDLConsole
{
public:
SDLConsole();
~SDLConsole(){};
void getInfo(int,int);
void initConsole(char*, char*, SDL_Surface*, int, int, int);
void sendMsg(char*,int, SDL_Surface*);
void cls(SDL_Surface*);
private:
TTF_Font *font;
SDL_Surface *consoleImg;
int width, pos, height, line, size, ctLine;
SDL_Surface* render(char*,int);
};
I know how to load a DLL and use the function inside a DLL, but how can I put a class inside a DLL? Thank you very much.
我知道如何加载 DLL 并在 DLL 中使用该函数,但是如何将类放入 DLL 中?非常感谢。
回答by bcsanches
If you use run time dynamic linking (uses LoadLibrary to load the dll) you cannot access the class directly, you need to declare a interface for your class and create a function that returns a instance of this class, like this:
如果使用运行时动态链接(使用 LoadLibrary 加载 dll),则无法直接访问该类,则需要为您的类声明一个接口并创建一个返回该类实例的函数,如下所示:
class ISDLConsole
{
public:
virtual void getInfo(int,int) = 0;
virtual void initConsole(char*, char*, SDL_Surface*, int, int, int) = 0;
virtual void sendMsg(char*,int, SDL_Surface*) = 0;
virtual void cls(SDL_Surface*) = 0;
};
class SDLConsole: public ISDLConsole
{
//rest of the code
};
__declspec(dllexport) ISDLConsole *Create()
{
return new SDLConsole();
}
Otherwise, if you link the dll during load time, just use the information provided by icecrime: http://msdn.microsoft.com/en-us/library/a90k134d.aspx
否则,如果您在加载时链接 dll,只需使用 icecrime 提供的信息:http: //msdn.microsoft.com/en-us/library/a90k134d.aspx
回答by Nawaz
Solutionsuggested by bcsanches,
bcsanches建议的解决方案,
__declspec(dllexport) ISDLConsole *Create()
{
return new SDLConsole();
}
If you're going to use this approach as suggestedby bcsanches, then make sure that you use the following function to delete
your object,
如果你打算为使用这种方法,建议由bcsanches,然后确保你使用下面的函数来delete
你的对象,
__declspec(dllexport) void Destroy(ISDLConsole *instance)
{
delete instance;
}
Define such functions alwaysin pair, as it ensuresthat you delete your objects from the same heap/memory-pool/etc they were created on. See this pair-functions
始终成对定义此类函数,因为它确保您从创建它们的同一个堆/内存池/等中删除您的对象。看到这个对函数
回答by icecrime
You can, and all the information you need are on this pageand this page:
#ifdef _EXPORTING
#define CLASS_DECLSPEC __declspec(dllexport)
#else
#define CLASS_DECLSPEC __declspec(dllimport)
#endif
class CLASS_DECLSPEC SDLConsole
{
/* ... */
};
All there is left is to define the preprocessor symbol _EXPORTING
when building the DLL.
剩下的就是_EXPORTING
在构建 DLL 时定义预处理器符号。
回答by J?rgen Sigvardsson
If you want to expose the data in a class, the above solutions won't cut it. You have to slap a __declspec(dllexport)
on the class itself in the DLL compilation, and a __declspec(dllimport)
in the module that links to the DLL.
如果你想在一个类中公开数据,上面的解决方案不会削减它。您必须__declspec(dllexport)
在 DLL 编译中在类本身上打一个a __declspec(dllimport)
,在链接到 DLL 的模块中打一个。
A common technique is to do this (Microsoft wizards produce code like this):
一种常见的技术是这样做(微软向导生成这样的代码):
#ifdef EXPORT_API
#define MY_API __declspec(dllexport)
#else
#define MY_API __declspec(dllimport)
#endif
class MY_API MyClass {
...
};
Then make sure EXPORT_API
is defined in the DLL project, and make sure it isn't defined in the module that links to the DLL.
然后确保EXPORT_API
在 DLL 项目中定义了它,并确保它没有在链接到 DLL 的模块中定义。
If you create a new DLL project in Visual C++ from scratch, and check the check box "Export symbols", some sample code will be generated using this technique.
如果您从头开始在 Visual C++ 中创建一个新的 DLL 项目,并选中“导出符号”复选框,则将使用此技术生成一些示例代码。