Click here to Skip to main content
11,500,106 members (64,344 online)
Click here to Skip to main content

A class to wrap DLL functions

, 25 Jun 2002 37 2.1K 78
Rate this:
Please Sign up or sign in to vote.
Make using of dll functions much easy
<!-- Download Links --> <!-- Add the rest of your HTML here -->

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 Smile | :)

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

Share

About the Author

Neil Yao

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

 
QuestionFreeLibrary function VS 2005 - WXP Pin
picm24-Apr-10 1:30
memberpicm24-Apr-10 1:30 
GeneralHummmmmm Pin
Kelly Cristina Mara2-Mar-08 12:10
memberKelly Cristina Mara2-Mar-08 12:10 
GeneralMeaning of the second parametr DECLARE_DLL_FUNCTION Pin
badzio23-Feb-07 2:40
memberbadzio23-Feb-07 2:40 
GeneralRe: Meaning of the second parametr DECLARE_DLL_FUNCTION Pin
badzio23-Feb-07 2:57
memberbadzio23-Feb-07 2:57 
GeneralDLL code Pocket PC Pin
Ziggy2k27-Sep-05 6:26
memberZiggy2k27-Sep-05 6:26 
GeneralRe: DLL code Pocket PC Pin
Neil Yao27-Sep-05 18:56
memberNeil Yao27-Sep-05 18:56 
GeneralOPSEC SDK Pin
Anonymous24-May-05 5:34
sussAnonymous24-May-05 5:34 
QuestionWhat abt Consructor and others?? Pin
Pradip K. Jadav4-Mar-05 19:42
memberPradip K. Jadav4-Mar-05 19:42 
QuestionHow to calling this DLL into VB6 Pin
alejorb7-Aug-04 6:48
memberalejorb7-Aug-04 6:48 
AnswerRe: How to calling this DLL into VB6 Pin
Yao Zhifeng8-Aug-04 15:45
memberYao Zhifeng8-Aug-04 15:45 
Generaltanx Pin
asd175325-Nov-03 10:41
memberasd175325-Nov-03 10:41 
GeneralRe: tanx Pin
Ralph Wetzel9-Apr-04 2:29
memberRalph Wetzel9-Apr-04 2:29 
Generalcannot access private member declared in class 'CDLLClass' Pin
posbis9-Sep-03 10:43
memberposbis9-Sep-03 10:43 
GeneralRe: cannot access private member declared in class 'CDLLClass' Pin
posbis9-Sep-03 10:58
memberposbis9-Sep-03 10:58 
GeneralRe: cannot access private member declared in class 'CDLLClass' Pin
Yao Zhifeng9-Sep-03 14:59
memberYao Zhifeng9-Sep-03 14:59 
Questionhow to use it with no arguements Pin
nikhilpp8-Aug-03 7:43
membernikhilpp8-Aug-03 7:43 
AnswerRe: how to use it with no arguements Pin
Yao Zhifeng11-Aug-03 15:05
memberYao Zhifeng11-Aug-03 15:05 
GeneralSuggestion to use typedef in the macros Pin
Andrew Schetinin5-Jul-03 22:45
memberAndrew Schetinin5-Jul-03 22:45 
GeneralI forgot about UNICODE too Pin
Andrew Schetinin5-Jul-03 22:51
memberAndrew Schetinin5-Jul-03 22:51 
Generalcreating a Game engine DLL Pin
sriniatig10-Apr-03 22:57
membersriniatig10-Apr-03 22:57 
GeneralRe: creating a Game engine DLL Pin
Yao Zhifeng13-Apr-03 15:38
memberYao Zhifeng13-Apr-03 15:38 
GeneralRe: creating a Game engine DLL Pin
Andrew Schetinin3-Jul-03 3:36
memberAndrew Schetinin3-Jul-03 3:36 
GeneralHeader From lib Pin
bfadi2-Mar-03 14:15
memberbfadi2-Mar-03 14:15 
GeneralDebug Error! Pin
Jack Furr14-Feb-03 22:19
sussJack Furr14-Feb-03 22:19 
GeneralRe: Debug Error! Pin
Jack Furr15-Feb-03 15:14
sussJack Furr15-Feb-03 15:14 
Generalanother question Pin
alexkid99918-Dec-02 23:17
memberalexkid99918-Dec-02 23:17 
GeneralRe: another question Pin
Yao Zhifeng19-Dec-02 14:02
memberYao Zhifeng19-Dec-02 14:02 
QuestionSomeone could help me????? Pin
alexkid9995-Dec-02 23:51
memberalexkid9995-Dec-02 23:51 
AnswerRe: Someone could help me????? Pin
Yao Zhifeng8-Dec-02 18:57
memberYao Zhifeng8-Dec-02 18:57 
GeneralRe: Someone could help me????? Pin
alexkid9998-Dec-02 22:16
memberalexkid9998-Dec-02 22:16 
Generalunsigned int* return value of dll function Pin
Juergen Klingler23-Aug-02 1:33
memberJuergen Klingler23-Aug-02 1:33 
GeneralRe: unsigned int* return value of dll function Pin
Yao Zhifeng25-Aug-02 21:50
memberYao Zhifeng25-Aug-02 21:50 
QuestionHow to dynamically load a MFC dll ? Pin
WilliamXu5188-Jul-02 4:56
memberWilliamXu5188-Jul-02 4:56 
GeneralThere is one more approach -- DELAYLOAD Pin
igor196026-Jun-02 17:39
memberigor196026-Jun-02 17:39 
GeneralRe: There is one more approach -- DELAYLOAD Pin
Arbesto Pelta27-Jun-02 3:24
memberArbesto Pelta27-Jun-02 3:24 
GeneralRe: There is one more approach -- DELAYLOAD Pin
igor196027-Jun-02 17:57
memberigor196027-Jun-02 17:57 
GeneralRe: There is one more approach -- DELAYLOAD Pin
Anonymous5-Nov-02 10:26
sussAnonymous5-Nov-02 10:26 
GeneralRe: There is one more approach -- DELAYLOAD Pin
Philippe Lhoste1-Jul-02 2:56
memberPhilippe Lhoste1-Jul-02 2:56 
GeneralRe: There is one more approach -- DELAYLOAD Pin
Arbesto Pelta1-Jul-02 8:04
memberArbesto Pelta1-Jul-02 8:04 
GeneralRe: There is one more approach -- DELAYLOAD Pin
igor19601-Jul-02 21:15
memberigor19601-Jul-02 21:15 
GeneralRe: There is one more approach -- DELAYLOAD Pin
Philippe Lhoste1-Jul-02 21:47
memberPhilippe Lhoste1-Jul-02 21:47 
GeneralRe: There is one more approach -- DELAYLOAD Pin
Anonymous2-Jul-02 19:27
memberAnonymous2-Jul-02 19:27 
GeneralRe: There is one more approach -- DELAYLOAD Pin
Philippe Lhoste2-Jul-02 22:04
memberPhilippe Lhoste2-Jul-02 22:04 
GeneralRe: There is one more approach -- DELAYLOAD Pin
Anonymous5-Jul-02 21:13
memberAnonymous5-Jul-02 21:13 
GeneralRe: There is one more approach -- DELAYLOAD Pin
Philippe Lhoste7-Jul-02 21:54
memberPhilippe Lhoste7-Jul-02 21:54 
GeneralRe: There is one more approach -- DELAYLOAD Pin
Arbesto Pelta10-Jul-02 10:20
memberArbesto Pelta10-Jul-02 10:20 
GeneralRe: There is one more approach -- DELAYLOAD Pin
Philippe Lhoste10-Jul-02 21:53
memberPhilippe Lhoste10-Jul-02 21:53 
GeneralAnybody know how to modify the article after being removed from the unedited section Pin
Yao Zhifeng26-Jun-02 15:42
memberYao Zhifeng26-Jun-02 15:42 
GeneralRe: Anybody know how to modify the article after being removed from the unedited section Pin
KarstenK20-Feb-03 23:09
memberKarstenK20-Feb-03 23:09 
GeneralAnalogy with HTTP homemade APIs vs. SOAP Pin
Joaquín M López Muñoz26-Jun-02 13:47
memberJoaquín M López Muñoz26-Jun-02 13:47 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 26 Jun 2002
Article Copyright 2002 by Neil Yao
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid