Click here to Skip to main content
Licence 
First Posted 1 May 2001
Views 240,821
Bookmarked 107 times

Loading DLLs made easy

By | 1 May 2001 | Article
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

About the Author

Shoalin Panda



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
SuggestionMaking it unicode safe PinmemberMember 822898012:33 24 Jan '12  
Questionwhy else? Pinmemberuur0:50 4 Oct '10  
AnswerRe: why else? PinmemberMember 822898012:42 24 Jan '12  
GeneralRe: why else? Pinmemberuur21:17 24 Jan '12  
Generalmaking dll with matlab Pinmembertimo9115:16 8 Apr '07  
GeneralCall VC++ DLL in Matlab PinmemberSylvianne0:58 14 Jun '06  
GeneralI am getting runtime error 53 Pinmemberdeva2k11:10 7 Apr '06  
GeneralRe: I am getting runtime error 53 Pinmembervijayan.S2:12 18 Apr '07  
GeneralDebugging -DLL PinmemberAntonymaharaj20:00 7 Mar '06  
QuestionOverride DLLs? Pinmemberngoctrongda6:45 28 Sep '05  
AnswerRe: Override DLLs? Pinmembernoxmortis1:45 12 Aug '08  
GeneralError adding to my project Pinmemberbuho_usp5:27 2 Jun '05  
AnswerRe: Error adding to my project Pinmembernoxmortis1:49 12 Aug '08  
GeneralCall VB 6.0 DLL from C Pinmemberdagwood20055:32 17 Sep '04  
QuestionHow to link implicitly a foreign DLL PinmemberCEERI19:53 16 Sep '04  
AnswerRe: How to link implicitly a foreign DLL Pinmemberdagwood20055:50 17 Sep '04  
AnswerRe: How to link implicitly a foreign DLL Pinmemberdeva2k11:23 7 Apr '06  
GeneralDLL project ignores its RGS settings Pinmemberccoti20:03 26 Jun '04  
QuestionRe: DLL project ignores its RGS settings Pinmembernoxmortis4:48 12 Aug '08  
Generalproblem with inpout32.dll Pinmemberdnqhung1:04 10 Jun '04  
GeneralRe: problem with inpout32.dll Pinmembernoxmortis2:03 12 Aug '08  
Generalserial port com communication on W2000/xp PinsussAnonymous0:04 4 Jun '04  
GeneralReloading ResourcesDLL Pinmembersakthisaravanan2:47 26 May '04  
AnswerRe: Reloading ResourcesDLL Pinmembernoxmortis4:47 12 Aug '08  
QuestionError? PinmemberDaFrawg1:36 12 May '04  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 2 May 2001
Article Copyright 2001 by Shoalin Panda
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid