c++ - 如何在 DLL 中使用类?

标签 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);

};

我知道如何加载 DLL 并在 DLL 中使用函数,但是如何将类放入 DLL 中?非常感谢。

最佳答案

如果您使用运行时动态链接(使用 LoadLibrary 加载 dll)您无法直接访问该类,您需要为您的类声明一个接口(interface)并创建一个返回此类实例的函数,如下所示:

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

否则,如果您在加载时链接dll,只需使用icecrime提供的信息:http://msdn.microsoft.com/en-us/library/a90k134d.aspx

关于c++ - 如何在 DLL 中使用类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4555961/

相关文章:

r - 在R中创建包时链接多个文件

c++ - 用dll库编译程序

C# - 可以使用 IOCTL

java - java调用dll文件的方法

c++ - 如何重载operator new来实现类的快速分配器?

c++ - SFML 参照窗口绘制绝对位置

c++ - '(' 标记之前的预期构造函数、析构函数或类型转换

c++ - boost::local_time 未读取正确的 iso_extended_format

ios - 如何在 Swift 2 中将结构转换为 Anyobject?

php - 访问子类中的私有(private)变量