From: Chitra Nanda on
Hi all,

I am trying to update the IPv4 address of an Adapter on a windows
server using AddIPAddress API. I can see that whatever IP address or
the subnet mask I try to work with I always get the error "Object
Already Exists" error as the return value of the same. I want to use
AddIPAddress to update the IPv4 address. Please let me know if anyone
has any problem in using the same. If anyone can provide any helpful
pointers on this, it will be highly helpful. If anyone has any other
simpler alternative also please suggest.

Thanks in advance for your time.

I am pasting the code which I am using below.

#ifdef WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <time.h>

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))

#endif



int _tmain(int argc, _TCHAR* argv[])
{
/* variables used for AddIpAddress */
UINT iaIPAddress;
UINT imIPMask;
ULONG NTEContext=0;
ULONG NTEInstance=0;

/* variables used for GetAdapterInfo */
ULONG ulOutBufLen;
DWORD dwRetVal, interfaceIndex, interfacePos;
IP_ADAPTER_INFO *pAdapterInfo;
IP_ADAPTER_INFO *pAdapter;


char strInterface[256]={"0000C993300C0000"};

//ELXLOG( ( ELX_DBG, "SetIPv4Address -- Adapter info for interface %s
with the ip adddress %s\n", strInterface.c_str(), ipAddress.c_str()));


pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(sizeof (IP_ADAPTER_INFO));
if (pAdapterInfo == NULL) {
//ELXLOG( ( ELX_DBG,"SetIPv4Address -- Error allocating memory
needed to call GetAdapterInfo\n"));
return ( -1 );
}

ulOutBufLen = sizeof (IP_ADAPTER_INFO);

if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) ==
ERROR_BUFFER_OVERFLOW) {
FREE(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(ulOutBufLen);
if (pAdapterInfo == NULL) {
//ELXLOG( ( ELX_DBG,"SetIPv4Address -- Error allocating
memory needed to call GetAdapterInfo\n"));
return ( -1 );
}
}

if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) !=
NO_ERROR) {
//ELXLOG( ( ELX_DBG,"SetIPv4Address -- GetAdaptersInfo failed
with error %d\n", dwRetVal));
if (pAdapterInfo)
FREE(pAdapterInfo);
return ( -1 );
}

pAdapter = pAdapterInfo;
bool interfaceFound = false;
char interfaceAddress[256], sNewMacAddress[256];

while (pAdapter)
{

memset(interfaceAddress,0,256);
int i;
for ( i = 0; i < pAdapter->AddressLength; i++)
{
if (i == (pAdapter->AddressLength - 1))
{
sprintf(sNewMacAddress, "%02x", (int) pAdapter-
>Address[i]);
strcat(interfaceAddress,sNewMacAddress);
}
else
{
sprintf(sNewMacAddress, "%02x", (int)pAdapter->Address[i]);
strcat(interfaceAddress,sNewMacAddress);
}
}//End of for

//Compare the passed interface name with the list of interfaces
available
if(strnicmp(interfaceAddress,
strInterface,strlen(interfaceAddress)) == 0)
{
//ELXLOG( ( ELX_DBG, "SetIPv4Address -- Matching interface found for
the interface %s\n", strInterface.c_str()));
if(pAdapter->IpAddressList.IpAddress.String)
{
interfaceFound = true;
interfaceIndex = pAdapter->Index;
interfacePos = i;
imIPMask = inet_addr(pAdapter->IpAddressList.IpMask.String);
//ELXLOG( ( ELX_DBG, "SetIPv4Address -- Successfully found the
matching SubnetMask %s ", pAdapter->IpAddressList.IpMask.String));
break; //out of while
}
}
pAdapter = pAdapter->Next;
}//End of While loop

LPVOID lpMsgBuf;

if(interfaceFound)
{
//iaIPAddress = inet_addr(ipAddress.c_str());
//ELXLOG( ( ELX_DBG, "SetIPv4Address --- IP address to be added %d",
iaIPAddress));
//ELXLOG( ( ELX_DBG, "SetIPv4Address ---IP Mask to be added %d",
imIPMask));
iaIPAddress =inet_addr("10.192.193.194");
imIPMask=inet_addr("255.255.240.0");

dwRetVal = AddIPAddress(iaIPAddress, imIPMask, interfaceIndex,
&NTEContext, &NTEInstance);
switch (dwRetVal) {
case NO_ERROR:
printf("AddIPAddress successful");
break;
case ERROR_DEV_NOT_EXIST:
printf("The adapter specified by the IfIndex parameter
does not exist");
break;
case ERROR_DUP_DOMAINNAME:
{
if ((dwRetVal = DeleteIPAddress(NTEContext)) == NO_ERROR) {
printf("\tIPv4 address %s was successfully deleted.\n");
break;
}
else
{
printf("\tDeleteIPAddress failed with error: %d\n", dwRetVal);
break;
}

printf("The IPv4 address to add that is specified in the Address
parameter already exists");
break;
}
case ERROR_GEN_FAILURE:
printf("A general failure");
case ERROR_INVALID_HANDLE:
printf("The user attempting to make the function call is not an
administrator");
break;
case ERROR_INVALID_PARAMETER:
printf("One or more of the parameters is invalid");
break;
case ERROR_NOT_SUPPORTED:
printf("The function call is not supported on the version of
Windows on which it was run");
break;
case ERROR_OBJECT_ALREADY_EXISTS:
printf("SetIPv4Address ---IPV4Address and IPV4 Subnet Mask
has been updated already - No updation was done" );
return true;
default:
{
if(dwRetVal == 5010)
{

printf("AddIPAddress failed with error: %d\n", dwRetVal);

if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
dwRetVal, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default
language
(LPTSTR) & lpMsgBuf, 0, NULL))
{
printf("\tError: %s", lpMsgBuf);

if ((dwRetVal = DeleteIPAddress(NTEContext)) == NO_ERROR)
printf("\tIPv4 address %s was successfully deleted.\n");
else
printf("\tDeleteIPAddress failed with error: %d\n", dwRetVal);
break;
}
}
}

}//End of Switch
}//ENd of If

return 0;
}//End of main


Regards,
Chitra