Thread: dll : design issue!

  1. #1
    Registered User dug's Avatar
    Join Date
    Jun 2003
    Posts
    66

    dll : design issue!

    hi there,

    i'm not that experienced with dll's... so please bear with me on this one... basically i have a design question for you...

    the scenario is this: i have a library that i want to distribute to someone as a dll, so they can use part of the functionality of the library... but only a very small part of it... so what i planned to do is have a wrapper around one class in the library that would allow limited access to a single class in the library...

    this is a basic example of how i thought i could do it:

    Code:
    /////////////////////////////////////////////////////////
    //the original library class
    
    class SpecialImage
    {
        public:
    
            SpecialImage();
            ~SpecialImage();
    
            int openSpecialImage(char* filename);
            int getSpecialImageColour(int x, int y);
    
        private:
    
            Image image;
            int special_number;
    };
    
    /////////////////////////////////////////////////////////
    
    /////////////////////////////////////////////////////////
    //the exported dll 'wrapper' class
    
    #include "SpecialImage.h"
    
    class class __declspec(dllexport) ExportedSpecialImage
    {
        public: 
            
            ExportedSpecialImage();
            ~ExportedSpecialImage();
    
            int openExportedSpecialImage(char* filename);
            int getExportedSpecialImageColour(int x, int y);
    
        private:
            
            SpecialImage* special_image;
    };
    
    ///and in the ExportedSpecialImage.cpp
    ExportedSpecialImage::ExportedSpecialImage()
    {
        special_image = NULL;
    }
    ExportedSpecialImage::~ExportedSpecialImage()
    {
        if(special_image!=NULL)delete special_image;
    }
    
    int ExportedSpecialImage::openExportedSpecialImage(char* filename)
    {
        specialImage->openSpecialImage(filename);
    }
    
    int ExportedSpecialImage::getExportedSpecialImageColour(int x, int y)
    {
        return special_image->getSpecialImageColour(x, y);
    }
    /////////////////////////////////////////////////////////

    the problem is that in my ExportedSpecialImage.h file, i need to import the 'SpecialImage.h" and i also have a member variable of type 'SpecialImage'... which would mean i could not just send the .dll and 'ExportedSpecialImage.h'.... i would also have to send 'SpecialImage.h'.... and whatever header files it depends on.... wouldn't I?? so basically i want to limit the exposure of 'SimpleImage' somehow....

    so that's my dilemma... how to do this 'wrapper' class... without exposing anymore of the library than necessary...? does that make any sense?

    it may be that i am just being stupid... but i can't figure out how to do this one.... any help/thoughts greatly appreciated!! thanks.
    Last edited by dug; 02-16-2005 at 11:25 AM.
    "take the long road.... and walk it."

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> i would also have to send 'SpecialImage.h'.... and whatever header files it depends on.... wouldn't I??
    Not necessarily. If you have a forward declaration of SpecialImage, then you can declare pointers to SpecialImage without the compiler needing to know the details of SpecialImage......as long as you don't "use" any of those pointers within the header (that clients of your DLL receive). This is often referred to as the Pimpl Idiom.
    Code:
    class SpecialImage; // forward decl.
    
    class __declspec(dllexport) ExportedSpecialImage
    {
        public: 
            
            ExportedSpecialImage();
            ~ExportedSpecialImage();
    
            int openExportedSpecialImage(char* filename);
            int getExportedSpecialImageColour(int x, int y);
    
        private:
            
            SpecialImage* special_image;
    };
    gg

  3. #3
    Registered User dug's Avatar
    Join Date
    Jun 2003
    Posts
    66
    oh yeah... forgot i could do that! that is a nice clean way to do it...

    what i did [in case anyone is interested] is use 'void*' as below:


    Code:
    /////////////////////////////////////////////////////////
    //the exported dll 'wrapper' class
    
    class class __declspec(dllexport) ExportedSpecialImage
    {
        public: 
            
            ExportedSpecialImage();
            ~ExportedSpecialImage();
    
            int openExportedSpecialImage(char* filename);
            int getExportedSpecialImageColour(int x, int y);
    
        private:
            
            void* special_image;
    };
    
    ///and in the ExportedSpecialImage.cpp
    ExportedSpecialImage::ExportedSpecialImage()
    {
        special_image = NULL;
    }
    ExportedSpecialImage::~ExportedSpecialImage()
    {
        if(special_image!=NULL)delete special_image;
    }
    
    int ExportedSpecialImage::openExportedSpecialImage(cha  r* filename)
    {
        special_image = static_cast<void *>(new SpecialImage(filename));
    }
    
    int ExportedSpecialImage::getExportedSpecialImageColou  r(int x, int y)
    {
        return static_cast<SpecialImage *>special_image->getSpecialImageColour(x, y);
    }
    /////////////////////////////////////////////////////////
    which is messy... but seems to work, it also hides the type of our member variable completely in the distributed header file...

    thanks for your help.
    "take the long road.... and walk it."

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    No need to use void*. Why not do what Codeplug suggested?

  5. #5
    Registered User dug's Avatar
    Join Date
    Jun 2003
    Posts
    66
    Quote Originally Posted by dug
    ...it also hides the type of our member variable completely in the distributed header file...
    yeah, his answer was a good one... and i could do it that way, but the less i expose the better for this particular problem. thanks though.
    "take the long road.... and walk it."

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    I can't see what harm exposing the forward declaration could do..it's almost certainly less of a Bad Thing than using void* when you do in fact know the type.

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    void* special_image;
    
    ...
    
    ExportedSpecialImage::~ExportedSpecialImage()
    {
        if(special_image!=NULL)delete special_image;
    }
    I don't think the destructor is going to be called with this construct. You should cast is back to the proper type before calling delete.

  8. #8
    Registered User dug's Avatar
    Join Date
    Jun 2003
    Posts
    66
    well, the example here is a simple one, but my actual project is much more complicated... so void* is working fine for me, and i am happy with it...

    and like i say, i totally accept that codeplug has illustrated the best, cleanest way to do it... so respect to him [her? don't want to be sexist], and i admitted that my solution is messy... but it's actually the one required for my particular situation.
    "take the long road.... and walk it."

  9. #9
    Registered User dug's Avatar
    Join Date
    Jun 2003
    Posts
    66
    Quote Originally Posted by anonytmouse
    Code:
    void* special_image;
    
    ...
    
    ExportedSpecialImage::~ExportedSpecialImage()
    {
        if(special_image!=NULL)delete special_image;
    }
    I don't think the destructor is going to be called with this construct. You should cast is back to the proper type before calling delete.
    yes, this is correct anonytmouse, the code should actually be:

    Code:
    ExportedSpecialImage::~ExportedSpecialImage()
    {
        if(special_image!=NULL)delete static_cast<SpecialImage*>(special_image);
    }
    "take the long road.... and walk it."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DLL Load issue
    By George2 in forum Windows Programming
    Replies: 2
    Last Post: 09-01-2007, 03:42 PM
  2. Replies: 3
    Last Post: 11-16-2006, 04:23 AM
  3. dll issue
    By axr0284 in forum C++ Programming
    Replies: 1
    Last Post: 03-23-2006, 08:37 AM
  4. dll communicating between each other
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2005, 02:20 AM
  5. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM