简体   繁体   English

如何从C#调用混合模式C ++ / CLI程序集?

[英]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. 我编写了一个混合模式的C ++ / CLI程序集,它包装了一个本机C ++库。 It compiles successfully. 它编译成功。 I can write a C++/CLI application that uses that assembly, so I know it works. 我可以编写一个使用该程序集的C ++ / CLI应用程序,所以我知道它有效。

So I wrote a C# app that uses the same C++/CLI assembly. 所以我写了一个使用相同C ++ / CLI程序集的C#应用​​程序。 This also compiles fine. 这也很好。 But when I try to run it, I get "BadImageFormatException," with the detailed exception message below. 但是当我尝试运行它时,我得到“BadImageFormatException”,下面有详细的异常消息。

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, 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:

The most likely cause is that you're running a 64-bit OS, and there's a 32/64-bit mismatch (eg, the DLL is 32-bit and the app is 64-bit/AnyCPU). 最可能的原因是您运行的是64位操作系统,并且存在32/64位不匹配(例如,DLL为32位且应用程序为64位/ AnyCPU)。

To fix, go to your app's properties and select x86 instead of AnyCPU. 要修复,请转到您应用的属性,然后选择x86而不是AnyCPU。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM