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

I wrote a C ++ / CLI assembly in mixed mode that wraps my own C ++ library. It compiles successfully. I can write a C ++ / CLI application that uses this assembly, so I know this works.

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

I think this exception happens because my build is mixed mode and therefore "unsafe". But from what I read, even unsafe builds should be trusted when starting from the local hard drive that I am doing.

Can someone help me understand what is going on here? Is this what I'm trying to do is even possible?

Detailed error 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, 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: 
+4
source share
1 answer

The most likely reason is that you are using a 64-bit OS and there is a 32/64-bit mismatch (for example, the DLL is 32-bit and the application is 64-bit / AnyCPU).

To fix the error, go to the application properties and select x86 instead of AnyCPU.

+9
source

All Articles