0

我正在尝试编写一个程序,该程序将在命令上切换所选适配器的 IP。

为此,我使用 GetAdaptersInfo() 函数来获取适配器的索引及其 IP 上下文,然后使用它来删除当前 IP(通过 DeleteIPAddress(context))。

之后,将通过 AddIPAddress() 插入新 IP。

问题是,在尝试删除“默认”IP(未由 AddIPAddress() 设置的 IP)并在删除旧 IP 之前调用 AddIPAddress 时,对 DeleteIPAddress 函数的调用失败,只会使适配器具有两个不同的同时 IP。

DeleteIPAddress 函数失败并出现错误 31(连接到系统的设备未运行)。

是否有另一种方法可以成功更改适配器上的 IP 地址?在没有从 AddIPAddress 获取上下文的情况下,DeleteIPAddress 是否真的可用?

4

1 回答 1

1

They say, DeleteIPAddress deletes address previously added by AddIPAddress :-) I had do some research by the interest and...

There are undocumented SetAdapterIPAddress function.

DWORD SetAdapterIpAddress( char *pszAdapGUID,
                       DWORD dwDHCP,
                       DWORD dwIP,
                       DWORD dwMask,
                       DWORD dwGateway );

It may work or not :-) Also there are WMI interface. See for Win32_NetworkAdapterConfiguration class. And there are a records in the windows registry for NIC IP address...

I think, the complexity are presence in Windows many "internal" configuration agents (DHCP, others..) and they all are the internal part of the OS and then as they manipulate an ip address, the other manipulations out of the control may break working of them. Also there are possibility to assign a static ip address in the Registry and it also should not be broken. And then you really have DeleteIPAddress which you previously add... I think, it is intended for your own dynamic config agents...

If you are not satisfied by work of other agents you should turn them off (instead of manipulating ip address out of the control)... Or control them by some specific API... Or set static IP...

于 2014-02-18T01:04:34.577 回答