?
Martin Richter schrieb:
Wo? Welche Klasse? Welcher Kontext? Code?
Hier ist mal der Code: hab diesen grad auf einem weiteren Xp-Rechner ausprobiert: hier kommt jetzt die Fehlermeldung bei EnableStatic(): IP not enabled....
HRESULT hr = CoInitialize( NULL); // COINIT_MULTITHREADED );
if ( FAILED( hr ) )
{
TRACE(_T("COM initialization failed\n"));
return FALSE;
}
// setup process-wide security context
hr = CoInitializeSecurity( NULL, // we're not a server
-1, // we're not a server
NULL, // dito
NULL, // reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // let DCOM decide
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
NULL );
if ( FAILED( hr ) )
{
TRACE(_T("Security initialization failed\n"));
return FALSE;
}
// connect to WMI
CComPtr< IWbemLocator > locator;
hr = CoCreateInstance( CLSID_WbemAdministrativeLocator, NULL,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, reinterpret_cast< void** >( &locator ) );
if ( FAILED( hr ) )
{
std::cerr << "Instantiation of IWbemLocator failed" << std::endl;
return -1;
}
// connect to local service with current credentials
CComPtr< IWbemServices > pServices;
hr = locator->ConnectServer( L"root\\cimv2", NULL, NULL, NULL,
WBEM_FLAG_CONNECT_USE_MAX_WAIT,
NULL, NULL, &pServices );
// Grab class required to work on Win32_NetworkAdapterConfiguration
IWbemClassObject *pClass = NULL;
BSTR ClassPath = SysAllocString(L"Win32_NetworkAdapterConfiguration");
hr = pServices->GetObject(ClassPath, 0, NULL, &pClass, NULL);
SysFreeString(ClassPath);
if( WBEM_S_NO_ERROR == hr )
{
// Grab pointers to the input parameter class of the two methods we are going to call
BSTR MethodName_ES = SysAllocString(L"EnableStatic");
BSTR MethodName_SG = SysAllocString(L"SetGateways");
IWbemClassObject *pInClass_ES = NULL;
IWbemClassObject *pInClass_SG = NULL;
if( WBEM_S_NO_ERROR==pClass->GetMethod(MethodName_ES, 0, &pInClass_ES, NULL) &&
WBEM_S_NO_ERROR==pClass->GetMethod(MethodName_SG, 0, &pInClass_SG, NULL) )
{
// Spawn instances of the input parameter classes, so that we can stuff our parameters in
IWbemClassObject *pInInst_ES = NULL;
IWbemClassObject *pInInst_SG = NULL;
if( WBEM_S_NO_ERROR==pInClass_ES->SpawnInstance(0, &pInInst_ES) &&
WBEM_S_NO_ERROR==pInClass_SG->SpawnInstance(0, &pInInst_SG) )
{
// Convert from multibyte strings to wide character arrays
wchar_t tmp_ip[40];
wchar_t tmp_netmask[40];
wchar_t tmp_gateway[40];
#define DESIRED_GW_METRIC 0
unsigned short metric = DESIRED_GW_METRIC;
// Insert into safe arrays, allocating memory as we do so (destroying the safe array will destroy the allocated memory)
long index[]={0};
BSTR ip = SysAllocString(L"2.0.0.2");
BSTR netmask = SysAllocString(L"255.0.0.0");
BSTR gateway = SysAllocString(L"2.0.0.3");
SAFEARRAY *ip_list = SafeArrayCreateVector(VT_BSTR, 0, 1);
SafeArrayPutElement(ip_list, index, ip);
SAFEARRAY *mask_list = SafeArrayCreateVector(VT_BSTR, 0, 1);
SafeArrayPutElement(mask_list, index, netmask);
SAFEARRAY *gateway_list = SafeArrayCreateVector(VT_BSTR, 0, 1);
SafeArrayPutElement(gateway_list, index, gateway);
SAFEARRAY *metric_list = SafeArrayCreateVector(VT_UI1, 0, 1);
SafeArrayPutElement(metric_list, index, &metric);
// Now wrap each safe array in a VARIANT so that it can be passed to COM function
VARIANT arg1_ES;
arg1_ES.vt = VT_ARRAY|VT_BSTR;
arg1_ES.parray = ip_list;
VARIANT arg2_ES;
arg2_ES.vt = VT_ARRAY|VT_BSTR;
arg2_ES.parray = mask_list;
VARIANT arg1_SG;
arg1_SG.vt = VT_ARRAY|VT_BSTR;
arg1_SG.parray = gateway_list;
VARIANT arg2_SG;
arg2_SG.vt = VT_ARRAY|VT_UI1;
arg2_SG.parray = metric_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) &&
WBEM_S_NO_ERROR==pInInst_SG->Put(L"DefaultIPGateway", 0, &arg1_SG, 0) &&
WBEM_S_NO_ERROR==pInInst_SG->Put(L"GatewayCostMetric", 0, &arg2_SG, 0) )
{
//
// (Step 4) - Call the methods
//
char instanceString[100];
wchar_t w_instanceString[100];
strcpy(instanceString, "Win32_NetworkAdapterConfiguration.Index=2");
mbstowcs(w_instanceString, instanceString, 100);
BSTR InstancePath = SysAllocString(w_instanceString);
IEnumWbemClassObject* pEnumerator = NULL;
hr = pServices->ExecQuery( L"WQL",
(L"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator );
IWbemClassObject *pclsObj3;
CString value ("IPEnabled");
ULONG uReturn = 0;
if(pEnumerator) {
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj3, &uReturn);
if(uReturn) {
_variant_t var_val;
hr = pclsObj3->Get(value, 0, &var_val, 0, 0);
_bstr_t str = var_val;
//ReturnValue = (char*)str;
VariantClear(&var_val);
}
else {
TRACE(_T("Error at pEnumerator->Next"));
return FALSE;
}
}
// Now call the methods
IWbemClassObject * pOutInst = NULL;
hr = pServices->ExecMethod(InstancePath, MethodName_ES, 0, NULL, pInInst_ES, &pOutInst, NULL);
if( WBEM_S_NO_ERROR==hr )
{
BSTR Text;
hr = pOutInst->GetObjectText(0, &Text);
Text = Text;
printf("SetAdapterConfig - Enabled static IP successfully.");
}
else
printf("SetAdapterConfig - Could not enable static IP - 0x%0x", hr);
IWbemClassObject * pOutInst2 = NULL;
hr = pServices->ExecMethod(InstancePath, MethodName_SG, 0, NULL, pInInst_SG, &pOutInst2, NULL);
if( WBEM_S_NO_ERROR==hr )
{
BSTR Text;
hr = pOutInst2->GetObjectText(0, &Text);
Text = Text;
printf("SetAdapterConfig - Set gateways successfully.");
}
else
printf("SetAdapterConfig - Could not set gateways - 0x%0x", hr);
SysFreeString(InstancePath);
}
// Clear the variants - does this actually get ride of safearrays?
VariantClear(&arg1_ES);
VariantClear(&arg2_ES);
VariantClear(&arg1_SG);
VariantClear(&arg2_SG);
// Destroy safe arrays, which destroys the objects stored inside them
SafeArrayDestroy(ip_list); ip_list=NULL;
SafeArrayDestroy(mask_list); mask_list = NULL;
SafeArrayDestroy(gateway_list); gateway_list = NULL;
// Destroy the BSTR pointers
SysFreeString(ip);
SysFreeString(netmask);
SysFreeString(gateway);
}
// Free up the instances that we spawned
if( pInInst_ES )
{
pInInst_ES->Release();
pInInst_ES = NULL;
}
if( pInInst_SG )
{
pInInst_SG->Release();
pInInst_SG = NULL;
}
}
// Free up methods input parameters class pointers
if( pInClass_ES )
{
pInClass_ES->Release();
pInClass_ES = NULL;
}
if( pInClass_SG )
{
pInClass_SG->Release();
pInClass_SG = NULL;
}
SysFreeString(MethodName_ES);
SysFreeString(MethodName_SG);
}
// Variable cleanup
if( pClass )
{
pClass->Release();
pClass = NULL;
}
CoUninitialize();
Gruß
Jakob