简体   繁体   English

处理器体系结构问题(x86与任何CPU)

[英]Problems with processor architecture (x86 vs any cpu)

I have a library that is built for x86. 我有一个为x86构建的库。 When compiling .net projects in "any cpu" that is referencing the assembly, I get the following compiler warning: 在引用程序集的“任何cpu”中编译.net项目时,我收到以下编译器警告:

"There was a mismatch between the processor architecture of the project being built "MSIL" and
the processor architecture of the reference "xxx", processorArchitecture=MSIL", "x86". This
mismatch may cause runtime failures. Please consider changing the targeted processor
achitecture of your project through the Configuration Manager..."

I have two projects. 我有两个项目。 Project A and B. Both is .net c# visual studio (2013) projects. 项目A和B。这两个都是.net c#visual studio(2013)项目。 Project A is a windows service, project B is a simple windows forms application. 项目A是Windows服务,项目B是简单的Windows窗体应用程序。 They both use a common library (also any cpu) that again (sometimes) use the x86 library. 它们都使用一个公共库(也可以是任何cpu),而再次(有时)使用x86库。

Project A(Windows Service,AnyCpu)-->CommonLibrary(AnyCPU)-->UtilityLibrary(x86) 项目A(Windows Service,AnyCpu)-> CommonLibrary(AnyCPU)-> UtilityLibrary(x86)

Project B(WinForms, AnyCpu)-->CommonLibrary(AnyCPU)-->UtilityLibrary(x86) 项目B(WinForms,AnyCpu)-> CommonLibrary(AnyCPU)-> UtilityLibrary(x86)

In Project A, I get a runtime error when using the UtilityLibrary. 在项目A中,使用UtilityLibrary时出现运行时错误。 But not from Project B. The runtime error in Project A is resolved once the UtilityLibrary is built in "Any Cpu", or Project A is built in x86, so i know it is related to this. 但是不是来自项目B。一旦在“ Any Cpu”中构建UtilityLibrary,或者在x86中构建了Project A,就可以解决项目A中的运行时错误,因此我知道它与此相关。

My question is: In which cases does the runtime errors of utilizing architecture specific libraries occur? 我的问题是:在哪些情况下会发生利用特定于体系结构的库的运行时错误?

The UtilityLibrary is a part for a recent software release, and the CommonLibrary is an API also part of the software release. UtilityLibrary是最新软件版本的一部分,而CommonLibrary是API也是该软件版本的一部分。 I am trying to determine the consequences of this. 我正在尝试确定其后果。 In future releases, the UtilityLibrary will be built for AnyCpu, as there is no reason for this to be X86. 在将来的版本中,将为AnyCpu构建UtilityLibrary,因为没有理由将其作为X86。

If running on a 64-bit machine, the standard behaviour is to run an AnyCPU executable in a 64-bit process, rather than a 32-bit process. 如果在64位计算机上运行,​​则标准行为是在64位进程而不是32位进程中运行AnyCPU可执行文件。 An assembly marked x86 will fail to load in a 64-bit process. 标有x86的程序集将无法在64位进程中加载​​。 The .NET Framework assumes that there's something in there which is architecture-specific, and that it's better to report the problem up-front, rather than crash or do the wrong thing when an architecture-specific operation is performed. .NET Framework假定其中存在特定于体系结构的内容,并且最好是预先报告问题,而不是在执行特定于体系结构的操作时崩溃或执行错误的操作。

The build tools are warning you that this will happen if you do run it in that scenario. 生成工具警告您,如果在那种情况下运行它,将会发生这种情况。

If you're sure that the UtilityLibrary doesn't contain any processor-specific code or depend on anything that's only available as a 32-bit library (eg the JET database engine), you can avoid a recompile by changing the headers, using CorFlags.exe . 如果您确定UtilityLibrary不包含任何特定于处理器的代码,或者仅依赖于只能作为32位库使用的任何内容(例如JET数据库引擎),则可以通过使用CorFlags更改标头来避免重新编译.exe The command you need is: 您需要的命令是:

corflags <path-to-assembly> /32bit-

In .NET 4.5, a new option became available: 'Prefer 32-bit'. 在.NET 4.5中,提供了一个新选项:“首选32位”。 For the C# compiler, this option is /platform:anycpu32bitpreferred . 对于C#编译器,此选项为/platform:anycpu32bitpreferred An executable marked with this value will run in a 32-bit process on a 64-bit machine, if the 32-bit runtime is available. 如果有32位运行时可用,则带有此值标记的可执行文件将在64位计算机上的32位进程中运行。 In Windows 7/Server 2008 R2 and later, it's actually possible to remove the WOW64 layer that allows 32-bit programs to run. 在Windows 7 / Server 2008 R2和更高版本中,实际上可以删除允许32位程序运行的WOW64层。 If you marked the executable as 'x86', it wouldn't run at all without WOW64, but if you used Any CPU + Prefer 32-bit, it would run as a 64-bit process instead. 如果将可执行文件标记为“ x86”,则在没有WOW64的情况下根本无法运行,但是如果您使用Any CPU + Prefer 32-bit,它将作为64位进程运行。

The defaults for Visual Studio 2013 are 'Any CPU' and 'Prefer 32-bit', for new Windows Forms and Windows Service projects. 对于新的Windows窗体和Windows服务项目,Visual Studio 2013的默认值为“任何CPU”和“首选32位”。 (Tested on Update 3.) Projects upgraded from older versions of Visual Studio may have different settings. (在Update 3上进行了测试。)从Visual Studio的较早版本升级的项目可能具有不同的设置。 This might explain why ProjectB works while ProjectA doesn't, if ProjectB was created on an older version, or if someone has changed that setting. 这可能可以解释为什么ProjectB不能工作而ProjectA不能工作,为什么在较旧的版本上创建了ProjectB或有人更改了该设置。

Check you build settings in detail. 检查您详细的构建设置。 In addition to the architecture choice, there is a "prefere 32 bit" checkbox. 除了体系结构选择之外,还有一个“首选32位”复选框。 This checkbox is checked by default for windows client applications, that's why your WinForms application runs in 32 bit and works. Windows客户端应用程序默认情况下选中此复选框,这就是WinForms应用程序以32位运行并且可以工作的原因。 Most likely your Windows Service doesn't have that checked and therefore runs in 64 bit. Windows Service很可能没有对此进行检查,因此以64位运行。

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

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