最近看到了一个模拟LoadLibrary的代码,其中有这么一段
if
(g_bIsWinNT)
...
{
// === Windows NT DLL Loading (supports shared sections) ===

if(svMappingName!=NULL) ...{
hmapping=OpenFileMapping(FILE_MAP_WRITE,TRUE,svMappingName);
bCreated=FALSE;
}

if(hmapping==NULL) ...{
hmapping=CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,dwImageSize+SIZE_OF_PARAMETER_BLOCK,svMappingName);
if(hmapping==NULL) return NULL;
bCreated=TRUE;
}

// Try to load file mapping view at preferred image base (not gonna happen in Win9x..sigh)
pImageBase=MapViewOfFileEx(hmapping,FILE_MAP_WRITE,0,0,0,pPreferredImageBase);

if(pImageBase==NULL) ...{
pImageBase=MapViewOfFileEx(hmapping,FILE_MAP_WRITE,0,0,0,NULL);
}
CloseHandle(hmapping);
if(pImageBase==NULL) return NULL;

}
else
...
{
// === Windows 9x DLL Loading (does not support shared sections) ===
pImageBase=VirtualAlloc(pPreferredImageBase,dwImageSize,MEM_COMMIT|MEM_RESERVE,PAGE_READWRITE);

if(pImageBase==NULL) ...{
pImageBase=VirtualAlloc(NULL,dwImageSize,MEM_COMMIT|MEM_RESERVE,PAGE_READWRITE);
if(pImageBase==NULL) return NULL;
}

bCreated=TRUE;
}
可见Windows NT和Windows 9x下面对于dll的空间分配是不一样的,Windows NT会使用共享内存,而Windows 9x对任何DLL都会产生一个新的空间来放置.
所以同一个程序,在9x下面每次运行,模块的地址都很不一样,而在NT下面只要在同一个机器下面,每次运行都很可能一样(除非模块变化了--比如中了注入的病毒),道理就在这里