Can't compile: BadImageFormatException

I’m trying to compile a project for iOS using Unity 4.3 and am getting the following two errors:

BadImageFormatException: Format of the executable (.exe) or library (.dll) is invalid.
Mono.Cecil.PE.ImageReader.ReadImage ()
Mono.Cecil.PE.ImageReader.ReadImageFrom (System.IO.Stream stream)
Mono.Cecil.ModuleDefinition.ReadModule (System.IO.Stream stream, Mono.Cecil.ReaderParameters parameters)
Mono.Cecil.ModuleDefinition.ReadModule (System.String fileName, Mono.Cecil.ReaderParameters parameters)
Mono.Cecil.ModuleDefinition.ReadModule (System.String fileName)
Mono.Cecil.AssemblyDefinition.ReadAssembly (System.String fileName)
UnityEditor.AssemblyReferenceChecker.CollectReferences (System.String path, Boolean withMethods, Single progressValue, Boolean ignoreSystemDlls)
UnityEditor.AssemblyReferenceChecker.GetScriptsHaveMouseEvents (System.String path)
UnityEditor.HostView:OnGUI()

…and…

!IsDirectoryCreated(kManagedDllsFolder)
UnityEditor.HostView:OnGUI()

The project was compiling fine yesterday, and I haven’t made that many changes today. I’ve tried reinstalling Unity and deleting the /Temp folder, as well as changing the Api Compatibility Level from .NET 2.0 Subset to .NET 2.0, but nothing seems to work. Does anyone have an idea? I’m going nuts and would appreciate the help.

2 Likes

Hmm… this exception usually means that a DLL you’re trying to access either isn’t managed or is corrupt. Is there anything else that’s been compiled in your project or maybe a 3rd party assembly that’s compiled for a target (like x64 instead of Any CPU)?

Thanks for the reply. It just got weirder. I created a new, empty project. It compiled. Then I created blank C# and JS scripts (leaving the default, empty functions), and hit build. I got the exact same error as above.

I looked in Temp/StagingArea/Data/Managed, and both Assembly-CSharp.dll and Assembly-UnityScript.dll are Zero bytes(!). The same files in Library/ScriptAssemblies and 3KB and 2KB respectively. What is going on? Any ideas?

Rebooted my QNAP NAS box where the project is stored. Seems to have solved the problem. Ugh.

I’m getting the same error after updating Unity to 4.3.3f1. So frustrating.

Does anyone know how to find what DLL is giving the error?

I had this same problem in Unity 5.6, Google Cardboard 1.5, until I moved my project to the main drive of my mac. It failed with this error when building on a second partition of the drive.

2 Likes

Wanted to report that I’m using Unity 5.6.1f1 on Windows 10, trying to build a Google Daydream app and I had to move my project onto my main drive. Trying to build from a secondary drive was giving me a “!IsDirectoryCreated(kManagedDllsFolder)” error with an IO exception related to the DLL.

Moving it to C: drive fixed the issue.

1 Like

It happens with this project for me. I have no idea to solve any corruption that is causing this error.

I finally fixed this. May not work for everyone.
I deleted Everything, without of (/keeping) Assets and Packages folder. And after opening again, the problem solved.
I think there was a problem with the library folder. Something might had corrupted.
Note that I also installed new version of Unity. But I think it has not effected my project so much.
Again, might not work for all but it may help you.

3 Likes

I had this error too, I had my project in an SSD so I copied it to the main drive and now the project builds without errors… The issue is now in Xcode saying ‘Linker command failed with exit code 1 (use -v to see invocation)’

Oh well one step at a time xD

I don’t have space on my main drive. Anyway to make builds work from external drive?

I had the same error as Jerware using version 2022, but it but i fixed it by using 2021 instead.

Never mind. It does not work. It was working, but now it stopped. Ugh.

I have also same error ):

System.BadImageFormatException: Format of the executable (.exe) or library (.dll) is invalid at Mono.Cecil.PE.ImageReader.ReadImage()

ya same error except i dont wanna risk losing my progress in my vr game

uhh will this delete any progress on my game like the stuff i built

any one still having this problem just close unity and delete the library folder it will not delete anything and will fix the problem

1 Like

Hey,

Im still having issues with this and I have deleted my libary folder twice.

Got anymore Ideas on what it could be?

System.BadImageFormatException: Format of the executable (.exe) or library (.dll) is invalid.
at Mono.Cecil.PE.ImageReader.ReadImage()
at Mono.Cecil.PE.ImageReader.ReadImage(Disposable1 stream, String file_name) at Mono.Cecil.ModuleDefinition.ReadModule(Disposable1 stream, String fileName, ReaderParameters parameters)
at Mono.Cecil.ModuleDefinition.ReadModule(String fileName, ReaderParameters parameters)
at ApiUpdater.MovedFromOptimizer.Program.CollectMovedFromTypeNamesFromAssembly(String assemblyPath, StreamWriter outputFile, IAPIUpdaterListener logger)
at ApiUpdater.MovedFromOptimizer.Program.RealMain(String[ ] args)
at ApiUpdater.MovedFromOptimizer.Program.Main(String[ ] args)

Error building Player because scripts had compiler errors

Build completed with a result of ‘Failed’ in 5 seconds (5255 ms)
2 errors
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

Im going to keep trying but so far it hasnt worked, Had an issue earlier and it seemed to resolve that.

will post here if I find a fix.

well it was easy to fix just install 2D package from package manager