gpt4 book ai didi

c++ - SetIpInterfaceEntry 返回 ERROR_INVALID_PARAMETER

转载 作者:行者123 更新时间:2023-11-28 04:23:56 31 4
gpt4 key购买 nike

我正在尝试执行 SetIpInterfaceEntry功能,但我总是得到 ERROR_INVALID_PARAMETER 结果。我想更改哪个确切参数并不重要,因为即使我不更改任何内容,我也会收到此错误。这是 C++ 代码示例,它没有任何更改,但输出始终相同:

// Initialize MIB_IPINTERFACE_ROW with actual InterfaceLuid:
auto row = MIB_IPINTERFACE_ROW{ AF_INET, 1689399632855040 };

auto result = GetIpInterfaceEntry(&row);

// Just to confirm GetIpInterfaceEntry success print result and InterfaceIndex:
std::cout << "GetIpInterfaceEntry result: " << result << "; InterfaceIndex: " << row.InterfaceIndex << std::endl;

// Try to execute SetIpInterfaceEntry, without any change:
result = SetIpInterfaceEntry(&row);

// Result is always ERROR_INVALID_PARAMETER (87):
std::cout << "SetIpInterfaceEntry result: " << result << std::endl;

这是上一个片段的输出:

GetIpInterfaceEntry result: 0; InterfaceIndex: 13
SetIpInterfaceEntry result: 87

根据 the docERROR_INVALID_PARAMETER 的可能原因是:

An invalid parameter was passed to the function. This error is returned if a NULL pointer is passed in the Row parameter, the Family member of the MIB_IPINTERFACE_ROW pointed to by the Row parameter was not specified as AF_INET or AF_INET6, or both the InterfaceLuid or InterfaceIndex members of the MIB_IPINTERFACE_ROW pointed to by the Row parameter were unspecified.

在我的情况下,这两个都不是真的(我刚刚得到实际的结构)。

我的问题很明显:我做错了什么?该方法可以调用哪种接口(interface)有限制吗? (我调用实体店。)还有其他建议吗?

谢谢!

更新:我忘了说,如果你想执行这段代码,你应该:

#include <winsock2.h>
#include <netioapi.h>

同时链接 iphlpapi.lib

最佳答案

解决了,感谢我的同事...

原因是在我的例子中 row.SitePrefixLength 字段最初设置为 64,根据 MIB_IPINTERFACE_ROW doc page,这是一个非法值:

For an IPv4 address, any value greater than 32 is an illegal value.

只要我在 SetIpInterfaceEntry 之前插入以下行调用,一切正常:

row.SitePrefixLength = 0;

虽然解决了问题,但是还有几个 Unresolved 问题:

  1. 实际当前值无效是怎么回事?
  2. 如果您在那里遇到无效值,应该输入什么值?看起来将其设置为 0 就是答案,但我不确定。

但首先我应该弄清楚 SitePrefixLength 字段/参数的用途是什么。 :)

更新:我刚刚找到了我剩下的第二个问题的答案 - 它在 the same doc page 中一直(因为我没有阅读所有内容而感到羞耻)。所以答案是:

For IPv4, the SitePrefixLength member must be set to 0.

关于c++ - SetIpInterfaceEntry 返回 ERROR_INVALID_PARAMETER,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54857292/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com