Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

'System.BadImageFormatException' when building for UWP

Discussion in 'Windows' started by paskal007r, Apr 25, 2016.

  1. paskal007r

    paskal007r

    Joined:
    Sep 6, 2013
    Posts:
    68
    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?
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,638
    Sounds like corrupted dll file.
    First of all make sure antivirus software isn't intervening with the build.
     
    paskal007r likes this.
  3. paskal007r

    paskal007r

    Joined:
    Sep 6, 2013
    Posts:
    68
    I tried to:
    1. temporarily disable avg
    2. erase build folder
    3. build project again
    4. open UWP project in VS
    5. see that deploys and runs well
    6. close VS project
    7. go back in unity editor
    8. change a text
    9. save scene
    10. build again
    11. open UWP project in VS
    12. see that deploys
    13. try to run again
    Result:
    System.TypeInitializationException

    Which is a different exception than before, so I'm even more confused... :S
     
  4. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
    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?
     
  5. paskal007r

    paskal007r

    Joined:
    Sep 6, 2013
    Posts:
    68
    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.
     
  6. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
  7. paskal007r

    paskal007r

    Joined:
    Sep 6, 2013
    Posts:
    68
    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:

    Code (CSharp):
    1. '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.
    2. 'cortanaTester.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Users\pasqu\Documents\TestCortana\cortanaTester\WindowsBuild\cortanaTester\bin\x86\Debug\AppX\entrypoint\cortanaTester.exe'. Symbols loaded.
    3. '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.
    4. '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.
    5. '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.
    6. '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.
    7. '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.
    8. '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.
    9. '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.
    10. '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.
    11. '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.
    12. '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.
    13. '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.
    14. '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.
    15. '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.
    16. '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.
    17. '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.
    18. '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.
    19. '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.
    20. '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.
    21. '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.
    22. '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.
    23. '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.
    24. '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.
    25. '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.
    26. '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.
    27. '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.
    28. Module information:
    29. Built with Compiler Ver '190023026'
    30. Built from '5.3/patch-release' branch
    31. Version is '5.3.4p4 (43a3882ae25c)'
    32. Debug build
    33. Application type 'XAML'
    34. Used 'UWP'
    35.  
    36. PlayerConnection initialized from C:/Users/pasqu/Documents/TestCortana/cortanaTester/WindowsBuild/cortanaTester/bin/x86/Debug/AppX/Data (debug = 0)
    37.  
    38. PlayerConnection initialized network socket : 0.0.0.0 55401
    39.  
    40. 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]...
    41.  
    42. Exception thrown: 'System.FormatException' in mscorlib.ni.dll
    43. GfxDevice: creating device client; threaded=1
    44.  
    45. Disabling Low Latency presentation API.
    46.  
    47. Direct3D:
    48.  
    49.     Version:  Direct3D 11.0 [level 9.3]
    50.  
    51.     Renderer: VMware SVGA 3D (ID=0x405)
    52.  
    53.     Vendor:   VMWare
    54.  
    55.     VRAM:     656 MB
    56.  
    57. Initialize engine version: 5.3.4p4 (43a3882ae25c)
    58.  
    59. Exception thrown: 'System.Runtime.InteropServices.COMException' in mscorlib.ni.dll
    60. 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
    61. Installing Voice Commands Failed: System.Runtime.InteropServices.COMException (0x80045578): The text associated with this error code could not be found.
    62.  
    63. 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
    64.    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    65.    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    66.    at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
    67.    at cortanaTester.App.<OnLaunched>d__5.MoveNext()
    68. s_PhysicalDpi > 0.0f
    69. (Filename: C:\buildslave\unity\build\PlatformDependent/MetroPlayer/MetroUtils.cpp Line: 249)
    70.  
    71.  
    72. The program '[7516] cortanaTester.exe' has exited with code -2147483645 (0x80000003).
    73.  
     
  8. paskal007r

    paskal007r

    Joined:
    Sep 6, 2013
    Posts:
    68
    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?
     
  9. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,638
    The duplicate indicates unclean build. Looks like AssemblyConverter processed assemblies twice or something like that. Can you try the latest patch release?
     
  10. paskal007r

    paskal007r

    Joined:
    Sep 6, 2013
    Posts:
    68
    I forgot to mention it, @Aurimas Cernius , I'm now with 5.3.4p4 Personal.
    Just to recap in a more readable way:
    1. I make my project in unity, set it to target windows 10, xaml
    2. I build it
    3. I go in the build folder and open the solution in VS
    4. I set it to Debug, x86 (it's a windows 10 virtual machine with 32 bit) and Local Machine
    5. I temporarily disable AVG
    6. I run it from VS and goes fine
    7. I close VS, back in unity
    8. I change the content of a UI text and save the scene
    9. I build again in the same folder
    10. back in VS, open solution again
    11. Same config, no edits, I run by clicking "Local Machine" again.
    12. 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'.
     
  11. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,638
    Does VS build application before the second run? What if you manually click build before running it for the second time?
     
  12. paskal007r

    paskal007r

    Joined:
    Sep 6, 2013
    Posts:
    68
    @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.
    :(
     
  13. rjmyth

    rjmyth

    Joined:
    Nov 22, 2012
    Posts:
    18
    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.
     
  14. paskal007r

    paskal007r

    Joined:
    Sep 6, 2013
    Posts:
    68
    *polite bump*
     
  15. malcomjarr

    malcomjarr

    Joined:
    Nov 18, 2020
    Posts:
    7
    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.