Run-Time 동적 연결 사용

로드 시간 및 런타임 동적 연결 모두에서 동일한 DLL을 사용할 수 있습니다. 다음 예제에서는 LoadLibrary 함수를 사용하여 Myputs DLL에 대한 핸들을 가져옵니다( 간단한 Dynamic-Link 라이브러리 만들기 참조). LoadLibrary가 성공하면 프로그램은 GetProcAddress 함수에서 반환된 핸들을 사용하여 DLL의 myPuts 함수의 주소를 가져옵니다. DLL 함수를 호출한 후 프로그램은 FreeLibrary 함수를 호출하여 DLL을 언로드합니다.

프로그램은 런타임 동적 연결을 사용하므로 모듈을 DLL에 대한 가져오기 라이브러리와 연결할 필요가 없습니다.

이 예제에서는 런타임과 로드 시간 동적 연결 간의 중요한 차이점을 보여 줍니다. DLL을 사용할 수 없는 경우 로드 시간 동적 연결을 사용하는 애플리케이션은 단순히 종료되어야 합니다. 그러나 런타임 동적 연결 예제는 오류에 응답할 수 있습니다.

// A simple program that uses LoadLibrary and 
// GetProcAddress to access myPuts from Myputs.dll. 
 
#include <windows.h> 
#include <stdio.h> 
 
typedef int (__cdecl *MYPROC)(LPCWSTR); 
 
int main( void ) 
{ 
    HINSTANCE hinstLib; 
    MYPROC ProcAdd; 
    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 
 
    // Get a handle to the DLL module.
 
    hinstLib = LoadLibrary(TEXT("MyPuts.dll")); 
 
    // If the handle is valid, try to get the function address.
 
    if (hinstLib != NULL) 
    { 
        ProcAdd = (MYPROC) GetProcAddress(hinstLib, "myPuts"); 
 
        // If the function address is valid, call the function.
 
        if (NULL != ProcAdd) 
        {
            fRunTimeLinkSuccess = TRUE;
            (ProcAdd) (L"Message sent to the DLL function\n"); 
        }
        // Free the DLL module.
 
        fFreeResult = FreeLibrary(hinstLib); 
    } 

    // If unable to call the DLL function, use an alternative.
    if (! fRunTimeLinkSuccess) 
        printf("Message printed from executable\n"); 

    return 0;

}

런타임 동적 연결