Addresses of Delphi and C++ WinAPI functions differ when they shouldn't

孤人 提交于 2019-12-11 00:05:02

问题


In C++, if you try to get a function pointer of a Windows API function, that pointer points to the same address you would get if you used GetProcAddress on the name of that function and it's respective module. For example:

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

would be true. However, in Delphi, that is not the case. This code:

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

Would not be true, and in my test, @MessageBoxA was 0x0040bd18 while the equivalent GetProcAdress returned what the test's C++ counterpart did, 0x7550fd1e.

So now for my question: why?


回答1:


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



来源:https://stackoverflow.com/questions/5137317/addresses-of-delphi-and-c-winapi-functions-differ-when-they-shouldnt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!