I read that on windows, modules of an executable are mapped in the same address space. I don't understand why
typedef int (__stdcall *fptr)();
int main(void)
{
HINSTANCE h;
fptr f;
std::stringstream oss;
h = LoadLibrary("test.dll");
if (! h)
return EXIT_FAILURE;
f = (fptr)GetProcAddress(h, "function");
if (! f)
return EXIT_FAILURE;
oss << (DWORD *)f;
std::cout <<"main: "<< oss << std::endl;
_getch();
return EXIT_SUCCESS;
}
and
extern "C" {
void __declspec(dllexport) function() {
return ;
}
}
int main(HMODULE m)
{
std::stringstream oss;
oss << (DWORD *)function;
std::cout << "dll: " << oss << std::endl;
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
main(hModule);
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
yields this result:
> "test.exe"
dll: 007BFDB8
main: 0039F944
Also, the address 007BFD88 can not be accessed from the main process. Why are the two addresses different ?