EDN Admin
Well-known member
<span><span>I have tried both C++ and VB script for <span style="color:#333333; font-family:Consolas,Courier New,Courier,monospace; line-height:16px; white-spacere SetIPConnectionMetric with the same result. The metric is not
getting set on windows 7. Although I can do it with netsh tool the <span style="color:#333333; font-family:Consolas,Courier New,Courier,monospace; line-height:16px; white-spacere SetIPConnectionMetric is not doing it.
Can some one please tell me how can I set the metric for an interface programatically?
Heres the scripts I have (the VB script I found here http://support.microsoft.com/kb/894564 http://support.microsoft.com/kb/894564
<pre>On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")
regValueDataMetric = "35"
Set colItems = objWMIService.ExecQuery ("Select * From Win32_NetworkAdapter Where NetConnectionID = Wireless Network Connection")
For Each objItem in colItems
strMACAddress = objItem.MACAddress
Wscript.Echo "MACAddress: " & strMACAddress
Next
Set colNetCard = objWMIService.ExecQuery ("Select * From Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each objNetCard in colNetCard
If objNetCard.MACAddress = strMACAddress Then
For Each strIPAddress in objNetCard.IPAddress
Wscript.Echo "Description: " & objNetCard.Description
Wscript.Echo "IP Address: " & strIPAddress
Wscript.Echo "IPConnectionMetric: " & objNetCard.IPConnectionMetric
Wscript.Echo "Index: " & objNetCard.Index
objNetCard.SetIPConnectionMetric(regValueDataMetric)
Next
End If
Next
[/code]
<br/>
The C++ code is a bit more complicated:
<pre>#include "stdafx.h"
#define _WIN32_DCOM
#include "windows.h"
#include <vector>
#include <iostream>
#include <sstream>
#include <stdlib.h>
using namespace std;
#include <wbemidl.h>
# pragma comment(lib, "wbemuuid.lib")
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr;
hr = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hr))
{
cout << "Failed to initialize COM library. Error code = 0x" << hex << hr << endl;
return hr;
}
hr = CoInitializeSecurity(
NULL, // Security descriptor
-1, // COM negotiates authentication service
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication level for proxies
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation level for proxies
NULL, // Authentication info
EOAC_NONE, // Additional capabilities of the client or server
NULL); // Reserved
if (FAILED(hr))
{
cout << "Failed to initialize security. Error code = 0x"
<< hex << hr << endl;
CoUninitialize();
return hr; // Program has failed.
}
// WMI connection
IWbemLocator *pLoc = 0;
hr = CoCreateInstance(CLSID_WbemLocator, 0,
CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hr))
{
cout << "Failed to create IWbemLocator object. Err code = 0x"
<< hex << hr << endl;
CoUninitialize();
return hr; // Program has failed.
}
IWbemServices *pSvc = 0;
// Connect to the rootdefault namespace with the current user.
hr = pLoc->ConnectServer(
BSTR(L"root\cimv2"),
NULL, NULL, 0, NULL, 0, 0, &pSvc);
if (FAILED(hr))
{
cout << "Could not connect. Error code = 0x"
<< hex << hr << endl;
pLoc->Release();
CoUninitialize();
return hr; // Program has failed.
}
cout << "Connected to WMI" << endl;
// setting security for WMI
// Set the proxy so that impersonation of the client occurs.
hr = CoSetProxyBlanket(pSvc,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE
);
if (FAILED(hr))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hr << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return hr; // Program has failed.
}
// retreive all the interfaces.
BSTR Language = SysAllocString(L"WQL");
BSTR Query = SysAllocString(L"Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True");
IEnumWbemClassObject *pEnum = 0;
hr = pSvc->ExecQuery(Language, Query, WBEM_FLAG_FORWARD_ONLY, 0, &pEnum);
SysFreeString(Query);
SysFreeString(Language);
if (SUCCEEDED(hr))
{
ULONG uTotal = 0;
// Retrieve the objects in the result set.
for (;
{
IWbemClassObject *pObj = 0;
ULONG uReturned = 0;
// 0 - time out, 1 - One object
hr = pEnum->Next(0, 1, &pObj, &uReturned);
if (FAILED(hr))
{
cout << "Error Get(IPConnectionMetric); hr= 0x" << hr << endl;
break;
}
uTotal += uReturned;
if (uReturned == 0)
break;
// read IPConnectionMetric
VARIANT vtProp;
VariantInit(&vtProp);
hr = pObj->Get(L"IPConnectionMetric", 0, &vtProp, 0, 0);
if (FAILED(hr))
{
cout << "Error Get(IPConnectionMetric); hr= 0x" << hr << endl;
break;
}
unsigned int IPConnectionMetric = vtProp.lVal;
cout << "IPConnectionMetric: " << IPConnectionMetric << endl;
VariantClear(&vtProp);
// read Index
VARIANT vtPropIndex;
VariantInit(&vtPropIndex);
hr = pObj->Get(L"Index", 0, &vtPropIndex, 0, 0);
if (FAILED(hr))
{
cout << "Error Get(Index); hr= 0x" << hr << endl;
break;
}
unsigned int index = vtPropIndex.lVal;
cout << "index: " << index << endl;
VariantClear(&vtPropIndex);
// Execute SetIPConnectionMetric
IWbemClassObject * pClass = NULL;
IWbemClassObject * pOutInst = NULL;
IWbemClassObject * pInClass = NULL;
IWbemClassObject * pInInst = NULL;
// Get the class object
hr = pSvc->GetObject(L"Win32_NetWorkAdapterConfiguration", 0, NULL, &pClass, NULL);
if (hr != WBEM_S_NO_ERROR)
{
cout << "Error getting object Win32_NetWorkAdapterConfiguration; hr=" << hr << endl;
break;
}
// Get the input argument and set the property
hr = pClass->GetMethod(L"SetIPConnectionMetric", 0, &pInClass, NULL);
if (hr != WBEM_S_NO_ERROR)
{
cout << "Error getting method SetIPConnectionMetric; hr=" << hr << endl;
break;
}
hr = pInClass->SpawnInstance(0, &pInInst);
if (hr != WBEM_S_NO_ERROR)
{
cout << "Error spawning instance; hr=" << hr << endl;
break;
}
// Set up the query string with the adapter index we obtained above.
wchar_t dest[10];
memset((char*)dest, 0, 20);
_itow(index, dest, 10);
wstring objPath = L"Win32_NetWorkAdapterConfiguration.Index=" + wstring(dest);
objPath = objPath + L"";
BSTR bstrObjPath = SysAllocString(objPath.c_str());
VARIANT varArg1;
VariantInit(&varArg1);
V_VT(&varArg1) = VT_BSTR;
V_BSTR(&varArg1) = SysAllocString(L"100");
hr = pInInst->Put(L"IPConnectionMetric", 0, &varArg1, CIM_UINT32);
if (FAILED(hr))
{
cout << "Error Put(IPConnectionMetric); hr=" << hr << endl;
break;
}
hr = pSvc->ExecMethod(bstrObjPath, L"SetIPConnectionMetric", 0, NULL, pInInst, &pOutInst, NULL);
if (hr != WBEM_S_NO_ERROR)
{
cout << "Error executing SetIPConnectionMetric! hr=" << hr << endl;
break;
}
// Get the EnableStatic method return value
VARIANT ret_value;
hr = pOutInst->Get(L"ReturnValue", 0, &ret_value, 0, 0);
if (FAILED(hr))
{
cout << "Error Get(ReturnValue); hr= 0x" << hr << endl;
break;
}
long ret = V_I4(&ret_value);
if(ret != 0)
{
hr = E_FAIL;
}
// Free up BSTR and VARIANT objects
VariantClear(&ret_value);
VariantClear(&varArg1);
pObj->Release(); // Release objects not owned.
}
cout << "Number of objects returned is " << uTotal << endl;
}
else
{
cout << "Error querying WMI for network adapters. hr=" << hr << endl;
}
pSvc->Release();
pLoc->Release();
CoUninitialize();
cout << "Done." << endl;
return 0; // Program successfully completed.
}
[/code]
<br/>
View the full article
getting set on windows 7. Although I can do it with netsh tool the <span style="color:#333333; font-family:Consolas,Courier New,Courier,monospace; line-height:16px; white-spacere SetIPConnectionMetric is not doing it.
Can some one please tell me how can I set the metric for an interface programatically?
Heres the scripts I have (the VB script I found here http://support.microsoft.com/kb/894564 http://support.microsoft.com/kb/894564
<pre>On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")
regValueDataMetric = "35"
Set colItems = objWMIService.ExecQuery ("Select * From Win32_NetworkAdapter Where NetConnectionID = Wireless Network Connection")
For Each objItem in colItems
strMACAddress = objItem.MACAddress
Wscript.Echo "MACAddress: " & strMACAddress
Next
Set colNetCard = objWMIService.ExecQuery ("Select * From Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each objNetCard in colNetCard
If objNetCard.MACAddress = strMACAddress Then
For Each strIPAddress in objNetCard.IPAddress
Wscript.Echo "Description: " & objNetCard.Description
Wscript.Echo "IP Address: " & strIPAddress
Wscript.Echo "IPConnectionMetric: " & objNetCard.IPConnectionMetric
Wscript.Echo "Index: " & objNetCard.Index
objNetCard.SetIPConnectionMetric(regValueDataMetric)
Next
End If
Next
[/code]
<br/>
The C++ code is a bit more complicated:
<pre>#include "stdafx.h"
#define _WIN32_DCOM
#include "windows.h"
#include <vector>
#include <iostream>
#include <sstream>
#include <stdlib.h>
using namespace std;
#include <wbemidl.h>
# pragma comment(lib, "wbemuuid.lib")
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr;
hr = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hr))
{
cout << "Failed to initialize COM library. Error code = 0x" << hex << hr << endl;
return hr;
}
hr = CoInitializeSecurity(
NULL, // Security descriptor
-1, // COM negotiates authentication service
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication level for proxies
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation level for proxies
NULL, // Authentication info
EOAC_NONE, // Additional capabilities of the client or server
NULL); // Reserved
if (FAILED(hr))
{
cout << "Failed to initialize security. Error code = 0x"
<< hex << hr << endl;
CoUninitialize();
return hr; // Program has failed.
}
// WMI connection
IWbemLocator *pLoc = 0;
hr = CoCreateInstance(CLSID_WbemLocator, 0,
CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hr))
{
cout << "Failed to create IWbemLocator object. Err code = 0x"
<< hex << hr << endl;
CoUninitialize();
return hr; // Program has failed.
}
IWbemServices *pSvc = 0;
// Connect to the rootdefault namespace with the current user.
hr = pLoc->ConnectServer(
BSTR(L"root\cimv2"),
NULL, NULL, 0, NULL, 0, 0, &pSvc);
if (FAILED(hr))
{
cout << "Could not connect. Error code = 0x"
<< hex << hr << endl;
pLoc->Release();
CoUninitialize();
return hr; // Program has failed.
}
cout << "Connected to WMI" << endl;
// setting security for WMI
// Set the proxy so that impersonation of the client occurs.
hr = CoSetProxyBlanket(pSvc,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE
);
if (FAILED(hr))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hr << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return hr; // Program has failed.
}
// retreive all the interfaces.
BSTR Language = SysAllocString(L"WQL");
BSTR Query = SysAllocString(L"Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True");
IEnumWbemClassObject *pEnum = 0;
hr = pSvc->ExecQuery(Language, Query, WBEM_FLAG_FORWARD_ONLY, 0, &pEnum);
SysFreeString(Query);
SysFreeString(Language);
if (SUCCEEDED(hr))
{
ULONG uTotal = 0;
// Retrieve the objects in the result set.
for (;
{
IWbemClassObject *pObj = 0;
ULONG uReturned = 0;
// 0 - time out, 1 - One object
hr = pEnum->Next(0, 1, &pObj, &uReturned);
if (FAILED(hr))
{
cout << "Error Get(IPConnectionMetric); hr= 0x" << hr << endl;
break;
}
uTotal += uReturned;
if (uReturned == 0)
break;
// read IPConnectionMetric
VARIANT vtProp;
VariantInit(&vtProp);
hr = pObj->Get(L"IPConnectionMetric", 0, &vtProp, 0, 0);
if (FAILED(hr))
{
cout << "Error Get(IPConnectionMetric); hr= 0x" << hr << endl;
break;
}
unsigned int IPConnectionMetric = vtProp.lVal;
cout << "IPConnectionMetric: " << IPConnectionMetric << endl;
VariantClear(&vtProp);
// read Index
VARIANT vtPropIndex;
VariantInit(&vtPropIndex);
hr = pObj->Get(L"Index", 0, &vtPropIndex, 0, 0);
if (FAILED(hr))
{
cout << "Error Get(Index); hr= 0x" << hr << endl;
break;
}
unsigned int index = vtPropIndex.lVal;
cout << "index: " << index << endl;
VariantClear(&vtPropIndex);
// Execute SetIPConnectionMetric
IWbemClassObject * pClass = NULL;
IWbemClassObject * pOutInst = NULL;
IWbemClassObject * pInClass = NULL;
IWbemClassObject * pInInst = NULL;
// Get the class object
hr = pSvc->GetObject(L"Win32_NetWorkAdapterConfiguration", 0, NULL, &pClass, NULL);
if (hr != WBEM_S_NO_ERROR)
{
cout << "Error getting object Win32_NetWorkAdapterConfiguration; hr=" << hr << endl;
break;
}
// Get the input argument and set the property
hr = pClass->GetMethod(L"SetIPConnectionMetric", 0, &pInClass, NULL);
if (hr != WBEM_S_NO_ERROR)
{
cout << "Error getting method SetIPConnectionMetric; hr=" << hr << endl;
break;
}
hr = pInClass->SpawnInstance(0, &pInInst);
if (hr != WBEM_S_NO_ERROR)
{
cout << "Error spawning instance; hr=" << hr << endl;
break;
}
// Set up the query string with the adapter index we obtained above.
wchar_t dest[10];
memset((char*)dest, 0, 20);
_itow(index, dest, 10);
wstring objPath = L"Win32_NetWorkAdapterConfiguration.Index=" + wstring(dest);
objPath = objPath + L"";
BSTR bstrObjPath = SysAllocString(objPath.c_str());
VARIANT varArg1;
VariantInit(&varArg1);
V_VT(&varArg1) = VT_BSTR;
V_BSTR(&varArg1) = SysAllocString(L"100");
hr = pInInst->Put(L"IPConnectionMetric", 0, &varArg1, CIM_UINT32);
if (FAILED(hr))
{
cout << "Error Put(IPConnectionMetric); hr=" << hr << endl;
break;
}
hr = pSvc->ExecMethod(bstrObjPath, L"SetIPConnectionMetric", 0, NULL, pInInst, &pOutInst, NULL);
if (hr != WBEM_S_NO_ERROR)
{
cout << "Error executing SetIPConnectionMetric! hr=" << hr << endl;
break;
}
// Get the EnableStatic method return value
VARIANT ret_value;
hr = pOutInst->Get(L"ReturnValue", 0, &ret_value, 0, 0);
if (FAILED(hr))
{
cout << "Error Get(ReturnValue); hr= 0x" << hr << endl;
break;
}
long ret = V_I4(&ret_value);
if(ret != 0)
{
hr = E_FAIL;
}
// Free up BSTR and VARIANT objects
VariantClear(&ret_value);
VariantClear(&varArg1);
pObj->Release(); // Release objects not owned.
}
cout << "Number of objects returned is " << uTotal << endl;
}
else
{
cout << "Error querying WMI for network adapters. hr=" << hr << endl;
}
pSvc->Release();
pLoc->Release();
CoUninitialize();
cout << "Done." << endl;
return 0; // Program successfully completed.
}
[/code]
<br/>
View the full article