2010-12-02 72 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

相關問題