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

Loading DLLs made easy

Rate me:
Please Sign up or sign in to vote.
4.66/5 (30 votes)
1 May 2001 312.7K   111   62
How to load dynamic link librarys the easiest way instead of the long way

Introduction

Have you ever got tired of loading Dynamic Link Libraries the long way, with the usual steps LoadLibrary, and GetProcAddress, then you have to check for each function address if they are NULL, and don't mention about casting the function pointer and hard ways that make your brain strain. And wish there was an easier way to get around things? Well this will just do that in a way and is about the easiest way I know of actually

//GetProcAddresses
//Argument1: hLibrary - Handle for the Library Loaded
//Argument2: lpszLibrary - Library to Load
//Argument3: nCount - Number of functions to load
//[Arguments Format]
//Argument4: Function Address - Function address we want to store
//Argument5: Function Name -  Name of the function we want
//[Repeat Format]
//
//Returns: FALSE if failure
//Returns: TRUE if successful
BOOL GetProcAddresses( HINSTANCE *hLibrary, 
    LPCSTR lpszLibrary, INT nCount, ... )
{
    va_list va;
    va_start( va, nCount );

    if ( ( *hLibrary = LoadLibrary( lpszLibrary ) ) 
        != NULL )
    {
        FARPROC * lpfProcFunction = NULL;
        LPSTR lpszFuncName = NULL;
        INT nIdxCount = 0;
        while ( nIdxCount < nCount )
        {
            lpfProcFunction = va_arg( va, FARPROC* );
            lpszFuncName = va_arg( va, LPSTR );
            if ( ( *lpfProcFunction = 
                GetProcAddress( *hLibrary, 
                    lpszFuncName ) ) == NULL )
            {
                lpfProcFunction = NULL;
                return FALSE;
            }
            nIdxCount++;
        }
    }
    else
    {
        va_end( va );
        return FALSE;
    }
    va_end( va );
    return TRUE;
}

So since we now have the main core to this article, lets now look at how to use this with a short sample that was compiled as a Windows console application.

#include <windows.h>

typedef int ( WINAPI *MESSAGEBOX ) 
    ( HWND , LPCSTR, LPCSTR, DWORD );
typedef int ( WINAPI *MESSAGEBOXEX ) 
    ( HWND , LPCSTR, LPCSTR, DWORD , WORD );

void main(void)
{
    MESSAGEBOX lpfMsgBox = NULL;
    MESSAGEBOXEX lpfMsgBoxEx = NULL;
    HINSTANCE hLib;
    if(GetProcAddresses( &hLib, "User32.dll", 2,
        &lpfMsgBox, "MessageBoxA",
        &lpfMsgBoxEx, "MessageBoxExA" ) )
    {
        lpfMsgBox( 0, "Test1", "Test1", MB_OK );
        lpfMsgBoxEx( 0, "Test2", "Test2", MB_OK, 
            MAKELANGID( LANG_ENGLISH, SUBLANG_ENGLISH_US ) );
    }
    if ( hLib != NULL )
        FreeLibrary( hLib );
}

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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow to call vb com dll in c++dll Pin
vbjoo22-Aug-02 4:18
vbjoo22-Aug-02 4:18 
Questionhow to call vb com dll in c++dll Pin
sliba45622-Aug-02 4:17
susssliba45622-Aug-02 4:17 
GeneralPrinting Pin
jeetendra nigam17-Feb-02 4:03
jeetendra nigam17-Feb-02 4:03 
GeneralExcellent Code Pin
27-Aug-01 13:01
suss27-Aug-01 13:01 
GeneralUse DelayLoad!! Pin
Ryan Schneider7-May-01 4:32
Ryan Schneider7-May-01 4:32 
GeneralRe: Use DelayLoad!! Pin
22-May-01 23:30
suss22-May-01 23:30 
GeneralRe: Use DelayLoad!! Pin
RabidCow11-Dec-02 17:31
RabidCow11-Dec-02 17:31 
Generalquake2 way Pin
2-May-01 18:04
suss2-May-01 18:04 
GeneralRe: quake2 way Pin
2-May-01 18:53
suss2-May-01 18:53 
GeneralRe: quake2 way Pin
3-May-01 12:08
suss3-May-01 12:08 
GeneralRe: quake2 way Pin
3-May-01 12:23
suss3-May-01 12:23 
GeneralRe: quake2 way Pin
Prakash Nadar14-Nov-03 21:05
Prakash Nadar14-Nov-03 21:05 

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.