LoadLibrary GetProcAddress - why does this work?

123 views Asked by At

When I run this code with Visual Studio Code with the function name "MessageBoxA", it works, and it provides me a pointer to the function.

However, when I change the name of the function to, for example, "MessageBoxATEST" and save it, it still provides me the same function address.

How is that possible (since this function does not exist in user32.dll)?

When I change, for example, the name of the DLL to User32TEST.dll, it gives me the error Function not loaded .... , as it should.

Can anybody help?

#include <windows.h>
#include <stdio.h>

typedef BOOL(*func_pointer)(LPSTR, LPCSTR);
int main () {

    HMODULE Handle_DLL = LoadLibrary("User32.dll");
    func_pointer Pointer = (func_pointer)(Handle_DLL,"MessageBoxATEST");

    if (Handle_DLL == NULL || Pointer == NULL){
        DWORD error = GetLastError();
        printf("Function not loaded into memory %d\n", error);
        return 1;
    }
    else{
        printf("Function address: %p\n", (void*)Pointer);
        FreeLibrary(Handle_DLL);
        return 0;
    }
}
1

There are 1 answers

1
Ben Voigt On

with the function name "MessageBoxA" it does work and it provides me a pointer to the function

No, it doesn't.

when I change the name of the function to for example "MessageBoxATEST" and save it, it still provides me the same function address

It never was a function address.

You forgot to actually call GetProcAddress. What you have now is (after considering the behavior of the comma operator) (func_pointer)"MessageBoxATest" which is clearly not a NULL pointer.