{"record":{"id":"a5a4d2d6f1f12fb7","repo":"Perfare/Il2CppDumper","slug":"error-invalid-pe-file-a5a4d2","errorCode":null,"errorMessage":"ERROR: Invalid PE file","messagePattern":"ERROR: Invalid PE file","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"Il2CppDumper/Utils/PELoader.cs","lineNumber":21,"sourceCode":"using System.IO;\nusing System.Runtime.InteropServices;\nusing System.Text;\n\nnamespace Il2CppDumper\n{\n    public class PELoader\n    {\n        [DllImport(\"kernel32.dll\", SetLastError = true, CharSet = CharSet.Unicode)]\n        private extern static IntPtr LoadLibrary(string path);\n\n        public static PE Load(string fileName)\n        {\n            var buff = File.ReadAllBytes(fileName);\n            using var reader = new BinaryStream(new MemoryStream(buff));\n            var dosHeader = reader.ReadClass<DosHeader>();\n            if (dosHeader.Magic != 0x5A4D)\n            {\n                throw new InvalidDataException(\"ERROR: Invalid PE file\");\n            }\n            reader.Position = dosHeader.Lfanew;\n            if (reader.ReadUInt32() != 0x4550u) //Signature\n            {\n                throw new InvalidDataException(\"ERROR: Invalid PE file\");\n            }\n            var fileHeader = reader.ReadClass<FileHeader>();\n            if (fileHeader.Machine == 0x14c && Environment.Is64BitProcess) //64bit process can't load 32bit dll\n            {\n                throw new InvalidOperationException(\"The file is a 32-bit file, please try to load it with Il2CppDumper-x86.exe\");\n            }\n            if (fileHeader.Machine == 0x8664 && !Environment.Is64BitProcess) //32bit process can't load 64bit dll\n            {\n                throw new InvalidOperationException(\"The file is a 64-bit file, please try to load it with Il2CppDumper.exe\");\n            }\n            var pos = reader.Position;\n            reader.Position = pos + fileHeader.SizeOfOptionalHeader;\n            var sections = reader.ReadClassArray<SectionHeader>(fileHeader.NumberOfSections);","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/Perfare/Il2CppDumper/blob/4741d46ba9cd6159c5d853eb9d6fc48b4bfa2b1a/Il2CppDumper/Utils/PELoader.cs#L3-L39","documentation":"Thrown by PELoader.Load when the file's DOS header magic is not 0x5A4D ('MZ'). PELoader resolves native exports for script generation (il2cpp bridge DLLs), and this check rejects files that are not Windows PE images before any loading occurs.","triggerScenarios":"Calling PELoader.Load(fileName) on a file that does not start with 'MZ' — e.g. a placeholder, a script, an ELF library, or a download that saved an HTML error page instead of the DLL.","commonSituations":"A required native DLL failed to download and the file is an HTML error page, the path in config points at the wrong file, or someone substituted an .so (ELF) where a .dll was expected.","solutions":["Open the file in a hex editor and confirm it starts with 4D 5A ('MZ').","Re-download the DLL from the correct source; HTML error pages indicate a failed download.","Check the path passed to Load resolves to the actual native DLL, not a text/config file.","If the target library is Linux-only (.so), use the ELF loading path instead of PELoader."],"exampleFix":"// before\nPELoader.Load(\"il2cpp_bridge.txt\"); // not a PE\n// after\nPELoader.Load(\"GameAssembly.dll\"); // starts with MZ","handlingStrategy":"validation","validationCode":"byte[] b = File.ReadAllBytes(dllPath);\nbool isPe = b.Length >= 2 && b[0] == 0x4D && b[1] == 0x5A;","typeGuard":"static bool IsLoadablePe(string path) => File.ReadAllBytes(path) is { Length: > 1 } b && b[0] == 0x4D && b[1] == 0x5A;","tryCatchPattern":"try { PELoader.Load(file); }\ncatch (InvalidDataException ex) when (ex.Message == \"ERROR: Invalid PE file\") { /* re-fetch or correct the DLL path */ }","preventionTips":["Confirm downloads are the actual DLL (check MZ header), not an HTML error page.","Use the ELF loader path for .so libraries, never PELoader.","Validate the configured DLL path points at a real native image."],"tags":["pe","pe-loader","invalid-header"],"backgroundTag":"invalid-argument-format","analyzedSha":"4741d46ba9cd6159c5d853eb9d6fc48b4bfa2b1a","analyzedAt":"2026-09-11T16:52:42.117Z","contentChangedAt":"2026-09-11T16:52:42.117Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}