来自 Windows GetLogicalProcessorInformationEx 函数的混淆 ReturnLength [英] Confusing ReturnLength from Windows GetLogicalProcessorInformationEx function

查看:17
本文介绍了来自 Windows GetLogicalProcessorInformationEx 函数的混淆 ReturnLength的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Windows 中使用(相当新的)GetLogicalProcessorInformationEx 函数.它给出的 ReturnLength 没有意义.

I'm trying to use the (fairly new) GetLogicalProcessorInformationEx function in Windows. The ReturnLength it gives isn't making sense.

较旧的 GetLogicalProcessorInformation 给出了合理的结果...

The older GetLogicalProcessorInformation gives reasonable results...

ReturnLength = 0;
Result = GetLogicalProcessorInformation(NULL, &ReturnLength);
printf("GLPI (%d): %d %d\n",
       Result,
       sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION),
       ReturnLength);

这是输出(2 核、64 位、Win7 盒):GLPI (0): 32 416

Here's the output (2-core, 64-bit, Win7 box): GLPI (0): 32 416

换句话说,该函数将使用 416/32=13 SYSTEM_LOGICAL_PROCESSOR_INFORMATION 结构填充我传递的缓冲区.

In other words, the function will populate the buffer I pass with 416/32=13 SYSTEM_LOGICAL_PROCESSOR_INFORMATION structures.

对于 GetLogicalProcessorInformationEx,这是我的电话...

For GetLogicalProcessorInformationEx, here's my call...

ReturnLength = 0;
Result = GetLogicalProcessorInformationEx(RelationProcessorCore,
                                          NULL, &ReturnLength);
printf("GLPIX (%d): %d %d %d\n",
       Result,
       sizeof(PROCESSOR_RELATIONSHIP),
       sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX),
       ReturnLength);

这是输出(2 核、64 位、Win7 盒):GLPIX (0): 40 80 96

Here's the output (2-core, 64-bit, Win7 box): GLPIX (0): 40 80 96

Microsoft 文档 (http://msdn.microsoft.com/en-us/library/windows/desktop/dd405488(v=vs.85).aspx) 表明该函数将返回 PROCESSOR_RELATIONSHIP 或 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX 结构,取决于第一个参数的值.ReturnLength 表明它也不会返回,尽管 - 96 不能被 sizeof(PROCESSOR_RELATIONSHIP) 或 sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX) 整除.

The Microsoft docs (http://msdn.microsoft.com/en-us/library/windows/desktop/dd405488(v=vs.85).aspx) indicate that the function will return either PROCESSOR_RELATIONSHIP or SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX structures, depending on the value of the first argument. ReturnLength suggests it isn't going to return either, though - 96 isn't divisible by sizeof(PROCESSOR_RELATIONSHIP) or sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX).

我还为第一个参数尝试了 RelationAll,结果返回长度为 768 - 也不是倍数、40 或 80.

I also tried RelationAll for the first argument, and that gave a ReturnLength of 768 - also not a multiple or 40 or 80.

任何人都可以透露任何信息吗?

Can anyone shed any light?

推荐答案

您需要相信函数返回的内容.必然如此,联合中的结构具有不可预测的大小.特别是 PROCESSOR_RELATIONSHIP 的这个成员:

You'll need to trust what the function returns you. Necessarily so, the structures in the union have an unpredictable size. Particularly this member of PROCESSOR_RELATIONSHIP:

  GROUP_AFFINITY GroupMask[ANYSIZE_ARRAY];

ANYSIZE_ARRAY 宏是提示,它表示 GroupMask 数组的大小是可变的,取决于 GroupCount 成员的值.在结构上使用 sizeof 永远不会给你正确的大小,它会太小.请务必使用返回的大小为结构分配存储空间,如下所示:

The ANYSIZE_ARRAY macro is the hint, that says that the size of the GroupMask array is variable and depends on the value of the GroupCount member. Using sizeof on the structure never gives you the correct size, it will be too low. Be sure to use the returned size to allocate the storage for the struct, like this:

SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* buf = 
    (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*)malloc(ReturnLength);

这种模式在 C 和 winapi 中很常见.

This pattern is otherwise common in C and the winapi.

这篇关于来自 Windows GetLogicalProcessorInformationEx 函数的混淆 ReturnLength的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆