How to use a class in DLL?

前端 未结 4 1454
抹茶落季
抹茶落季 2020-11-30 03:25

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

    class SDLConsole
{
      public:
             SDLConsole();
             ~SDLConsole(){};
             


        
相关标签:
4条回答
  • 2020-11-30 03:53

    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

    0 讨论(0)
  • 2020-11-30 03:56

    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

    0 讨论(0)
  • 2020-11-30 04:00

    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.

    0 讨论(0)
  • 2020-11-30 04:04

    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.

    0 讨论(0)
提交回复
热议问题