我编写了一个混合模式的C ++ / CLI程序集,它包装了一个本机C ++库。它编译成功。我可以编写一个使用该程序集的C ++ / CLI应用程序,所以我知道它可以工作。
所以我编写了一个使用相同C ++ / CLI程序集的C#应用程序。这也很好。但是当我尝试运行它时,我得到“BadImageFormatException”,下面会有详细的异常消息。
我认为这种情况正在发生,因为我的程序集是混合模式,因此“不安全”。但是从我所读到的内容来看,即使是不安全的程序集也应该在从本地硬盘运行时受到信任,我正在这样做。
任何人都可以帮我理解这里发生了什么吗?我正在努力做甚么可能吗?
详细的异常消息:
System.BadImageFormatException was unhandled Message="Could not load file or assembly 'asillyclass, Version=1.0.3988.20325, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format." Source="ConsoleApplication1" FileName="asillyclass, Version=1.0.3988.20325, Culture=neutral, PublicKeyToken=null" FusionLog="=== Pre-bind state information === : User = SIG\\user : DisplayName = asillyclass, Version=1.0.3988.20325, Culture=neutral, PublicKeyToken=null\n (Fully-specified) : Appbase = file:///C:/projects/API/TestApp-C#/ConsoleApplication1/bin/Debug/ : Initial PrivatePath = NULL assembly : ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null. === : This bind starts in default load context. : Using application configuration file: C:\\projects\\API\\TestApp-C#\\ConsoleApplication1\\bin\\Debug\\ConsoleApplication1.vshost.exe.config : Using machine configuration file from C:\\Windows\\Microsoft.NET\\Framework64\\v2.0.50727\\config\\machine.config. : Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind). : Attempting download of new URL file:///C:/projects/API/TestApp-C#/ConsoleApplication1/bin/Debug/asillyclass.DLL. : Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated. " StackTrace: at ConsoleApplication1.Program.Main(String[] args) at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:
答案 0 :(得分:9)
最可能的原因是您运行的是64位操作系统,并且存在32/64位不匹配(例如,DLL为32位且应用程序为64位/ AnyCPU)。
要修复,请转到您应用的属性,然后选择x86而不是AnyCPU。