【问题标题】:GetSystemInfo always returns 0 for dwAllocationGranularity on Win7 from Win ServiceGetSystemInfo 总是从 Win 服务返回 0 用于 Win7 上的 dwAllocationGranularity
【发布时间】:2011-09-23 17:07:49
【问题描述】:

我正在尝试使用 GetSystemInfo() 从 Windows 7 上的 C# 3.5 Windows 服务应用程序获取分配粒度大小。但是,当调用返回时,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,它在某些时候会更改)?谢谢。

【问题讨论】:

    标签: c# .net winapi windows-7 pinvoke


    【解决方案1】:

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

    【讨论】:

    • 这是一个非常有效和高效的放下好吧。但这仍然是正确的答案,所以你能做什么? :)
    • 你可以试着笑一下,这很有趣而不是放下。放轻松。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 2013-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多