Program Language/C & C++

[C++] 명시적 링크 (Explcit Linking) 시 GetProcAddress NULL 반환

야곰야곰+책벌레 2021. 7. 29. 18:40
728x90
반응형

아래는 mdsn에서 제공하는 dynamic linking 예제이다.

평소에 아무 생각없이 잘 사용하다가 갑자기 GetProcAddress에서 계속해서 NULL이 Return 된다.

 

ProcAdd = (MYPROC) GetProcAddress(hinstLib, "myPuts"); 

함수이름도 매개변수 형식도 모두 맞지만 계속해서 NULL이 반환 되었다.

그래서 이것저것 테스트해 본결과 namespace 안에 있는 함수를 가져 오지 못하는 것이었다.

그럼, 함수 이름 앞에 네임스페이스::함수 이렇게 하면 동작할려나.. @_@

 

왜 당연히 읽어 올거라고 생각한걸까.. 네임스페이스 안은 완전 다른 세상인데...

 

// A simple program that uses LoadLibrary and 
// GetProcAddress to access myPuts from Myputs.dll. 
 
#include <windows.h> 
#include <stdio.h> 
 
typedef int (__cdecl *MYPROC)(LPWSTR); 
 
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;

}
728x90
반응형