Yevhen
Yevhen

Reputation: 1975

DeleteIPAddress and AddIpAddress Problem

i tried to write program adding ip address using this function

 
DWORD AddIPAddress(
  __in   IPAddr Address,
  __in   IPMask IpMask,
  __in   DWORD IfIndex,
  __out  PULONG NTEContext,
  __out  PULONG NTEInstance
);

I have added it but how can I delete it. DeleteIPAddress takes NTEContext as Parameter how can i get it in MSDN they write it is returned by AddIPAddress function but when i call it for the second time with the same ip address it returns an error 2. What to do?

I can view add ip addresses using Ipconfig command in cmd, may be there is some other method to view or delete it manually

Upvotes: 1

Views: 1855

Answers (2)

Gabor Szelei
Gabor Szelei

Reputation: 306

Use GetAdaptersInfo in order to gain IP_ADAPTER_INFO. IP_ADDR_STRING has a suitable context for DeleteIPAddress in IP_ADAPTER_INFO.

Upvotes: 1

Steve Townsend
Steve Townsend

Reputation: 54178

You can't do this. From MSDN:

To use DeleteIPAddress, AddIPAddress must first be called to get the handle NTEContext. The previous procedure assumes that AddIPAddress has already been called somewhere in the code, and NTEContext has been saved and remains uncorrupted.

The lifetime of the added address is as shown here:

The AddIPAddress function is used to add a new IPv4 address entry on a local computer. The IPv4 address added by the AddIPAddress function is not persistent. The IPv4 address exists only as long as the adapter object exists. Restarting the computer destroys the IPv4 address, as does manually resetting the network interface card (NIC). Also, certain PnP events may destroy the address.

Upvotes: 3

Related Questions