Partager via


CreateUnicastIpAddressEntry (Compact 2013)

3/26/2014

This function adds a new unicast IP address entry on the local device.

Syntax

NETIOAPI_API CreateUnicastIpAddressEntry(
  __in  const MIB_UNICASTIPADDRESS_ROW* Row
);

Parameters

Return Value

Error Code

Meaning

NO_ERROR

The function succeeds.

ERROR_ACCESS_DENIED

Access is denied. This error is returned under several conditions that include the following: the user lacks the required administrative permissions on the local device or the application is not running in an improved shell as the built-in Administrator (RunAs administrator).

ERROR_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 Address member of the MIB_UNICASTIPADDRESS_ROW pointed to by the Row parameter was not set to a valid unicast IPv4 or IPv6 address, or both the InterfaceLuid and InterfaceIndex members of the MIB_UNICASTIPADDRESS_ROW pointed to by the Row parameter were unspecified.

This error is also returned for other errors in the values set for members in the MIB_UNICASTIPADDRESS_ROW structure. These errors include the following: if the ValidLifetime member is less than than the PreferredLifetime member, if the PrefixOrigin member is set to IpPrefixOriginUnchanged and the SuffixOrigin is the not set to IpSuffixOriginUnchanged, if the PrefixOrigin member is not set to IpPrefixOriginUnchanged and the SuffixOrigin is set to IpSuffixOriginUnchanged, if the PrefixOrigin member is not set to a value from the NL_PREFIX_ORIGIN enumeration, if the SuffixOrigin member is not set to a value from the NL_SUFFIX_ORIGIN enumeration, or if the OnLinkPrefixLength member is set to a value greater than the IP address length, in bits (32 for a unicast IPv4 address or 128 for a unicast IPv6 address).

ERROR_NOT_FOUND

The specified interface could not be found. This error is returned if the network interface specified by the InterfaceLuid or InterfaceIndex member of the MIB_UNICASTIPADDRESS_ROW pointed to by the Row parameter could not be found.

ERROR_NOT_SUPPORTED

The request is not supported. This error is returned if no IPv4 stack is located on the local device and an IPv4 address was specified in the Address member of the MIB_UNICASTIPADDRESS_ROW pointed to by the Row parameter. This error is also returned if no IPv6 stack is located on the local device and an IPv6 address was specified in the Address member.

ERROR_OBJECT_ALREADY_EXISTS

The object already exists. This error is returned if the Address member of the MIB_UNICASTIPADDRESS_ROW pointed to by the Row parameter is a duplicate of an existing unicast IP address on the interface specified by the InterfaceLuid or InterfaceIndex member of the MIB_UNICASTIPADDRESS_ROW.

Other

Use FormatMessage to obtain the message string for the returned error.

Remarks

The CreateUnicastIpAddressEntry function is used to add a new unicast IP address entry on a local device. The unicast IP address added by the CreateUnicastIpAddressEntry function is not persistent. The IP address exists only as long as the adapter object exists. Restarting the device destroys the IP address, as does manually resetting the network adapter. Also, certain PnP events may destroy the address.

To create an IPv4 address that persists, the EnableStatic method of the Win32_NetworkAdapterConfiguration Class in the Windows Management Instrumentation (WMI) controls may be used. The netsh command can also be used to create a persistent IPv4 or IPv6 address.

The InitializeUnicastIpAddressEntry function should be used to initialize the members of a MIB_UNICASTIPADDRESS_ROW structure entry with default values. An application can then change the members in the MIB_UNICASTIPADDRESS_ROW entry it wishes to modify, and then call the CreateUnicastIpAddressEntry function.

The Address member in the MIB_UNICASTIPADDRESS_ROW structure pointed to by the Row parameter must be initialized to a valid unicast IPv4 or IPv6 address and family. In addition, at least one of the following members in the MIB_UNICASTIPADDRESS_ROW structure pointed to the Row parameter must be initialized to the interface: the InterfaceLuid or InterfaceIndex. The fields are used in the order listed earlier. So if the InterfaceLuid is specified, this member is used to determine the interface on which to add the unicast IP address. If no value was set for the InterfaceLuid member (the values of this member was set to zero), the InterfaceIndex member is next used to determine the interface.

If the OnLinkPrefixLength member of the MIB_UNICASTIPADDRESS_ROW pointed to by the Row parameter is set to 255, CreateUnicastIpAddressEntry will add the new unicast IP address with the OnLinkPrefixLength member set equal to the length of the IP address. For a unicast IPv4 address, the OnLinkPrefixLength is set to 32 and the OnLinkPrefixLength is set to 128 for a unicast IPv6 address. If this would result in the incorrect subnet mask for an IPv4 address or the incorrect link prefix for an IPv6 address, the application should set this member to the correct value before it calls CreateUnicastIpAddressEntry.

If a unicast IP address is created that has the OnLinkPrefixLength member set incorrectly, the IP address may be changed by calling SetUnicastIpAddressEntry with the OnLinkPrefixLength member set to the correct value.

The DadState, ScopeId, and CreationTimeStamp members of the MIB_UNICASTIPADDRESS_ROW structure pointed to by the Row are ignored when the CreateUnicastIpAddressEntry function is called. These members are set by the network stack. The ScopeId member is automatically determined by the interface on which the address is added.

The CreateUnicastIpAddressEntry function will fail if the unicast IP address passed in the Address member of the MIB_UNICASTIPADDRESS_ROW pointed to by the Row parameter is a duplicate of an existing unicast IP address on the interface. Be aware that a loopback IP address can only be added to a loopback interface by using the CreateUnicastIpAddressEntry function.

The unicast IP address passed in the Address member of the MIB_UNICASTIPADDRESS_ROW pointed to by the Row parameter is not usable immediately. The IP address is usable after the duplicate address detection process has completed successfully. It can take several seconds for the duplicate address detection process to complete because IP packets must be sent and potential responses must be awaited. For IPv6, the duplicate address detection process typically takes about a second. For IPv4, the duplicate address detection process typically takes about three seconds.

If an application that has to know when an IP address is usable after a call to the CreateUnicastIpAddressEntry function, there are two methods that can be used. One method uses polling and the GetUnicastIpAddressEntry function. The second method calls one of the notification functions, NotifyAddrChange, NotifyIpInterfaceChange, or NotifyUnicastIpAddressChange to set up an asynchronous notification for when an address changes.

The following method describes how to use the GetUnicastIpAddressEntry and polling. After the call to the CreateUnicastIpAddressEntry function returns successfully, pause for one to three seconds (depending on whether an IPv6 or IPv4 address is being created) to allow time for the successful completion of the duplication address detection process. Then call the GetUnicastIpAddressEntry function to retrieve the updated MIB_UNICASTIPADDRESS_ROW structure and examine the value of the DadState member. If the value of the DadState member is set to IpDadStatePreferred, the IP address is now usable. If the value of the DadState member is set to IpDadStateTentative, duplicate address detection has not yet completed. In this case, call the GetUnicastIpAddressEntry function again every half second while the DadState member is still set to IpDadStateTentative. If the value of the DadState member returns with some value other than IpDadStatePreferred or IpDadStateTentative, duplicate address detection has failed and the IP address is not usable.

The following method describes how to use an appropriate notification function. After the call to the CreateUnicastIpAddressEntry function returns successfully, call the NotifyUnicastIpAddressChange function to register to be notified of changes to either IPv6 or IPv4 unicast IP addresses, depending on the type of IP address being created. When a notification is received for the IP address being created, call the GetUnicastIpAddressEntry function to retrieve the DadState member. If the value of the DadState member is set to IpDadStatePreferred, the IP address is now usable. If the value of the DadState member is set to IpDadStateTentative, duplicate address detection has not yet completed and the application must wait for future notifications. If the value of the DadState member returns with some value other than IpDadStatePreferred or IpDadStateTentative, duplicate address detection has failed and the IP address is not usable.

If during the duplicate address detection process the media is disconnected and then reconnected, the duplicate address detection process is restarted. The time to complete the process can increase beyond the typical 1 second value for IPv6 or 3 second value for IPv4.

The CreateUnicastIpAddressEntry function can only be called by a user logged on as a member of the Administrators group. If CreateUnicastIpAddressEntry is called by a user who is not a member of the Administrators group, the function call will fail and ERROR_ACCESS_DENIED is returned.

See Also

Reference

IP Helper Functions
DeleteUnicastIpAddressEntry
GetUnicastIpAddressEntry
GetUnicastIpAddressTable
InitializeUnicastIpAddressEntry
MIB_UNICASTIPADDRESS_ROW
MIB_UNICASTIPADDRESS_TABLE
NotifyAddrChange
NotifyIpInterfaceChange
NotifyUnicastIpAddressChange
SetUnicastIpAddressEntry

Other Resources

IP Helper