|
Any one know how to actually get string value for IP Address from Win32_NetworkAdapterConfiguration object. It is stored as an array. I guess I don't know how to actually retrieve the array from the variant object?
your help is greatly appreciated.
Diana.
|
|
|
|
|
It really depends on what is stored in the variant. You can check this by checking the vt of the variant. If it is VT_ARRAY|VT_VARIANT, then surely there is a SafeArray inside the variant. You will have to checkout methods of extracting the data from the SafeArray for this.
Regards,
There are two types of roads in our country. One
is under construction and the other is under repair.
|
|
|
|
|
Im really new in wmi programming using c++. Can you provide code snippets on how to check the vt of the variant and how to extract the data from the SafeArray. Thanks.
Expect the worst, and hope for the best.
|
|
|
|
|
hope that can help:
<br />
hres = pSvc->ExecQuery(<br />
bstr_t("WQL"), <br />
bstr_t("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'"),<br />
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, <br />
NULL,<br />
&pEnumerator);<br />
<br />
if (FAILED(hres))<br />
{<br />
pSvc->Release();<br />
pLoc->Release();<br />
CoUninitialize();<br />
return -1; <br />
}<br />
<br />
int nRez = 0;<br />
char sRezDescr[256] = {'\0'};<br />
char sRezModel[128] = {'\0'};<br />
<br />
szTmp = "";<br />
while (pEnumerator)<br />
{<br />
VARIANT vtProp;<br />
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, <br />
&pclsObj, &uReturn);<br />
<br />
if(0 == uReturn) break;<br />
<br />
<br />
hr = pclsObj->Get(L"IPEnabled", 0, &vtProp, 0, 0);<br />
if(vtProp.boolVal)<br />
{ <br />
LONG lstart, lend;<br />
LONG idx = -1;<br />
BSTR* pbstr;<br />
SAFEARRAY *sa;<br />
<br />
hr = pclsObj->Get(L"Description", 0, &vtProp, 0, 0);<br />
if(!FAILED(hr))<br />
wcout << "Description: " << vtProp.bstrVal << endl; <br />
<br />
<br />
hr = pclsObj->Get(L"DNSHostName", 0, &vtProp, 0, 0);<br />
if(!FAILED(hr))<br />
wcout << "DNS:" << vtProp.bstrVal << endl;<br />
<br />
hr = pclsObj->Get(L"IPAddress", 0, &vtProp, 0, 0);<br />
<br />
sa = vtProp.parray; <br />
<br />
hr = SafeArrayGetLBound( sa, 1, &lstart );<br />
if(FAILED(hr)) return hr;<br />
hr = SafeArrayGetUBound( sa, 1, &lend );<br />
if(FAILED(hr)) return hr;<br />
<br />
hr = SafeArrayAccessData(sa,(void HUGEP**)&pbstr);<br />
if(SUCCEEDED(hr))<br />
{<br />
for(idx=lstart; idx <= lend; idx++)<br />
wcout << "IP:" << pbstr[idx] << endl;<br />
hr = SafeArrayUnaccessData(sa); <br />
}<br />
<br />
hr = pclsObj->Get(L"IPSubnet", 0, &vtProp, 0, 0);<br />
sa = vtProp.parray; <br />
<br />
hr = SafeArrayGetLBound( sa, 1, &lstart );<br />
if(FAILED(hr)) return hr;<br />
hr = SafeArrayGetUBound( sa, 1, &lend );<br />
if(FAILED(hr)) return hr;<br />
<br />
hr = SafeArrayAccessData(sa,(void HUGEP**)&pbstr);<br />
if(SUCCEEDED(hr))<br />
{<br />
for(idx=lstart; idx <= lend; idx++)<br />
wcout << "SUBNET:" << pbstr[idx] << endl;<br />
hr = SafeArrayUnaccessData(sa); <br />
}<br />
<br />
<br />
hr = pclsObj->Get(L"DefaultIPGateway", 0, &vtProp, 0, 0);<br />
sa = vtProp.parray; <br />
<br />
hr = SafeArrayGetLBound( sa, 1, &lstart );<br />
if(FAILED(hr)) return hr;<br />
hr = SafeArrayGetUBound( sa, 1, &lend );<br />
if(FAILED(hr)) return hr;<br />
<br />
hr = SafeArrayAccessData(sa,(void HUGEP**)&pbstr);<br />
if(SUCCEEDED(hr))<br />
{<br />
for(idx=lstart; idx <= lend; idx++)<br />
wcout << "GATEWAY:" << pbstr[idx] << endl;<br />
hr = SafeArrayUnaccessData(sa); <br />
<br />
}<br />
}<br />
VariantClear(&vtProp);<br />
}<br />
<br />
if(pEnumerator)<br />
pEnumerator->Release();<br />
if(pclsObj)<br />
pclsObj->Release();<br />
<br />
<br />
|
|
|
|
|
Hi,
First: :-DThank you for the enlightning sample!!!
Second: while the downloaded executable works properly, I get "Could not Enumerate" when I run the compiled code from VC++ 6.0,
when I step into pWbemServices->ExecQuery I get a messagebox labled "Just-In-Time Debugging" saying: "An exception 'Unhandeled win32 exception' has occurred in svchost.exe
Any Ideas why this happenes?
thanks, Isaac
|
|
|
|
|
Looks like you are not getting a valid pointer to pWbemServices. Check the output of the previous method i.e., ConnectServer(...) and see what it gives. Looks to be an error there
Regards,
Aamir
|
|
|
|
|
thanks, but this is not the case.
I get pointers and S_OK results for pWbemServices and for pEnumObject.
only the pClassObject gets a NULL pointer....
|
|
|
|
|
Hi !
This is the best sample i´ve found based on C++. It works fine.
I have modified it to collect HW and SW info on several PC´s and i have found a little problem when i try to change to another namespace
First I create the Locator Object in this way:
IWbemLocator* pLoc = NULL;
hres = CoCreateInstance(CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
(LPVOID *) &pLoc);
Then I use it to get information from \\root\\cimv2
IWbemServices* pSvc = NULL;
hres = pLoc->ConnectServer(L"\\root\\cimv2",
bstruser,
bstrpasswd,
NULL,
NULL,
0,
0,
&pSvc
);
All works fine. But when I try to connect to the namespace:
\\root\\cimv2\\Applications\\MicrosoftIE
I get 'Access denied' although there is no difference between the permissions on both namespaces.
I am reusing the same interfaces, pLoc and pSvc
hres = pLoc->ConnectServer(L"\\root\\cimv2\\Applications\\MicrosoftIE",
bstruser,
bstrpasswd,
NULL,
NULL,
0,
0,
&pSvc
);
Have you found an error like this?
Do I have to create a new instance of the Locator Object to connect to the new namespace or can i use the same ?
Do I have to create another interface to the IWBemServices, pSvc?
(I have tried this, but i get the same error, 0x80070005)
Thanks in advance
AngelM
AngelM
-- modified at 9:13 Monday 14th November, 2005
|
|
|
|
|
Sorry for a very delayed reply. I, in fact havent done these things to this extent. Therefore, I am not sure what might be the problem. I will have a look at it and will see what might be the problem.
Regards,
Aamir
|
|
|
|
|
Thanks Aamir,
I think I´ve found the problem. Now it works, but I don´t know if it´s correct. I´m going to explain myself again:
I extract the information in 5 steps:
Step 1: COM initialization
Step 2: Set COM Security level
Step 3: Create IWbemLocator interface
Step 4: Create IWbemServices interface using this Locator (ConnectServer)
Step 5: Set Proxy Security
Extract the desired information using one of the available methods (ExecQuery, GetObject ...)
I want to read information from root/default, root/CIMv2 and root/CIMv2/Applications/MicrosoftIE namespaces. It´s necessary to use the ConnectServer method for every namespace. At this point I got the error I refered in my first post.
To solve this I have tried the following :
When you want to change to another namespace you only have to repeat two steps, 4 and 5:
Step 4:(IWbemLocator::ConnectServer)
hres = pLoc->ConnectServer(bstrNamespace,
bstrUser,
bstrPasswd,
NULL,
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices
);
Step 5: ( IWbemLocator::CoSetProxyBlanket)
Both using the same IWbemLocator* interface created previously.
The problem appears in the step 4 (ConnectServer). You can´t send the bstrUser/bstrPasswd again. It´s necesary for the first time (root/default), but the second (root/cimv2) and subsequent you have to change these parameters to NULL/NULL:
hres = pLoc->ConnectServer(bstrNamespace,
NULL,
NULL,
NULL,
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices
);
It works fine in this way.
Do you know a correct way to do this?
Thanks in advance for your time
AngelM
|
|
|
|
|
Hi I am using VC6 with WMISDK installed. However, when I am trying to compile the program, it happends to show the following error message
Using WMIDlg.cpp
C:\Documents and Settings\tikcire\Desktop\WMI\Using_WMI\Using WMIDlg.cpp(184) : error C2065: 'CoInitializeSecurity' : undeclared identifier
C:\Documents and Settings\tikcire\Desktop\WMI\Using_WMI\Using WMIDlg.cpp(191) : error C2065: 'EOAC_NONE' : undeclared identifier
Error executing cl.exe.
I know CoInitializeSecurity is being defined at ObjBase.h, which is already included in SDK\include folder. Do you guys know why is that? And what's wrong with my project setting?
Thanks.
tikcireviva
|
|
|
|
|
Have u included wbemuuid.lib in linking libs. I dont know whether WMISDK puts default paths for these libs in the VC environment or not. So, please check it.
Regards,
Aamir
|
|
|
|
|
hi
hi
i have the same problem as the user above whom you replied to. i am using vc++6 and the required SDK.
i have other examples of code that have CoInitializeSecurity and i dont get any compile errors. in "using wmidlg.cpp" i have added two lines as below to try and resolve the problem but no luck. i cant see an option to add additional dependecies in vc++6 so below should do the same?
#define _WIN32_DCOM
#pragma comment(lib, "wbemuuid.lib")
please help
|
|
|
|
|
_WIN32_DCOM should be define in "Project Setting >> Preprocessor definitions"
wewqewqe
|
|
|
|
|
That sorted it out for me thanks xiaozhijian!
|
|
|
|
|
Thanks you so much...when i put _WIN32_DCOM to project setting it compiled with zero error zero warning...
|
|
|
|
|
use your code can not connect to remote machine(win2K),why? please help
wesley
|
|
|
|
|
You need rights to connect to the remote host. Give yourself enough rights and you will be through. e.g., Just try to put yourself in the Administrators group on the other machine and you will be able to browse that machine's WMI Info.
Regards,
Aamir
|
|
|
|
|
hi,
i am working with the code given by aamir butt.
i have vc6
and i have microsoft sdk xp sp2
i selected win32 console application that has mfc support.
My problem is while compiling the code,the following error is coming
Linking...
Wbemuuid.lib(wbemcli_i.obj) : fatal error LNK1103: debugging information corrupt; recompile module
Error executing link.exe.
i have written only a fraction of code given by butt
i only included the functions
- coinitialize
- coinitializesecurity and
- cocreateinstance
when i removed 'cocreateinstance' and compiled, it is not giving errors
i read the articles given here
and tried the first argument with 'CLSID_WbemLocator' and
'CLSID_WbemAdministrativeLocator'.
but no use
i need it for my final sem project
please help me.
following is my code
::CoInitialize(NULL);
if(CoInitializeSecurity( NULL,-1,NULL,NULL,RPC_C_AUTHN_LEVEL_PKT,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_NONE,0) != S_OK)
return -1;
IWbemLocator * pIWbemLocator = NULL;
IWbemServices * pWbemServices = NULL;
IEnumWbemClassObject * pEnumObject = NULL;
BSTR bstrNamespace = (L"root\\cimv2");
HRESULT hRes = CoCreateInstance (CLSID_WbemLocator,
NULL ,
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER , IID_IUnknown ,
( void ** ) & pIWbemLocator ) ;
|
|
|
|
|
Had the same problem last weekend.
Looks like the DDK having a problem.
I found an older version of the library which seems to work better:
24-Jun-04 15:37 32,100 WbemUuid.Lib
Sorry, but can't remember where I downloaded it from.
If you can't find that version, let me know (beecken@image.dk) and I'll send you a copy of my version.
--> Don't like this work around as I don't know what side effects it has.
Regards
Peter Beecken
|
|
|
|
|
I realize I'm responding to an OLD message, but apparently the problem has still not been fixed. Does anyone know of a fix for this? Does anyone know where an older, non-corrupted copy of this library may be obtained.
Thanks to all for any help or advice you can offer.
Jeff Nygren
|
|
|
|
|
Jeff Nygren:
I have fixed this problem by copy wbemuuid.lib from VC.net(VC7) as the first reply said. You can send me a E_mail (pilixuanke@gmail.com) to request this file if u can't get it by yourself.
|
|
|
|
|
i have vc++ 6.0 and sdk xp sp2
when i copied and run the code given by Aamir Butt,
i got the link error saying "object file is corrupted, recompile the code"
compilation is ok
but linking is the problem
any suggestions?
bye
visu
|
|
|
|
|
The following is a copy of my post in COM message board. Please, help ....
Has anyone been successful in executing the
Win32_OperatingSystem class method "Reboot" or "Shutdown" from a call to the ExecMethod routine in C++. This method works fine using VBScript. But in C++, the method returns an error in ExecMethod - WBEM_E_INVALID_METHOD_PARAMETERS.
Why ?
Here is my code :
...
HRESULT hr;
CComPtr<IWbemLocator> pLocator;
CComPtr<IWbemServices> pServices;
CComBSTR bstrPath = L"//";
bstrPath.AppendBSTR(bstrComputer);
bstrPath.Append(L"/root/cimv2");
CComBSTR bstrObject = L"Win32_OperatingSystem";
CComBSTR bstrMethod = L"Win32Shutdown";
CComBSTR bstrLanguage = SysAllocString(L"WQL");
CComBSTR bstrQuery = SysAllocString(L"select * from Win32_OperatingSystem where Primary=true");
CComBSTR bstrFlags = L"Flags";
CComBSTR bstrReserved = L"Reserved";
hr = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hr))
{
cout << "Failed in CoInitializeEx" << endl;
return hr;
}
hr = CoInitializeSecurity(
NULL,
-1, // COM negotiates service
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (FAILED(hr))
{
cout << "Failed in CoInitializeSecurity" << endl;
CoUninitialize();
return hr;
}
//get the initial namespace of the CIM Object Manager
hr = pLocator.CoCreateInstance(__uuidof(WbemLocator), 0, CLSCTX_INPROC_SERVER);
if (FAILED(hr)){
cout << "Failed in CoCreateInstance" << endl;
return hr;
}
//create an instance for the interface that connects WBEM services
hr = pLocator->ConnectServer(bstrPath, NULL, NULL, NULL, 0, NULL, NULL, &pServices);
if (FAILED(hr)){
cout << "Failed in ConnectServer" << endl;
return hr;
}
//authenticate to the service, using current credentials
hr = CoSetProxyBlanket(pServices, 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 << "Failed in CoSetProxyBlanket" << endl;
return hr;
}
//query WMI for objects: get the Win32_OperatingSystem class using WQL
CComPtr<IEnumWbemClassObject> pEnumWbem;
hr = pServices->ExecQuery(bstrLanguage, bstrQuery, WBEM_FLAG_FORWARD_ONLY, NULL, &pEnumWbem);
if (FAILED(hr)){
cout << "Failed in ExecQuery" << endl;
return hr;
}
//for each returned object call Win32Shutdown
IWbemClassObject* pWbemObject;
DWORD nRetObjects;
pEnumWbem->Reset();
while (pEnumWbem->Next(WBEM_INFINITE, 1, &pWbemObject, &nRetObjects) == S_OK)
{
CComPtr<IWbemClassObject> pClass;
CComPtr<IWbemClassObject> pMethod;
CComPtr<IWbemClassObject> pInParams;
CComPtr<IWbemClassObject> pOutParams;
//get the class definition of Win32_OperatingSystem
hr = pServices->GetObject(bstrObject, 0, NULL, &pClass, NULL);
if (FAILED(hr))
{
cout << "Failed in GetObject" << endl;
pWbemObject->Release();
return hr;
}
//get our method
hr = pClass->GetMethod(bstrMethod, 0, &pMethod, NULL);
if (FAILED(hr))
{
cout << "Failed in GetMethod" << endl;
pWbemObject->Release();
return hr;
}
//create a new object for in parameters
hr = pMethod->SpawnInstance(0, &pInParams);
if (FAILED(hr))
{
cout << "Failed in SpawnInstance" << endl;
pWbemObject->Release();
return hr;
}
//add parameters
CComVariant var;
V_VT(&var) = VT_I4;
V_I4(&var) = (short)nAction;
hr = pInParams->Put(bstrFlags, 0, &var, CIM_SINT32);
if (FAILED(hr))
{
cout << "Failed in Put" << endl;
pWbemObject->Release();
return hr;
}
//call Win32Shutdown with our action
hr = pServices->ExecMethod(bstrObject, bstrMethod, 0L, NULL, pInParams, &pOutParams, NULL);
if(hr == WBEM_E_INVALID_METHOD_PARAMETERS){
cout << "That's it" << endl;
}
if (FAILED(hr)){
cout << "Failed in ExecMethod" << endl;
pWbemObject->Release();
return hr;
}
pWbemObject->Release();
}
Code Red
|
|
|
|
|
???? mark
Any automation language, such as Visual Basic, VBScript, JScript, or Perl, can call a WMI method. Some languages can use direct access but others must use SWbemServices.ExecMethod to execute the provider method indirectly.
refer to MSDN ,I think
smart
|
|
|
|
|