G'Day All,
I have been trying to get the EnableStatic function to work with Multiple IP's/Netmasks and am having trouble. At NO point do I get any errors to any of the function calls, but none of my passed IP/Netmask pairs are actually being programmed. Hoping someone on here might have an idea as to what I am possibly doing wrong or missing.
The code to do the C++ EnableStatic was based off of this article in the posts section from "Brian Clow For God's Sake 10:38 30 May '06":
Making WMI Queries In C++[
^]
I have changed the function to instead take a list of IP/Netmasks and build the EnableStatic parameters from those.
Here is the code (there is still some cleanup to do in the error returns for the function calls, but I have been first trying to get it to work):
typedef struct IP_INFO
{
char ip[40];
char netmask[40];
} IP_INFO, *PIP_INFO;
bool AddIPAddressToAdapterPersistant(PIP_INFO &ipInfo, INT count, INT fIndex)
{
HRESULT hr;
IWbemLocator *pLocator = NULL;
IWbemServices *pNamespace = NULL;
BSTR path = SysAllocString(L"ROOT\\CIMV2");
hr = CoInitialize(0);
if(FAILED(hr))
{
return (false);
}
hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
if(FAILED(hr))
{
CoUninitialize();
return(false);
}
hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLocator);
if (FAILED(hr))
{
CoUninitialize();
return (false);
}
hr = pLocator->ConnectServer(path, NULL, NULL, NULL, 0, NULL, NULL, &pNamespace);
if(FAILED(hr))
{
pLocator->Release();
CoUninitialize();
return(false);
}
IWbemClassObject *pClass = NULL;
BSTR ClassPath = SysAllocString(L"Win32_NetworkAdapterConfiguration");
hr = pNamespace->GetObject(ClassPath, 0, NULL, &pClass, NULL);
SysFreeString(ClassPath);
if( WBEM_S_NO_ERROR == hr )
{
BSTR MethodName_ES = SysAllocString(L"EnableStatic");
IWbemClassObject *pInClass_ES = NULL;
if(WBEM_S_NO_ERROR == pClass->GetMethod(MethodName_ES, 0, &pInClass_ES, NULL))
{
IWbemClassObject *pInInst_ES = NULL;
if(WBEM_S_NO_ERROR == pInClass_ES->SpawnInstance(0, &pInInst_ES))
{
wchar_t tmp_ip[_countof(ipInfo->ip)];
SAFEARRAY *ip_list = SafeArrayCreateVector(VT_BSTR, 0, count);
long idx[] = {0};
for(int i = 0; i < count; i++)
{
mbstowcs(tmp_ip, ipInfo[i].ip, _countof(ipInfo[i].ip));
BSTR ip = SysAllocString(tmp_ip);
idx[0] = i;
if(FAILED(SafeArrayPutElement(ip_list, idx, ip)))
{
return (false);
} SysFreeString(ip);
}
wchar_t tmp_netmask[_countof(ipInfo->netmask)];
SAFEARRAY *netmask_list = SafeArrayCreateVector(VT_BSTR, 0, count);
for(int i = 0; i < count; i++)
{
mbstowcs(tmp_netmask, ipInfo[i].netmask, _countof(ipInfo[i].netmask));
BSTR netmask = SysAllocString(tmp_netmask);
idx[0] = i;
if(FAILED(SafeArrayPutElement(netmask_list, idx, netmask)))
{
return (false);
} SysFreeString(netmask);
}
VARIANT arg1_ES;
VariantInit(&arg1_ES);
arg1_ES.vt = VT_ARRAY|VT_BSTR;
arg1_ES.parray = ip_list;
VARIANT arg2_ES;
VariantInit(&arg2_ES);
arg2_ES.vt = VT_ARRAY|VT_BSTR;
arg2_ES.parray = netmask_list;
if((WBEM_S_NO_ERROR == pInInst_ES->Put(L"IPAddress", 0, &arg1_ES, 0)) &&
(WBEM_S_NO_ERROR == pInInst_ES->Put(L"SubNetMask", 0, &arg2_ES, 0)))
{
char indexString[10];
itoa(fIndex, indexString, 10);
char instanceString[100];
wchar_t w_instanceString[100];
strcpy(instanceString, "Win32_NetworkAdapterConfiguration.Index='");
strcat(instanceString, indexString);
strcat(instanceString, "'");
mbstowcs(w_instanceString, instanceString, 100);
BSTR InstancePath = SysAllocString(w_instanceString);
IWbemClassObject * pOutInst = NULL;
hr = pNamespace->ExecMethod(InstancePath, MethodName_ES, 0, NULL, pInInst_ES, &pOutInst, NULL);
if(FAILED(hr))
{
return(false);
} SysFreeString(InstancePath);
}
VariantClear(&arg1_ES);
VariantClear(&arg2_ES);
SafeArrayDestroy(ip_list); ip_list = NULL;
SafeArrayDestroy(netmask_list); netmask_list = NULL;
}
else
{
}
if(pInInst_ES)
{
pInInst_ES->Release();
pInInst_ES = NULL;
} }
else
{
}
if(pInClass_ES)
{
pInClass_ES->Release();
pInClass_ES = NULL;
} SysFreeString(MethodName_ES);
}
else
{
}
if (pNamespace)
{
pNamespace->Release();
pNamespace = NULL;
}
if (pLocator)
{
pLocator->Release();
pLocator = NULL;
} if(pClass)
{
pClass->Release();
pClass = NULL;
} CoUninitialize();
return(true);
}
Thanks!