开发者

How can I call a mixed-mode C++/CLI assembly from C#?

I've written a mixed-mode C++/CLI assembly, which wraps a native C++ library. It compiles successfully. I can write a C++/CLI application that uses that assembly, so I know it works.

So I wrote a C# app that uses the same C++/CLI assembly. This also compiles fine. But when I try to run it, I get "BadImageFormatException," with the detailed exception message below.

I think this exception is happening because my assembly is mixed-mode and therefore "unsafe." But from what I've read, even unsafe assemblies are supposed to be trusted when run from the local harddrive, which I'm doing.

Can anyone help me understand what's going on here? Is what I'm trying to do even possible?

Detailed exception message:

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, C开发者_Go百科ulture=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: 


The most likely cause is that you're running a 64-bit OS, and there's a 32/64-bit mismatch (e.g., the DLL is 32-bit and the app is 64-bit/AnyCPU).

To fix, go to your app's properties and select x86 instead of AnyCPU.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜