LoadLibrary GetProcAddress - 为什么这有效?

-1 c windows winapi loadlibrary getprocaddress

当我使用具有函数名称的 Visual Studio Code 运行此代码时"MessageBoxA",它可以工作,并且为我提供了指向该函数的指针。

但是,当我将函数名称更改为(例如)"MessageBoxATEST"并保存时,它仍然为我提供相同的函数地址。

这怎么可能(因为这个函数在 中不存在user32.dll)?

例如,当我将 DLL 的名称更改为 时User32TEST.dll,它会给出错误Function not loaded .... ,这是应该的。

有人可以帮忙吗?

#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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Ben*_*igt 7

使用函数名称"MessageBoxA"它确实可以工作,并且它为我提供了指向该函数的指针

不,事实并非如此。

当我将函数名称更改为 example"MessageBoxATEST"并保存它时,它仍然为我提供相同的函数地址

它从来都不是函数地址。

你忘了真正打电话GetProcAddress。您现在拥有的是(在考虑逗号运算符的行为之后),(func_pointer)"MessageBoxATest"它显然不是 NULL 指针。