'System.BadImageFormatException' when building for UWP

Hi everyone,
I’m trying to build a tech example project in Universal Windows Platform, target Windows 10, with XAML build type.
I also checked Development Build in build settings.

Now, I’m following a tutorial from here http://darkgenesis.zenithmoon.com/making-your-game-stand-out-with-cortana-on-windows-10/ and got to actually have the speech recognition to work.

Now, I got a strange exception: System.BadImageFormatException.
I don’t understand why. Not even what it exactly means.
But I’ve narrowed when it happens.

If I completely delete the contents of the build folder, then build one single time in unity editor and then open the visual studio project in the build folder to follow the tutorial and edit files there, everything goes just fine.

If I go back in unity editor, change anything (even just ui elements) and build again in the same folder, I get the ‘System.BadImageFormatException’ error.

Even without touching a single line of code or player settings.

What am I doing wrong? How can I use that kind of procedure (editing stuff in the visual studio project to get cortana going) and yet work in unity to do stuff that requires testing with cortana during development?

Sounds like corrupted dll file.
First of all make sure antivirus software isn’t intervening with the build.

1 Like

I tried to:

  • temporarily disable avg
  • erase build folder
  • build project again
  • open UWP project in VS
  • see that deploys and runs well
  • close VS project
  • go back in unity editor
  • change a text
  • save scene
  • build again
  • open UWP project in VS
  • see that deploys
  • try to run again

Result:
System.TypeInitializationException

Which is a different exception than before, so I’m even more confused… :S

Does it have lines like “‘Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null’ cannot reduce access.” or similar? Also which Unity version is this?

unity version: 5.3.4f1 Personal
working on a 32 bit windows 10 install

I didn’t see that line and I’m unable to re-run the project to confirm that at the moment, will do so tomorrow morning.
If I can provide any more information please tell me how.

It may have been in the Output window, if it’s something different, please submit a bug with repro project attached.

Additionally, try using latest patch release Download Archive

Well… this is strange, I’m now unable to run again the project at all.

I get some unhandled exception in VS.
Same code on unity side, same copy-paste operations on VS project.

Here’s the output in VS:

'cortanaTester.exe' (CoreCLR: DefaultDomain): Loaded 'C:\Program Files\WindowsApps\Microsoft.NET.CoreRuntime.1.0_1.0.23819.0_x86__8wekyb3d8bbwe\mscorlib.ni.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\entrypoint\cortanaTester.exe'. Symbols loaded.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\System.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\WinMetadata\Windows.winmd'. Module was built without symbols.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\System.Runtime.InteropServices.WindowsRuntime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\UnityPlayer.winmd'. Module was built without symbols.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\System.Threading.Tasks.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\System.Runtime.WindowsRuntime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\System.Diagnostics.Debug.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\System.Runtime.WindowsRuntime.UI.Xaml.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\System.Collections.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\WinRTBridge.winmd'. Cannot find or open the PDB file.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\BridgeInterface.winmd'. Module was built without symbols.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\Assembly-CSharp.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\UnityEngine.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\System.Private.Uri.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\UnityEngine.Networking.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\UnityEngine.UI.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\System.IO.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\System.Net.Primitives.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\System.Private.Networking.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\System.Reflection.Primitives.dll'. Module was built without symbols.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\System.Reflection.dll'. Module was built without symbols.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\System.Text.Encoding.dll'. Module was built without symbols.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\System.Runtime.InteropServices.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\System.Threading.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\System.Runtime.Extensions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Module information:
Built with Compiler Ver '190023026'
Built from '5.3/patch-release' branch
Version is '5.3.4p4 (43a3882ae25c)'
Debug build
Application type 'XAML'
Used 'UWP'

PlayerConnection initialized from C:/Users/pasqu/Documents/TestCortana/cortanaTester/WindowsBuild/cortanaTester/bin/x86/Debug/AppX/Data (debug = 0)

PlayerConnection initialized network socket : 0.0.0.0 55401

Multi-casting "[IP] 192.168.96.128 [Port] 55401 [Flags] 2 [Guid] 3884968313 [EditorId] 3996964826 [Version] 1048832 [Id] MetroPlayerX86(DESKTOP-H701BBB) [Debug] 0" to [225.0.0.222:54997]...

Exception thrown: 'System.FormatException' in mscorlib.ni.dll
GfxDevice: creating device client; threaded=1

Disabling Low Latency presentation API.

Direct3D:

    Version:  Direct3D 11.0 [level 9.3]

    Renderer: VMware SVGA 3D (ID=0x405)

    Vendor:   VMWare

    VRAM:     656 MB

Initialize engine version: 5.3.4p4 (43a3882ae25c)

Exception thrown: 'System.Runtime.InteropServices.COMException' in mscorlib.ni.dll
WinRT information: A PhraseList was referenced inside of a 'ListenFor' or 'Feedback' element to which there is no corresponding PhraseList. Ensure that the reference maps to a PhraseList with the same 'Label' attribute value. Error was found at Line: 10 Position: 1
Installing Voice Commands Failed: System.Runtime.InteropServices.COMException (0x80045578): The text associated with this error code could not be found.

A PhraseList was referenced inside of a 'ListenFor' or 'Feedback' element to which there is no corresponding PhraseList. Ensure that the reference maps to a PhraseList with the same 'Label' attribute value. Error was found at Line: 10 Position: 1
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at cortanaTester.App.<OnLaunched>d__5.MoveNext()
s_PhysicalDpi > 0.0f
(Filename: C:\buildslave\unity\build\PlatformDependent/MetroPlayer/MetroUtils.cpp Line: 249)


The program '[7516] cortanaTester.exe' has exited with code -2147483645 (0x80000003).

update: I disabled the “just my code” option in VS (I don’t know why it was on, maybe I activated it and then forgot) and the exception has returned:

Exception thrown: ‘System.BadImageFormatException’ in cortanaTester.exe

Additional information: Duplicate type with name ‘UnityEngine.Internal.Types.$UnityType1’ in assembly ‘Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null’.

Looks like @Aurimas-Cernius was right!
What do I do then?

The duplicate indicates unclean build. Looks like AssemblyConverter processed assemblies twice or something like that. Can you try the latest patch release?

I forgot to mention it, @Aurimas-Cernius , I’m now with 5.3.4p4 Personal.
Just to recap in a more readable way:

  • I make my project in unity, set it to target windows 10, xaml
  • I build it
  • I go in the build folder and open the solution in VS
  • I set it to Debug, x86 (it’s a windows 10 virtual machine with 32 bit) and Local Machine
  • I temporarily disable AVG
  • I run it from VS and goes fine
  • I close VS, back in unity
  • I change the content of a UI text and save the scene
  • I build again in the same folder
  • back in VS, open solution again
  • Same config, no edits, I run by clicking “Local Machine” again.
  • I get Exception thrown: ‘System.BadImageFormatException’ in cortanaTester.exe
    Additional information: Duplicate type with name ‘UnityEngine.Internal.Types.$UnityType1’ in assembly ‘Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null’.

Does VS build application before the second run? What if you manually click build before running it for the second time?

@Aurimas-Cernius thanks for the reply
Yes, it does build successfully. I also tried to use “clean” then run, rebuild then run, clean then build then run, clean then deploy then run…
Right now I’m trying basically any combination I can think of sequences of compilation/unity build/VS build/runs.
:frowning:

Hey, I’m having the same issue in my main project. Building to a fresh directory works for now, and I’ll see how long that lasts but the issue keeps popping up, often seemingly when the computer is put to sleep/shutdown or visual studio is closed and opened again, or maybe when I change build targets between x86/x64/ARM and debug/Master/Release, when using the .sln file of the build output directory.

Building to a fresh directory will allow me to work somewhat, but my project has multiple external dependancies which I haven’t yet but will have to try individually port over to this new directory possibly each time this error starts occuring.

I found that building from unity > rebuild solution > rebuild firstpass > rebuild solution again and run works for now in the fresh directory.

Please let me know if there’s anything I can do to help get to the bottom of this issue for all of us.

polite bump

The exception that is thrown when the file image of a dynamic link library (DLL) or an executable program is invalid. If you get a BadImageFormatException when interfacing with a native DLL, it almost always means that you are trying to interface with a 32-bit DLL while running in the 64-bit CLR, or vice versa. In most cases you might be facing the problem with your website after deploying on server.

Make sure that you are not having 32-bit / 64-bit conflict. So, you need to adjust your application pool to Enable 32-Bit or 64-Bit accordingly. Set the Target platform setting on your c# EXE project, not your class library project. Alternatively, you can ship both 32-bit and 64-bit DLLs with different file names, define separate P/Invoke stubs for each version, and decide which one to call at runtime. The easiest way to do this would probably be to wrap your native calls in an interface (e.g., INativeMethods) and choose which implementation to instantiate at runtime based on IntPtr.Size. With this method, you could still target AnyCPU.