我正在开发一个C#程序,该程序将加载文件并获取诸如已加载文件的创建日期,修改日期,大小等信息。我需要了解的另一件事是,已加载文件(executable.exe)是否与LARGEADDRESSAWARE标志链接。 FileInfo类不提供此信息。

有谁知道在C#中如何找出给定的execute.exe是否与LARGEADDRESSAWARE标志链接(以处理大于2 GB的地址)?

最佳答案

这是一些代码,用于检查大地址感知标志。您要做的就是传递一个指向可执行文件开始的流。

IsLargeAware("some.exe");

static bool IsLargeAware(string file)
{
    using (var fs = File.OpenRead(file))
    {
        return IsLargeAware(fs);
    }
}
/// <summary>
/// Checks if the stream is a MZ header and if it is large address aware
/// </summary>
/// <param name="stream">Stream to check, make sure its at the start of the MZ header</param>
/// <exception cref=""></exception>
/// <returns></returns>
static bool IsLargeAware(Stream stream)
{
    const int IMAGE_FILE_LARGE_ADDRESS_AWARE = 0x20;

    var br = new BinaryReader(stream);

    if (br.ReadInt16() != 0x5A4D)       //No MZ Header
        return false;

    br.BaseStream.Position = 0x3C;
    var peloc = br.ReadInt32();         //Get the PE header location.

    br.BaseStream.Position = peloc;
    if (br.ReadInt32() != 0x4550)       //No PE header
        return false;

    br.BaseStream.Position += 0x12;
    return (br.ReadInt16() & IMAGE_FILE_LARGE_ADDRESS_AWARE) == IMAGE_FILE_LARGE_ADDRESS_AWARE;
}

关于c# - 如何检查exe是否设置为LARGEADDRESSAWARE,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9054469/

10-11 15:30