Click here to Skip to main content
6,630,586 members and growing! (15,789 online)
Email Password   helpLost your password?
General Programming » DLLs & Assemblies » General     Intermediate

A class to wrap DLL functions

By Neil Yao

Make using of dll functions much easy
VC6Win2K, WinXP, Visual Studio, Dev
Posted:25 Jun 2002
Views:208,786
Bookmarked:64 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
49 votes for this article.
Popularity: 7.57 Rating: 4.48 out of 5
2 votes, 5.9%
1
2 votes, 5.9%
2
2 votes, 5.9%
3
4 votes, 11.8%
4
24 votes, 70.6%
5

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

About the Author

Neil Yao


Member
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
Location: China China

Other popular DLLs & Assemblies articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 77 (Total in Forum: 77) (Refresh)FirstPrevNext
GeneralHummmmmm PinmemberKelly Cristina Mara13:10 2 Mar '08  
GeneralMeaning of the second parametr DECLARE_DLL_FUNCTION Pinmemberbadzio3:40 23 Feb '07  
GeneralRe: Meaning of the second parametr DECLARE_DLL_FUNCTION Pinmemberbadzio3:57 23 Feb '07  
GeneralDLL code Pocket PC PinmemberZiggy2k7:26 27 Sep '05  
GeneralRe: DLL code Pocket PC PinmemberNeil Yao19:56 27 Sep '05  
GeneralOPSEC SDK PinsussAnonymous6:34 24 May '05  
GeneralWhat abt Consructor and others?? PinmemberPradip K. Jadav20:42 4 Mar '05  
GeneralHow to calling this DLL into VB6 Pinmemberalejorb7:48 7 Aug '04  
GeneralRe: How to calling this DLL into VB6 PinmemberYao Zhifeng16:45 8 Aug '04  
Generaltanx Pinmemberasd175311:41 25 Nov '03  
GeneralRe: tanx PinmemberRalph Wetzel3:29 9 Apr '04  
Generalcannot access private member declared in class 'CDLLClass' Pinmemberposbis11:43 9 Sep '03  
GeneralRe: cannot access private member declared in class 'CDLLClass' Pinmemberposbis11:58 9 Sep '03  
GeneralRe: cannot access private member declared in class 'CDLLClass' PinmemberYao Zhifeng15:59 9 Sep '03  
Generalhow to use it with no arguements Pinmembernikhilpp8:43 8 Aug '03  
GeneralRe: how to use it with no arguements PinmemberYao Zhifeng16:05 11 Aug '03  
GeneralSuggestion to use typedef in the macros PinmemberAndrew Schetinin23:45 5 Jul '03  
GeneralI forgot about UNICODE too PinmemberAndrew Schetinin23:51 5 Jul '03  
Generalcreating a Game engine DLL Pinmembersriniatig23:57 10 Apr '03  
GeneralRe: creating a Game engine DLL PinmemberYao Zhifeng16:38 13 Apr '03  
GeneralRe: creating a Game engine DLL PinmemberAndrew Schetinin4:36 3 Jul '03  
GeneralHeader From lib Pinmemberbfadi15:15 2 Mar '03  
GeneralDebug Error! PinsussJack Furr23:19 14 Feb '03  
GeneralRe: Debug Error! PinsussJack Furr16:14 15 Feb '03  
Generalanother question Pinmemberalexkid9990:17 19 Dec '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Jun 2002
Editor: Brian Delahunty
Copyright 2002 by Neil Yao
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project