Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a class in DLL?

Tags:

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.

like image 503
r1cebank Avatar asked Dec 29 '10 16:12

r1cebank


People also ask

Can DLL have classes?

If you are programming in Visual Basic or C#, you must declare DLL functions within a class or Visual Basic module. Within a class, you define a static method for each DLL function you want to call.

What is a DLL class?

The Class Library . DLL contains program code, data, and resources that can be can used by other programs and are easily implemented into other Visual Studio projects.

Can DLL have multiple classes?

Yes U can build as many classes as u want into a Single Dll. Select Visual C# or ur desired language and then in the right pane Select Class Library. Give the Class Library a name and then click on OK. Now add as many classes as u want in this project and when ur done with adding all the classes build the project.


1 Answers

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

like image 170
bcsanches Avatar answered Sep 28 '22 10:09

bcsanches