Click here to Skip to main content
Click here to Skip to main content

Loading DLLs made easy

, 1 May 2001
Rate this:
Please Sign up or sign in to vote.
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
No Biography provided

Comments and Discussions

 
GeneralMy vote of 3 PinmemberMạnh Lê5-Nov-13 20:33 
QuestionLoadLibrary succeeded but GetLastError returns 203 Pinmemberagak4-Aug-12 0:51 
AnswerRe: LoadLibrary succeeded but GetLastError returns 203 Pinmemberdhamusport2-Jul-14 1:11 
SuggestionMaking it unicode safe PinmemberMember 822898024-Jan-12 12:33 
Questionwhy else? Pinmemberuur4-Oct-10 0:50 
AnswerRe: why else? PinmemberMember 822898024-Jan-12 12:42 
GeneralRe: why else? Pinmemberuur24-Jan-12 21:17 
Generalmaking dll with matlab Pinmembertimo9118-Apr-07 5:16 
GeneralCall VC++ DLL in Matlab PinmemberSylvianne14-Jun-06 0:58 
GeneralI am getting runtime error 53 Pinmemberdeva2k17-Apr-06 1:10 
GeneralRe: I am getting runtime error 53 Pinmembervijayan.S18-Apr-07 2:12 
GeneralDebugging -DLL PinmemberAntonymaharaj7-Mar-06 20:00 
QuestionOverride DLLs? Pinmemberngoctrongda28-Sep-05 6:45 
AnswerRe: Override DLLs? Pinmembernoxmortis12-Aug-08 1:45 
GeneralError adding to my project Pinmemberbuho_usp2-Jun-05 5:27 
AnswerRe: Error adding to my project Pinmembernoxmortis12-Aug-08 1:49 
GeneralCall VB 6.0 DLL from C Pinmemberdagwood200517-Sep-04 5:32 
QuestionHow to link implicitly a foreign DLL PinmemberCEERI16-Sep-04 19:53 
AnswerRe: How to link implicitly a foreign DLL Pinmemberdagwood200517-Sep-04 5:50 
AnswerRe: How to link implicitly a foreign DLL Pinmemberdeva2k17-Apr-06 1:23 
GeneralDLL project ignores its RGS settings Pinmemberccoti26-Jun-04 20:03 
QuestionRe: DLL project ignores its RGS settings Pinmembernoxmortis12-Aug-08 4:48 
Generalproblem with inpout32.dll Pinmemberdnqhung10-Jun-04 1:04 
GeneralRe: problem with inpout32.dll Pinmembernoxmortis12-Aug-08 2:03 
Generalserial port com communication on W2000/xp PinsussAnonymous4-Jun-04 0: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.

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