我是Visual Studio 2010项目配置的新手,但我做了一些研究,仍然不能完全解决这个问题。我有一个Visual Studio解决方案的c++ DLL引用c# DLL。c# DLL引用了一些其他DLL,一些在我的项目内,一些在外部。当我试图编译c++ DLL时,我得到这个警告:

警告MSB3270:正在构建的“MSIL”项目的处理器架构与参考“[内部c# dll]”,“x86”的处理器架构之间不匹配。

它告诉我去配置管理器调整我的架构。c# DLL是用目标平台x86建立的。如果我尝试将其更改为其他东西,如任何CPU,它会抱怨,因为它所依赖的外部dll之一具有平台目标x86。

当我查看配置管理器时,它显示了我的c# DLL作为x86和我的c++项目作为Win32的平台。这似乎是正确的设置;当然,我不希望我的c++项目的项目平台设置为x64,这是唯一的其他选项。

我哪里做错了?


当前回答

只是想为那些在这里找不到答案的人解决他们的问题。

运行应用程序时,确保正确设置了解决方案平台下拉菜单。我的是在x86上,这反过来又导致了这个问题。

其他回答

我解决了这个警告,将“配置管理器”更改为发布(混合平台)。

For C# projects, the target of x86 does what it sounds like. It says that this assembly only supports x86 architectures. Likewise for x64. Any CPU on the other hand says that I don't care which architecture, I support both. So, the next 2 questions are (1) what is the configuration of the executable that uses these dlls? and (2) what is the bitness of your OS/Computer? The reason I ask is because if your executable is compiled to run in 64-bit, then it NEEDS all dependencies to be able to run in 64-bit mode as well. Your Any CPU assembly should be able to be loaded, but perhaps it is referencing some other dependency that is only capable of running in x86 configuration. Check all dependencies and dependencies-of-dependencies to make sure everything is either "Any CPU" or "x64" if you plan to run the executable in 64-bit mode. Otherwise, you'll have issues.

在许多方面,Visual Studio并没有使编译任意CPU和各种依赖于体系结构的程序集的混合变得容易。这是可行的,但它通常要求必须针对x86和x64分别编译“任何CPU”的程序集,因为某个依赖的某个依赖有两个版本。

There should be a way to make a .NET EXE/DLL AnyCPU, and any unmanaged DLLs it depends on compiled both with x86 and x64, both bundled perhaps with different filenames and then the .NET module dynamically loading the correct one based on its runtime processor architecture. That would make AnyCPU powerful. If the C++ DLL only supports x86 or x64 then AnyCPU is of course pointless. But the bundling both idea I have yet to see implemented as the configuration manager does not even provide a means to build the same project twice with a different configuration/platform for multiple bundling allowing AnyCPU or even other concepts like any configuration to be possible.

如果你的c# DLL有基于x86的依赖关系,那么你的DLL本身就必须是x86的。我真不知道还有什么办法。VS抱怨将其更改为(例如)x64,因为64位可执行文件无法加载32位库。

I'm a little confused about the configuration of the C++ project. The warning message that was provided for the build suggests that it was targeted for AnyCPU, because it reported the platform that it targeted was [MSIL], but you indicated that the configuration for the project was actually Win32. A native Win32 app shouldn't involve the MSIL - although it would likely need to have CLR support enabled if it is interacting with a C# library. So I think there are a few gaps on the information side.

我能否恭敬地请您回顾并发布更多关于这些项目的确切配置以及它们是如何相互关联的细节?如有可能,我很乐意进一步提供帮助。

我正在使用IIS Express,对我来说,解决方法是确保我的Web项目将比特位设置为64。