解决方案是选项嵌入在 PE 二进制标头中。但与其
loadedImage.FileHeader.FileHeader.Characteristics
在里面:
loadedImage.FileHeader.OptionalHeader.DllCharacteristics
在哪里设置标志:
IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE = 0x0040; //The DLL can be relocated at load time.
对于辅助函数伪代码:
void SetPEOptFlags(String filename, UInt32 flags)
{
// Any code is released into the public domain. No attribution required.
LOADED_IMAGE li;
MapAndLoad(filename, null, li, false, false);
li.FileHeader.OptionalHeader.DllCharacteristics |= flags;
UnMapAndLoad(li);
}
然后打电话
//Optional dll characteristics
const IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE = 0x0040; //The DLL can be relocated at load time.
const IMAGE_DLLCHARACTERISTICS_NX_COMPAT = 0x0100; //The image is compatible with data execution prevention (DEP).
SetPEOptFlags("C:\Foo\Contoso.exe",
IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE | IMAGE_DLLCHARACTERISTICS_NX_COMPAT);