我已经编写了一个混合模式的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:
因此,我编写了一个使用相同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:
x86_64
,但库只能在x86(32位)
环境中加载”的信息。(如果您尝试加载为WinCE设计的DLL,则为Itanium、ARM或MIPS) - Ben Voigt