我刚刚进入一个基于处理器架构加载.dll
的项目。编译运行正常,但在运行时我收到System.BadImageFormatException
。
在进一步检查时,我注意到代码引用了包System.Runtime.InteropServices.RuntimeInformation
v4.3.0并检查了ProcessArchitecture并报告了X86而不是X64。
我在64位i7处理器上运行Windows 10,并在测试项目中运行以下内容:
// returns true
var is64Bit = IntPtr.Size == 8;
// returns true
// kernel32 pinvoke attempted, shortened here just to get the point across
GetNativeSystemInfo(ref info);
var isReally64Bit = info.wProcessorArchitecture == 9;
// returns true
// kernel32 shortened
GetSystemInfo(ref info);
var isReallyReally64Bit = info.wProcessorArchitecture == 9;
// returns false (ProcessorArchitecture = x86)
var isProcessor64 = System.Runtime.InteropServices.RuntimeInformation.ProcessorArchitecture ==
System.Runtime.InteropServices.Architecture.X64;
前三种方法确定我在x64架构上,最后一次告诉我我是x86。有什么我想念的吗?我的目标是.netcore 2.0,并审核了来源RuntimeInformation和SYSTEM_INFO,但没有一个看起来与众不同。
** 更新 **
我的目标是.netcore 2.0并注意到RuntimeInformation源它正在调用GetSystemInfo,而在我的测试中我正在调用GetNativeSystemInfo。我试过调用两个并明确编译了x64,在我上面的测试代码中都给了我x64。有什么想法吗?
答案 0 :(得分:0)
请尝试System.InteropServices.RuntimeInformation.OSArchitecture
。
或者,如果使用.NET Framework< 4.7.1,有一个Environment.Is64BitOperatingSystem
布尔属性,可用于确定OS位数。