Click here to Skip to main content
15,891,033 members
Articles / Programming Languages / C++
Article

A class to wrap DLL functions

Rate me:
Please Sign up or sign in to vote.
4.76/5 (36 votes)
25 Jun 20021 min read 312.1K   2.5K   81   79
Make using of dll functions much easy

Introduction

My project needs to manage a large set of DLLs. All of them export a same set of functions. None of them link with the main project at compile time. We call them PlugIns. These plugins are used to extend the ability of the main program. Once they are copied into the program directory, the program can use them, and if they are removed from the directory, the main program works just fine, except that some functions cannot be used.

Since these DLLs are not available at the time that the main program is compiled, we have to use LoadLibrary, GetProcAddress and FreeLibrary to use these DLLs. It's really boring! I needed a class to ease this job. In fact, these DLLs looks just like classes (they all have the same interface: a set of functions). Using a class to wrap these DLLs is just reasonable.

Here comes the class CDLLModule. This class is only for deriving. Let's see how to use it first. Suppose you have a DLL which export two functions like this:

int WINAPI GetEngine(char *szEngineType, char *szEngineVersion);
int WINAPI StartEngine(HWND hWnd);

The wrapper class would like this:

class CMyClass : public CDLLModule
{
    DECLARE_DLL_FUNCTION(int, WINAPI,
                          GetEngine, (char *, char *))
    DECLARE_DLL_FUNCTION(int, WINAPI,
                                  StartEngine, (HWND))

    BEGIN_DLL_INIT()
        INIT_DLL_FUNCTION(int, WINAPI,
             GetEngine, (char *, char *), "GetEngine")
        INIT_DLL_FUNCTION(int, WINAPI,
                   StartEngine, (HWND), "StartEngine")
    END_DLL_INIT()
};

Then we can use it like this:

CMyClass    module;
module.Init("MyDLL.dll");
module.GetEngine(m_str1, m_str2);
module.StartEngine(m_hWnd);

I should explain it in more detail, but I am really not good at English. Anyway, the code is very simple, only 64 lines including comments. I think it's very useful, and I hope you think so too.

One last thing. It's a good idea to check the validation of the function before calling. In another word, if you are not sure that all functions are exported well from the DLL, Call it like this:

if (module.GetEngine)
    module.GetEngine(m_str1, m_str2);

Enjoy :)

Revision History

26 Jun 2002 - Initial Revision

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
China China
I'm a chinese programer living in Shanghai, currently working for a software company whose main business is to deliver computer based testing. Software simulation for computer based testing and certifications is my main responsibility in this company. Execpt for software development, I like out-door activities and photography. I am willing to make friends in China and all over the world, so contact me if you have anything in common with meSmile | :)

Comments and Discussions

 
GeneralRe: You want interfaces: Just use real Interfaces! Pin
Ingo Kellpinski25-Jun-02 23:38
Ingo Kellpinski25-Jun-02 23:38 
GeneralRe: You want interfaces: Just use real Interfaces! Pin
Neil Yao25-Jun-02 23:42
Neil Yao25-Jun-02 23:42 
GeneralRe: You want interfaces: Just use real Interfaces! Pin
Chris Losinger26-Jun-02 5:41
professionalChris Losinger26-Jun-02 5:41 
GeneralRe: You want interfaces: Just use real Interfaces! Pin
Ingo Kellpinski26-Jun-02 6:13
Ingo Kellpinski26-Jun-02 6:13 
GeneralRe: You want interfaces: Just use real Interfaces! Pin
Chris Losinger26-Jun-02 6:26
professionalChris Losinger26-Jun-02 6:26 
GeneralRe: You want interfaces: Just use real Interfaces! Pin
.WTL26-Jun-02 6:41
.WTL26-Jun-02 6:41 
GeneralRe: You want interfaces: Just use real Interfaces! Pin
Chris Losinger26-Jun-02 6:42
professionalChris Losinger26-Jun-02 6:42 
GeneralRe: You want interfaces: Just use real Interfaces! Pin
Ingo Kellpinski26-Jun-02 6:52
Ingo Kellpinski26-Jun-02 6:52 
> big deal. with a DLL, you change the function name, if you need to change its signature.
> if the interface changes, you still have to recode the things that use it

No! If an interface has a UUID it "cannot" change. Of course some programmers do. In this case the interface (its syntax or semantic) becomes invalid.
If you want to change it you may:
- extend it: encapsulate it by a new interface (then the old functionality is still available and only the extensions have to be recoded)
- create a new one: this will have a new UUID (you have to recode your caller since you wrote a new interface)

This may sound simple but it is very important.

>> decouple the interface from the object/algorithm implementation
> this is not a problem with standard DLLs, either

Of course not. Everything can be re-implemented and mostly with new bugs.

>> no manual LoadLibrary calls,
> instead, you get the joys of reference counting.

Use smart pointers!
If you use the MSVC++ compiler and if compatibility to other compilers doesn't matter I recommend the #import "My.dll" directive. This will create the smart pointers etc. automatically.

> in my experience, COM is usually much more trouble than it's worth. the only times COM has been
> useful for me is in cases where there is no alternative: COM+ for doing web server components,
> for example. otherwise, it's just a big, fat, clumsy wrapper around LoadLibrary/GetProcAddress.

Of course COM is not the one and only solution. But I have made often good experience with it and still think that DLL programming is very susceptible to bugs.

Greetings, Ingo.


GeneralRe: You want interfaces: Just use real Interfaces! Pin
Chris Losinger26-Jun-02 7:02
professionalChris Losinger26-Jun-02 7:02 
GeneralRe: You want interfaces: Just use real Interfaces! Pin
Ingo Kellpinski26-Jun-02 7:09
Ingo Kellpinski26-Jun-02 7:09 
GeneralRe: You want interfaces: Just use real Interfaces! Pin
Chris Losinger26-Jun-02 7:13
professionalChris Losinger26-Jun-02 7:13 
GeneralRe: You want interfaces: Just use real Interfaces! Pin
Ingo Kellpinski26-Jun-02 7:16
Ingo Kellpinski26-Jun-02 7:16 
GeneralRe: You want interfaces: Just use real Interfaces! Pin
#realJSOP26-Jun-02 7:43
mve#realJSOP26-Jun-02 7:43 
GeneralRe: You want interfaces: Just use real Interfaces! Pin
Chris Losinger26-Jun-02 7:47
professionalChris Losinger26-Jun-02 7:47 
GeneralRe: You want interfaces: Just use real Interfaces! Pin
Chris Maunder26-Jun-02 13:39
cofounderChris Maunder26-Jun-02 13:39 
GeneralRe: You want interfaces: Just use real Interfaces! Pin
#realJSOP27-Jun-02 5:08
mve#realJSOP27-Jun-02 5:08 
GeneralRe: You want interfaces: Just use real Interfaces! Pin
Sam Levy26-Jun-02 12:44
Sam Levy26-Jun-02 12:44 
GeneralRe: You want interfaces: Just use real Interfaces! Pin
Zac Howland1-Jul-02 6:23
Zac Howland1-Jul-02 6:23 
GeneralRe: You want interfaces: Just use real Interfaces! Pin
Jim A. Johnson26-Jun-02 14:08
Jim A. Johnson26-Jun-02 14:08 
GeneralRe: You want interfaces: Just use real Interfaces! Pin
Neil Yao26-Jun-02 15:38
Neil Yao26-Jun-02 15:38 
GeneralRe: You want interfaces: Just use real Interfaces! Pin
Ingo Kellpinski26-Jun-02 22:47
Ingo Kellpinski26-Jun-02 22:47 
GeneralWhat do you gain? Pin
Arnt Witteveen4-Nov-02 4:28
Arnt Witteveen4-Nov-02 4:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.