wmi enum all cddrive and dump properties
//The following can run on windows 2000 professional when login as
guest
#define _WIN32_DCOM
#include <Wbemidl.h>
#pragma comment(lib,"Wbemuuid.lib")
#include <atlbase.h>
#include "objbase.h"
#include <windows.h>
#include <comutil.h>
#pragma comment(lib, "comsupp.lib")
#include <iostream>
void print_property(CComBSTR bstrPropName, _variant_t& varProperty)
{
std::cout<<" "<<(LPCTSTR)_bstr_t(bstrPropName);
std::cout<<" = ";
if(varProperty.vt !=VT_EMPTY && VT_NULL != varProperty.vt)
{
_variant_t vDest;
HRESULT hr = VariantChangeType(&vDest, &varProperty,0,VT_BSTR);
if(SUCCEEDED(hr))
std::cout<< (LPCTSTR)_bstr_t(varProperty);
else
{
if(varProperty.vt == (VT_ARRAY | VT_I4) )
{
SAFEARRAY* psa = varProperty.parray;
int *pIntArray = NULL;
SafeArrayAccessData(psa,(VOID**)&pIntArray);
UINT uDim = SafeArrayGetDim(psa);
if(1==uDim)
{
long lLbound,lRbound;
SafeArrayGetLBound(psa,1,&lLbound);
SafeArrayGetUBound(psa,1,&lRbound);
for(long i=lLbound;i<=lRbound;i++)
{
std::cout<< pIntArray[i]<<" ";
}
}
SafeArrayUnaccessData(psa);
}
else
{
std::cout<< varProperty.vt << " type ";
}
}
}
else
{
if(varProperty.vt == VT_EMPTY)
std::cout<< "VT_EMPTY ";
else
std::cout<< "VT_NULL";
}
std::cout<<std::endl;
}
HRESULT PrintObject(IWbemClassObject* spInstance)
{
// std::cout<<__FUNCTION__<<" start "<<std::endl;
LPSAFEARRAY psa = NULL;
HRESULT hres;
hres = spInstance->GetNames( NULL,
WBEM_FLAG_ALWAYS | WBEM_FLAG_NONSYSTEM_ONLY,
NULL,
&psa);
long lLower, lUpper;
SafeArrayGetLBound(psa , 1, &lLower);
SafeArrayGetUBound(psa , 1, &lUpper);
for (long i = lLower; i <= lUpper; ++i)
{
CComBSTR bstrPropName;
if (S_OK != (hres = SafeArrayGetElement(psa, &i, &bstrPropName)) )
{
if (NULL != psa)
SafeArrayDestroy(psa);
return hres;
}
_variant_t varProperty;
HRESULT hr = spInstance->Get(bstrPropName,0,&varProperty,NULL,0);
print_property(bstrPropName,varProperty);
}
if (NULL != psa)
SafeArrayDestroy(psa);
// std::cout<<__FUNCTION__<<" end "<<std::endl;
return S_OK;
}
main()
{
CoInitialize(NULL);
{
HRESULT hr = S_OK;
CComPtr<IWbemLocator> spLoc;
hr = spLoc.CoCreateInstance(CLSID_WbemLocator);
CComBSTR bstrNamespace("\\\\.\\root\\CIMV2");
CComPtr<IWbemServices> spServices;
hr = spLoc->ConnectServer(bstrNamespace, NULL, NULL, 0,
NULL, 0, 0, &spServices);
hr = CoSetProxyBlanket(spServices, RPC_C_AUTHN_DEFAULT,
RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
{
CComPtr<IEnumWbemClassObject> spEnumInst;
hr = spServices->CreateInstanceEnum(CComBSTR("Win32_CDROMDrive"),
WBEM_FLAG_SHALLOW, NULL, &spEnumInst);
bool bFinished = false;
while (!bFinished)
{
ULONG uNumOfInstances = 0;
CComPtr<IWbemClassObject> spInstance;
HRESULT hrNext = spEnumInst->Next(10000, 1, &spInstance,
&uNumOfInstances);
if (hrNext == WBEM_S_FALSE)
bFinished = true;
else if (hrNext == WBEM_S_NO_ERROR)
{
CComBSTR bstrPropName = "Drive";
_variant_t varProperty;
hr = spInstance->Get(bstrPropName,0,&varProperty,NULL,0);
print_property(bstrPropName,varProperty);
if(SUCCEEDED(hr) && VT_BSTR == varProperty.vt)
{
_bstr_t bsdrive = varProperty;
bstrPropName = "Caption";
hr = spInstance->Get(bstrPropName,0,&varProperty,NULL,0);
if(SUCCEEDED(hr) && VT_BSTR == varProperty.vt)
{
}
print_property(bstrPropName,varProperty);
}
}
}
}
}
std::cout<< "finished "<<std::endl;
CoUninitialize();
}
guest
#define _WIN32_DCOM
#include <Wbemidl.h>
#pragma comment(lib,"Wbemuuid.lib")
#include <atlbase.h>
#include "objbase.h"
#include <windows.h>
#include <comutil.h>
#pragma comment(lib, "comsupp.lib")
#include <iostream>
void print_property(CComBSTR bstrPropName, _variant_t& varProperty)
{
std::cout<<" "<<(LPCTSTR)_bstr_t(bstrPropName);
std::cout<<" = ";
if(varProperty.vt !=VT_EMPTY && VT_NULL != varProperty.vt)
{
_variant_t vDest;
HRESULT hr = VariantChangeType(&vDest, &varProperty,0,VT_BSTR);
if(SUCCEEDED(hr))
std::cout<< (LPCTSTR)_bstr_t(varProperty);
else
{
if(varProperty.vt == (VT_ARRAY | VT_I4) )
{
SAFEARRAY* psa = varProperty.parray;
int *pIntArray = NULL;
SafeArrayAccessData(psa,(VOID**)&pIntArray);
UINT uDim = SafeArrayGetDim(psa);
if(1==uDim)
{
long lLbound,lRbound;
SafeArrayGetLBound(psa,1,&lLbound);
SafeArrayGetUBound(psa,1,&lRbound);
for(long i=lLbound;i<=lRbound;i++)
{
std::cout<< pIntArray[i]<<" ";
}
}
SafeArrayUnaccessData(psa);
}
else
{
std::cout<< varProperty.vt << " type ";
}
}
}
else
{
if(varProperty.vt == VT_EMPTY)
std::cout<< "VT_EMPTY ";
else
std::cout<< "VT_NULL";
}
std::cout<<std::endl;
}
HRESULT PrintObject(IWbemClassObject* spInstance)
{
// std::cout<<__FUNCTION__<<" start "<<std::endl;
LPSAFEARRAY psa = NULL;
HRESULT hres;
hres = spInstance->GetNames( NULL,
WBEM_FLAG_ALWAYS | WBEM_FLAG_NONSYSTEM_ONLY,
NULL,
&psa);
long lLower, lUpper;
SafeArrayGetLBound(psa , 1, &lLower);
SafeArrayGetUBound(psa , 1, &lUpper);
for (long i = lLower; i <= lUpper; ++i)
{
CComBSTR bstrPropName;
if (S_OK != (hres = SafeArrayGetElement(psa, &i, &bstrPropName)) )
{
if (NULL != psa)
SafeArrayDestroy(psa);
return hres;
}
_variant_t varProperty;
HRESULT hr = spInstance->Get(bstrPropName,0,&varProperty,NULL,0);
print_property(bstrPropName,varProperty);
}
if (NULL != psa)
SafeArrayDestroy(psa);
// std::cout<<__FUNCTION__<<" end "<<std::endl;
return S_OK;
}
main()
{
CoInitialize(NULL);
{
HRESULT hr = S_OK;
CComPtr<IWbemLocator> spLoc;
hr = spLoc.CoCreateInstance(CLSID_WbemLocator);
CComBSTR bstrNamespace("\\\\.\\root\\CIMV2");
CComPtr<IWbemServices> spServices;
hr = spLoc->ConnectServer(bstrNamespace, NULL, NULL, 0,
NULL, 0, 0, &spServices);
hr = CoSetProxyBlanket(spServices, RPC_C_AUTHN_DEFAULT,
RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
{
CComPtr<IEnumWbemClassObject> spEnumInst;
hr = spServices->CreateInstanceEnum(CComBSTR("Win32_CDROMDrive"),
WBEM_FLAG_SHALLOW, NULL, &spEnumInst);
bool bFinished = false;
while (!bFinished)
{
ULONG uNumOfInstances = 0;
CComPtr<IWbemClassObject> spInstance;
HRESULT hrNext = spEnumInst->Next(10000, 1, &spInstance,
&uNumOfInstances);
if (hrNext == WBEM_S_FALSE)
bFinished = true;
else if (hrNext == WBEM_S_NO_ERROR)
{
CComBSTR bstrPropName = "Drive";
_variant_t varProperty;
hr = spInstance->Get(bstrPropName,0,&varProperty,NULL,0);
print_property(bstrPropName,varProperty);
if(SUCCEEDED(hr) && VT_BSTR == varProperty.vt)
{
_bstr_t bsdrive = varProperty;
bstrPropName = "Caption";
hr = spInstance->Get(bstrPropName,0,&varProperty,NULL,0);
if(SUCCEEDED(hr) && VT_BSTR == varProperty.vt)
{
}
print_property(bstrPropName,varProperty);
}
}
}
}
}
std::cout<< "finished "<<std::endl;
CoUninitialize();
}
0 Comments:
Post a Comment
<< Home