如何使用DLL中的类?

发布于 2024-10-09 18:17:35 字数 593 浏览 0 评论 0原文

我可以将类放入 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中呢?非常感谢。

Can I put a class inside a DLL?
The class i wrote is this:

    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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

2024-10-16 18:17:35

如果您使用运行时动态链接(使用 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();
 }

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

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:

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

鸠书 2024-10-16 18:17:35

解决方案bcsanches,

 __declspec(dllexport) ISDLConsole *Create()
 {
    return new SDLConsole();
 }

如果您打算按照建议使用此方法< /a> by bcsanches,然后确保使用以下函数删除您的对象,

 __declspec(dllexport) void Destroy(ISDLConsole *instance)
 {
       delete instance;
 }

成对定义此类函数always,因为它确保从创建对象的同一堆/内存池等中删除对象。请参阅此配对函数

Solution suggested by bcsanches,

 __declspec(dllexport) ISDLConsole *Create()
 {
    return new SDLConsole();
 }

If you're going to use this approach as suggested by bcsanches, then make sure that you use the following function to delete your object,

 __declspec(dllexport) void Destroy(ISDLConsole *instance)
 {
       delete instance;
 }

Define such functions always in pair, as it ensures that you delete your objects from the same heap/memory-pool/etc they were created on. See this pair-functions

落花浅忆 2024-10-16 18:17:35

您可以,并且您需要的所有信息都位于此页面和< a href="http://msdn.microsoft.com/en-us/library/8fskxacy.aspx" rel="noreferrer">此页面:

#ifdef _EXPORTING
   #define CLASS_DECLSPEC __declspec(dllexport)
#else
   #define CLASS_DECLSPEC __declspec(dllimport)
#endif

class CLASS_DECLSPEC SDLConsole
{
    /* ... */
};

剩下的就是定义预处理器符号 _EXPORTING 构建 DLL 时。

You can, and all the information you need are on this page and 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.

赠佳期 2024-10-16 18:17:35

如果您想公开类中的数据,上述解决方案将无法解决。您必须在 DLL 编译中的类本身上添加一个 __declspec(dllexport) ,并在链接到 DLL 的模块中添加一个 __declspec(dllimport)

一种常见的技术是这样做(Microsoft 向导生成这样的代码):

#ifdef EXPORT_API
#define MY_API __declspec(dllexport)
#else
#define MY_API __declspec(dllimport)
#endif

class MY_API MyClass {
   ...
};

然后确保在 DLL 项目中定义了 EXPORT_API,并确保它没有在链接到 DLL 的模块中定义。

如果您从头开始在 Visual C++ 中创建一个新的 DLL 项目,并选中“导出符号”复选框,则将使用此技术生成一些示例代码。

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.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文