c# - 在 Win 7 上,GetSystemInfo 总是从 Windows 服务返回 0 分配粒度

标签 c# .net winapi windows-7 pinvoke

我正在尝试使用 Windows 7 上的 C# 3.5 Windows 服务应用程序中的 GetSystemInfo() 获取分配粒度大小。但是,从调用返回时,SYSTEM_INFO 结构在 dwAllocationGranularity 中始终为 0(其他字段已填充数据正如预期的那样)

SYSTEM_INFO 结构看起来像这样,为简洁起见省略了 PROCESSOR_ARCHITECTURE 和 PROCESSOR_TYPE 枚举:

public struct SYSTEM_INFO
{
    public PROCESSOR_ARCHITECTURE wProcessorArchitecture;
    public ushort wReserved;
    public uint dwPageSize;
    public int lpMinimumApplicationAddress;
    public int lpMaximumApplicationAddress;
    public uint dwActiveProcessorMask;
    public uint dwNumberOfProcessors;
    public PROCESSOR_TYPE dwProcessorType;
    public uint dwAllocationGranularity;
    public ushort wProcessorLevel;
    public ushort wProcessorRevision;
}

对 GetSystemInfo 的外部调用是这样的:

[DllImport("kernel32")]
public static extern void GetSystemInfo(ref SYSTEM_INFO SystemInfo); 

调用代码是这样的:

SYSTEM_INFO sysInfo = new SYSTEM_INFO();
GetSystemInfo(ref sysInfo);

运行代码后输出的 SYS_INFO 结构为:

dwActiveProcessorMask        4294901759
dwAllocationGranularity      0
dwNumberOfProcessors         2047
dwPageSize                   4096
dwProcessorType              15
lpMaximumApplicationAddress  0
lpMinimumApplicationAddress  65536
wProcessorArchitecture       9
wProcessorLevel              4
wProcessorRevision           0
wReserved                    0

关于我遗漏了什么的任何想法或关于获取此信息的其他方法的建议(我不想硬编码到 64Kb JIC 它在某些时候发生了变化)?谢谢。

最佳答案

你也没有 2047 处理器 :) 声明是错误的,它会在 64 位模式下失败。 lpMin/MaxApplicationAddress 和 dwActiveProcessorMask 是 IntPtr。

关于c# - 在 Win 7 上,GetSystemInfo 总是从 Windows 服务返回 0 分配粒度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7532507/

相关文章:

c++ - 为什么我的 Button 看起来像是 1990 年的?

c++ - boost::interprocess_mutex 与 Win32 native 互斥锁的性能如何?

c# - 在 ASP.Net 中跟踪文件下载点击/计数

c# - 如何在 C# 中获取图像的 DPI

c# - XDocument.Load() 在 DTD header 中引入空的 InternalSubset

c# - NServiceBus - 商业许可的建议工作线程数

c - Windows XP 和 Windows 7 之间注销通知事件的差异

c# - 当我可以在派生类中隐藏它时,为什么我需要声明一个虚拟方法

c# - 重命名存储过程导致 Entity Framework 代码优先

c# - 在您的单元测试项目中引用 System.Windows.Form 是一种不好的做法吗?