GetSystemInfo总是从Win Service上的Win7上的dwAllocationGranularity返回0

时间:2011-09-23 17:07:49

标签: 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的extern调用是这样的:

[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它在某些时候被更改)?感谢。

1 个答案:

答案 0 :(得分:7)

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