2010-12-02 70 views
4

我写过一个混合模式的C++/CLI程序集,它包装了一个本地C++库。它编译成功。我可以编写一个使用该程序集的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: 

回答

9

最可能的原因是,你运行的是64位操作系统,并有一个六十四分之三十二位不匹配(例如,DLL是32位和该应用程序是64位/ AnyCPU)。

要修复,请转到您的应用的属性,然后选择x86而不是AnyCPU。

+1

微软真的需要修复这个错误信息,因为几乎每个C++/CLI程序员至少运行一次。它不应该很难包含“当前进程是`x86_64`,但该库只能在`x86(32位)`环境中加载”。 (或者`Itanium`,或者`ARM`或`MIPS`,如果你试图加载一个为WinCE设计的DLL) – 2010-12-02 19:48:19

相关问题