r1c*_*ank 19 c++ class dllimport dllexport
我可以在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);
};
Run Code Online (Sandbox Code Playgroud)
我知道如何加载DLL并在DLL中使用该函数,但是如何在DLL中放置一个类?非常感谢你.
bcs*_*hes 25
如果使用运行时动态链接(使用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();
}
Run Code Online (Sandbox Code Playgroud)
否则,如果您在加载时链接dll,只需使用icecrime提供的信息:http://msdn.microsoft.com/en-us/library/a90k134d.aspx
Naw*_*waz 13
bcsanches建议的解决方案,
__declspec(dllexport) ISDLConsole *Create()
{
return new SDLConsole();
}
Run Code Online (Sandbox Code Playgroud)
如果你打算使用这种方法作为建议由bcsanches,然后确保你使用下面的函数来delete你的对象,
__declspec(dllexport) void Destroy(ISDLConsole *instance)
{
delete instance;
}
Run Code Online (Sandbox Code Playgroud)
始终成对定义此类函数,因为它确保您从创建它们的同一堆/内存池/等中删除对象.看到这对功能
#ifdef _EXPORTING
#define CLASS_DECLSPEC __declspec(dllexport)
#else
#define CLASS_DECLSPEC __declspec(dllimport)
#endif
class CLASS_DECLSPEC SDLConsole
{
/* ... */
};
Run Code Online (Sandbox Code Playgroud)
剩下的就是_EXPORTING在构建DLL时定义预处理器符号.