Addresses of Delphi and C ++ WinAPI Functions Differ When They Should Not

In C ++, if you try to get a pointer to a function of a Windows API function, this pointer points to the same address that you would get if you used GetProcAddress for the name of this function and the corresponding module. For example:

 &MessageBoxA == GetProcAddress("User32.dll", "MessageBoxA"); 

will be true . However, in Delphi this is not so. This code:

 @MessageBoxA = GetProcAddress('User32.dll', 'MessageBoxA'); 

It would not be true , and in my test @MessageBoxA was 0x0040bd18 , while the equivalent GetProcAdress returned what the C ++ test instance did, 0x7550fd1e .

So now for my question: why?

+2
winapi delphi delphi-2010 getprocaddress
source share
1 answer

The address with 0x004 .. is the declaration address of the imported api function (in windows.pas for MessageBoxA ) so that it is statically loaded , therefore, it will, of course, be in the executable sample (which by default has the base address 0x00400000). The actual function is called the image of the library loaded into the memory of this function. You can get the library image database using GetModuleHandle . In your case, it will probably be something with 0x75 ... With the C ++ test, you are probably linking to the runtime library, so the function is dynamically loaded anyway.

+6
source share

All Articles