Click here to Skip to main content
15,888,610 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.6K   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

 
AnswerRe: I can not get the function address! why? Pin
noxmortis12-Aug-08 1:55
noxmortis12-Aug-08 1:55 
Generalload functions from a .dll file Pin
Member 7981053-Jan-04 9:11
Member 7981053-Jan-04 9:11 
GeneralRe: load functions from a .dll file Pin
Graham Bartlett28-Feb-04 2:42
sussGraham Bartlett28-Feb-04 2:42 
AnswerRe: load functions from a .dll file Pin
noxmortis12-Aug-08 4:46
noxmortis12-Aug-08 4:46 
GeneralChanging text and background color Pin
Asesh shrestha17-Dec-03 5:53
Asesh shrestha17-Dec-03 5:53 
GeneralRe: Changing text and background color Pin
Selvam R6-Jan-04 3:14
professionalSelvam R6-Jan-04 3:14 
GeneralMicrosoft version problem Pin
Anonymous27-Nov-03 16:47
Anonymous27-Nov-03 16:47 
GeneralRe: Microsoft version problem Pin
DaFrawg12-May-04 1:48
DaFrawg12-May-04 1:48 
Do you mean that only one source code version is needed to compile different programs, one for each platform? Or do you mean that you want to make one program (or more already comiled files) that run on every platform of Windows?
QuestionHow to use .dll that implement from visual c++ in java application. Pin
kikuvee11-Nov-03 21:09
susskikuvee11-Nov-03 21:09 
AnswerRe: How to use .dll that implement from visual c++ in java application. Pin
sunkarakalyan@dacafe.com9-Jul-04 22:30
sunkarakalyan@dacafe.com9-Jul-04 22:30 
GeneralDLL in VC++ Pin
Randy Andy14-Aug-03 2:50
Randy Andy14-Aug-03 2:50 
GeneralRe: DLL in VC++ Pin
Peter Mares23-Oct-03 13:03
Peter Mares23-Oct-03 13:03 
GeneralRe: DLL in VC++ Pin
Sachin R Sangoi12-Jan-07 1:41
Sachin R Sangoi12-Jan-07 1:41 
QuestionHow to make Installation Program to load ATL Com Add-in(.dll) with MS Outlook ? Pin
Atif Bashir12-Aug-03 18:39
Atif Bashir12-Aug-03 18:39 
Generalloading dll to my project.cpp Pin
Arun Moyal15-May-03 23:54
Arun Moyal15-May-03 23:54 
GeneralAn easier way Pin
Jeff Glatt1-Dec-02 18:22
Jeff Glatt1-Dec-02 18:22 
GeneralRe: An easier way Pin
Jeff Glatt5-Nov-03 17:43
Jeff Glatt5-Nov-03 17:43 
QuestionHow to export classes using DLLs Pin
Anonymous6-Nov-02 17:31
Anonymous6-Nov-02 17:31 
AnswerRe: How to export classes using DLLs Pin
Prakash Nadar14-Nov-03 20:35
Prakash Nadar14-Nov-03 20:35 
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 

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.