Click here to Skip to main content
15,888,521 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: How to link implicitly a foreign DLL Pin
deva2k17-Apr-06 1:23
deva2k17-Apr-06 1:23 
GeneralDLL project ignores its RGS settings Pin
ccoti26-Jun-04 20:03
ccoti26-Jun-04 20:03 
QuestionRe: DLL project ignores its RGS settings Pin
noxmortis12-Aug-08 4:48
noxmortis12-Aug-08 4:48 
Generalproblem with inpout32.dll Pin
dnqhung10-Jun-04 1:04
dnqhung10-Jun-04 1:04 
GeneralRe: problem with inpout32.dll Pin
noxmortis12-Aug-08 2:03
noxmortis12-Aug-08 2:03 
Generalserial port com communication on W2000/xp Pin
Anonymous4-Jun-04 0:04
Anonymous4-Jun-04 0:04 
GeneralReloading ResourcesDLL Pin
sakthisaravanan26-May-04 2:47
sakthisaravanan26-May-04 2:47 
AnswerRe: Reloading ResourcesDLL Pin
noxmortis12-Aug-08 4:47
noxmortis12-Aug-08 4:47 
Look for LoadLibrary() and GetProcAddress() in the MSDN.
QuestionError? Pin
DaFrawg12-May-04 1:36
DaFrawg12-May-04 1:36 
AnswerRe: Error? Pin
DaFrawg13-May-04 21:53
DaFrawg13-May-04 21:53 
QuestionI can not get the function address! why? Pin
ginee22-Apr-04 22:55
ginee22-Apr-04 22:55 
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 
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 

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.