简体   繁体   中英

Why does DeleteIPAddress fails when deleting the adapter's default IP?

I'm trying to write a program that will switch the selected adapter's IP on command.

For that, i'm using the GetAdaptersInfo() function to get the adapter's Index and it's IP context, and then using that to delete the current IP (through DeleteIPAddress(context)).

After that, the new IP would be inserted through AddIPAddress().

The problem is, the call to the DeleteIPAddress function fails when trying to delete the "default" IP (the one not set by AddIPAddress()) and calling AddIPAddress before deleting the old IP just leaves the adapter with two different simultaneous IPs.

The DeleteIPAddress function fails with error 31 (A device attached to the system is not functioning).

Is there another way to sucessfully change the IP Address on an adapter? Is DeleteIPAddress actually usable without getting context from AddIPAddress?

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM