-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLAAUtils.cs
More file actions
38 lines (32 loc) · 1.38 KB
/
LAAUtils.cs
File metadata and controls
38 lines (32 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System.IO;
namespace ModAPI.Common
{
public static class LAAUtils
{
private const long POINTER_TO_PE_HEADER = 0x3C;
private const long CHARACTERISTICS_OFFSET_FROM_PE_HEADER = 0x16;
private const ushort IMAGE_FILE_LARGE_ADDRESS_AWARE = 0x20;
/// <summary>
/// Returns true if the specified path is a LAA (Large Address Aware, aka 4GB patched) executable.
/// The specified path must be a valid Windows executable, otherwise behavior is undefined.
/// </summary>
public static bool IsLAA(string path)
{
// Based on https://stackoverflow.com/a/9056757
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
using (var reader = new BinaryReader(stream))
{
// Locate PE header
reader.BaseStream.Position = POINTER_TO_PE_HEADER;
var peHeaderPosition = reader.ReadInt32();
// Locate Characteristics field
reader.BaseStream.Position = peHeaderPosition + CHARACTERISTICS_OFFSET_FROM_PE_HEADER;
// Check if the LAA flag is set
var characteristics = reader.ReadUInt16();
return (characteristics & IMAGE_FILE_LARGE_ADDRESS_AWARE) != 0;
}
}
}
}
}