本文整理汇总了C++中IWbemClassObject类的典型用法代码示例。如果您正苦于以下问题:C++ IWbemClassObject类的具体用法?C++ IWbemClassObject怎么用?C++ IWbemClassObject使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了IWbemClassObject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: BSTR
std::shared_ptr<CHardware> CHardwareFactory::newOpticalDiscDrive() const
{
/*
* IWbemServices::ExecQuery http://msdn.microsoft.com/en-us/library/windows/desktop/aa392107%28v=VS.85%29.aspx
* IWbemClassObject::Next http://msdn.microsoft.com/en-us/library/windows/desktop/aa391453%28v=VS.85%29.aspx
* IWbemClassObject::Get http://msdn.microsoft.com/en-us/library/windows/desktop/aa391442%28v=VS.85%29.aspx
* Win32_CDROMDrive class http://msdn.microsoft.com/en-us/library/windows/desktop/aa394081%28v=VS.85%29.aspx
*
* Creating a WMI Application Using C++ http://msdn.microsoft.com/en-us/library/windows/desktop/aa389762%28v=VS.85%29.aspx
*/
IEnumWbemClassObject* enumerator = 0;
HRESULT hr = mWbemServices->ExecQuery(BSTR(L"WQL"), BSTR(L"SELECT * FROM Win32_CDROMDrive"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 0, &enumerator);
if (FAILED(hr))
{
throw CBasicException("ExecQuery failed");
}
std::list<std::string> opticalDriveList;
while(enumerator)
{
IWbemClassObject* obj = 0;
ULONG ret = 0;
hr = enumerator->Next(WBEM_INFINITE, 1, &obj, &ret);
if (ret == 0 || FAILED(hr))
{
break;
}
VARIANT vtProp;
hr = obj->Get(L"Caption", 0, &vtProp, 0, 0);
if (FAILED(hr))
{
continue;
}
opticalDriveList.push_back(CUtils::str2utf8(vtProp.bstrVal));
VariantClear(&vtProp);
obj->Release();
}
enumerator->Release();
return std::shared_ptr<CHardware>(new CHardware(CHardware::OPTICALDISCDRIVE, opticalDriveList));
}
示例2: testForHyperv
/**
* Test to see whether we are running on a system with Hyper-V enabled.
* We consider Hyper-V to be running (and can export per vm stats) if
* the Hyper-V related services (nvspwmi, vmm, vhdsvc) are running and
* we can access the WMI namespace root\virtualization (ie v1 for Win 2008).
* We do not check for the v2 namespace, since this is not required for
* per vm stats.
* The ability connect to the sFlow filter for export of packet samples
* and counter samples for the virtual switch is made separately.
*/
BOOL testForHyperv()
{
BSTR path = SysAllocString(WMI_CIMV2_NS);
HRESULT hr = S_FALSE;
IWbemServices *pNamespace = NULL;
hr = connectToWMI(path, &pNamespace);
SysFreeString(path);
if (WBEM_S_NO_ERROR != hr) {
myLog(LOG_ERR,"testForHyperv: connectToWMI failed for namespace %S", path);
return false;
}
//Test for Hyper-V services
BOOL gotHyperV = false;
BSTR queryLang = SysAllocString(L"WQL");
BSTR query = SysAllocString(L"SELECT * FROM Win32_Service WHERE Name=\"nvspwmi\" OR Name=\"vmms\" OR Name=\"vhdsvc\"");
IEnumWbemClassObject *serviceEnum = NULL;
hr = pNamespace->ExecQuery(queryLang, query, WBEM_FLAG_FORWARD_ONLY, NULL, &serviceEnum);
SysFreeString(query);
SysFreeString(queryLang);
if (WBEM_S_NO_ERROR != hr) {
myLog(LOG_ERR, "testForHyperv: ExecQuery() failed for %S error=0x%x", query, hr);
} else {
IWbemClassObject *serviceObj = NULL;
ULONG uReturned = 0;
BOOL gotHyperVSvc = false;
hr = serviceEnum->Next(WBEM_INFINITE, 1, &serviceObj, &uReturned);
if (SUCCEEDED(hr)) {
if (uReturned == 1) {
gotHyperVSvc = true;
serviceObj->Release();
}
}
serviceEnum->Release();
pNamespace->Release();
if (gotHyperVSvc) { //now check that we have the v1 virtualization namespace
CoUninitialize();
path = SysAllocString(WMI_VIRTUALIZATION_NS_V1);
hr = connectToWMI(path, &pNamespace);
SysFreeString(path);
if (WBEM_NO_ERROR == hr) {
gotHyperV = true;
pNamespace->Release();
}
}
}
CoUninitialize();
myLog(LOG_INFO, "testForHyperv: HyperV=%u", gotHyperV);
return gotHyperV;
}
示例3: vmware_wmi
/*
Check VMWare bios using WMI
*/
BOOL vmware_wmi()
{
IWbemServices *pSvc = NULL;
IWbemLocator *pLoc = NULL;
IEnumWbemClassObject* pEnumerator = NULL;
BOOL bStatus = FALSE;
HRESULT hr;
// Init WMI
bStatus = InitWMI(&pSvc, &pLoc);
if (bStatus)
{
// If success, execute the desired query
bStatus = ExecWMIQuery(&pSvc, &pLoc, &pEnumerator, _T("SELECT * FROM Win32_PnPEntity"));
if (bStatus)
{
// Get the data from the query
IWbemClassObject *pclsObj = NULL;
ULONG uReturn = 0;
VARIANT vtProp;
while (pEnumerator)
{
hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if (0 == uReturn)
break;
// Get the value of the Name property
hr = pclsObj->Get(_T("DeviceId"), 0, &vtProp, 0, 0);
//_tprintf(_T("DeviceId : %s"), vtProp.bstrVal);
// release the current result object
VariantClear(&vtProp);
pclsObj->Release();
}
}
}
// Cleanup
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
CoUninitialize();
return TRUE;
}
示例4: TryAddress
static bool TryAddress(const std::string& ipAddress)
{
IEnumWbemClassObject* enumerator = 0;
std::wstring query(L"SELECT * FROM Win32_PingStatus WHERE (Address=\"");
query.append(::UTF8ToWide(ipAddress));
query.append(L"\")");
BSTR queryBstr = SysAllocString(query.c_str());
HRESULT result = service->ExecQuery(L"WQL", queryBstr,
WBEM_FLAG_FORWARD_ONLY, 0, &enumerator);
SysFreeString(queryBstr);
if (FAILED(result))
{
HandleHResultError("Failed to execute address query", result);
return false;
}
ULONG count;
IWbemClassObject* ping = 0;
result = enumerator->Next(WBEM_INFINITE, 1L, &ping, &count);
if (FAILED(result))
{
HandleHResultError("Failed to get ping data", result);
enumerator->Release();
return false;
}
if (count < 1)
{
LogError("Did not find ping result");
enumerator->Release();
ping->Release();
return false;
}
variant_t pingValue;
result = ping->Get(L"StatusCode", 0, &pingValue, 0, 0);
if (FAILED(result))
{
HandleHResultError("Could not get ping StatusCode value", result);
enumerator->Release();
ping->Release();
return false;
}
ping->Release();
return static_cast<int>(pingValue) == 0;
}
示例5: ReadACL
bool ReadACL(IWbemServices *pNamespace)
{
bool bRet = false;
_bstr_t InstPath(L"[email protected]");
_bstr_t MethName(L"GetSD");
IWbemClassObject * pOutParams = NULL;
// The security descriptor is returned via the GetSD method
HRESULT hr = pNamespace->ExecMethod(InstPath,
MethName,
0,
NULL, NULL,
&pOutParams, NULL);
if(SUCCEEDED(hr))
{
// The output parameters has a property which has the descriptor
_bstr_t prop(L"sd");
_variant_t var;
hr = pOutParams->Get(prop, 0, &var, NULL, NULL);
if(SUCCEEDED(hr))
{
if(var.vt != (VT_ARRAY | VT_UI1))
return false;
SAFEARRAY * psa = var.parray;
PSECURITY_DESCRIPTOR pSD;
hr = SafeArrayAccessData(psa, (void HUGEP* FAR*)&pSD);
if(hr != 0)
return false;
// Dump out some information
DumpSD(pSD);
// Given that the security desciptor is now present, the code could use standard
// nt functions to add or remove ace's. There are also various libraries available
// that can make the job a bit easier. This sample does not change the security
// descriptor, but does write it back unchanged as an example.
StoreSD(pNamespace, pSD);
SafeArrayUnaccessData(psa);
bRet = true;
}
}
return bRet;
}
示例6: getTimeProcessors
int getTimeProcessors(IWbemServices *pSvc,ULONG *ulVal ,int n)
{
int nError = 0;
IEnumWbemClassObject* pEnumerator = NULL;
HRESULT hres = pSvc->ExecQuery( L"WQL", L"SELECT * FROM Win32_PerfRawData_PerfOS_Processor",
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);
if (FAILED(hres))
{
ShowMessage("Query for operating system name failed.");
return 1;
}
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
int nCtr = 0;
while (nCtr<n)
{
pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if(0 == uReturn)
{
break;
}
VARIANT vtProp;
VariantInit(&vtProp);
pclsObj->Get(L"PercentProcessorTime", 0, &vtProp, 0, 0);
ulVal[nCtr] = _wtol(vtProp.bstrVal);
VariantClear(&vtProp);
pclsObj->Get(L"TimeStamp_Sys100NS", 0, &vtProp, 0, 0);// время в милисекундах прошедших с получночи 1 января 1970 года
ulVal[nCtr+1] = _wtol(vtProp.bstrVal);
VariantClear(&vtProp);
nCtr+=2;
}
pclsObj->Release();
pEnumerator->Release();
return nError;
}
示例7: ListClasses
void ListClasses(IWbemServices *pNamespace)
{
HRESULT hr;
IEnumWbemClassObject *pEnum = NULL;
hr = pNamespace->CreateClassEnum(NULL, 0, NULL, &pEnum);
if(SUCCEEDED(hr))
{
// Note that even though security was set on the namespace pointer, it must
// also be set on this pointer since COM will revert back to the default
// settings for any new pointers!
hr = SetProxySecurity(pEnum);
if(SUCCEEDED(hr))
{
IWbemClassObject* Array[1];
Array[0] = NULL;
ULONG uRet = 0;
while (SUCCEEDED(hr = pEnum->Next(10000, 1, Array, &uRet)) && Array[0])
{
// Note that IWbemClassObjects are inproc and thus have no proxy.
// Therefore, they never need CoSetProxyBlanket.
BSTR strText;
IWbemClassObject* pObj = Array[0];
hr = pObj->GetObjectText(0, &strText);
if(SUCCEEDED(hr) && strText)
{
printf("\nGot class %S", strText);
SysFreeString(strText);
}
pObj->Release();
Array[0] = NULL;
}
}
else
printf("\nFAILED TO SET SECURITY FOR ENUMERATOR!!!!! hr = 0x%x", hr);
pEnum->Release();
}
}
示例8: GetInputParameters
HRESULT GetInputParameters(__in IWbemServices* pSvc, __in PCWSTR pClassName, __in PCWSTR pMethodName, __out IWbemClassObject** ppInParams)
{
HRESULT hr;
IWbemClassObject* pClass = NULL;
IWbemClassObject* pInParamsDefinition = NULL;
IWbemClassObject* pInParamsLocal = NULL;
hr = pSvc->GetObject(_bstr_t(pClassName),
0,
NULL,
&pClass,
NULL);
if(HB_FAILED(hr)) {
goto cleanexit;
}
hr = pClass->GetMethod(_bstr_t(pMethodName),
0,
&pInParamsDefinition,
NULL);
if(HB_FAILED(hr)) {
goto cleanexit;
}
hr = pInParamsDefinition->SpawnInstance(0, &pInParamsLocal);
if(HB_FAILED(hr)) {
goto cleanexit;
}
*ppInParams = pInParamsLocal;
pInParamsLocal = NULL;
cleanexit:
HB_SAFE_RELEASE(pClass);
HB_SAFE_RELEASE(pInParamsDefinition);
HB_SAFE_RELEASE(pInParamsLocal);
return hr;
}
示例9: _GetClassObjectValue
BOOL _GetClassObjectValue(IEnumWbemClassObject* pEnumerator, const CString& cstrValueName, VARIANT& vt)
{
BOOL bRet = FALSE;
IWbemClassObject *pclsObj = NULL;
if (pEnumerator != NULL)
{
ULONG uReturn = 0;
while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE,
1,
&pclsObj,
&uReturn);
if(0 == uReturn || pclsObj == NULL)
{
break;
}
VARIANT vtProp;
hr = pclsObj->Get(cstrValueName, 0, &vtProp, 0, 0);
if (FAILED(hr))
{
continue;
}
vt = vtProp;
bRet = TRUE;
break;
}
}
if (pclsObj)
{
pclsObj->Release();
}
return bRet;
}
示例10: readInterfacesWin32
/**
* Enumerates the adapters for this host from WMI Win32_NetworkAdapter
* where NetConnectionStatus=2 (to exclude tunnels, ras, wan miniports etc).
* Uses the information to populate the sp->adaptorList structure.
* adapter->deviceName = Win32_NetworkAdapter.GUID (converted to
* lowercase char with enclosing {} removed)
* adapter->ifIndex = Win32_NetworkAdapter.InterfaceIndex
* this is the interface index used in the route table (rather than Index
* which is the index for the interface in the registry).
* adapter->userData->countersInstance = Win32_NetworkAdapter.Name
* (with reserved chars replaced)
* adapter->userData->isVirtual = (Win32_NetworkAdapter.ServiceName == "VMSMP")
* Optionally gets the IP address (v4 and/or v6) from the associated
* Win32_NetworkAdapterConfiguration. This is only required when trying
* to identify the IP addresses that could be used as the agent address.
* Returns true on success, false on failure.
*/
static BOOL readInterfacesWin32(SFLAdaptorList *adaptorList, BOOL getIpAddr)
{
BSTR path = SysAllocString(WMI_CIMV2_NS);
HRESULT hr = S_FALSE;
IWbemServices *pNamespace = NULL;
hr = connectToWMI(path, &pNamespace);
SysFreeString(path);
if (WBEM_S_NO_ERROR != hr) {
myLog(LOG_ERR,"readInterfacesWin32: connectToWMI failed for namespace %S", path);
return FALSE;
}
BSTR queryLang = SysAllocString(L"WQL");
BSTR query = SysAllocString(L"SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionStatus=2");
IEnumWbemClassObject *adapterEnum = NULL;
hr = pNamespace->ExecQuery(queryLang, query, WBEM_FLAG_FORWARD_ONLY, NULL, &adapterEnum);
SysFreeString(queryLang);
if (!SUCCEEDED(hr)) {
myLog(LOG_ERR,"readInterfacesWin32: ExecQuery() failed for query %S error=0x%x", query, hr);
SysFreeString(query);
pNamespace->Release();
return FALSE;
}
SysFreeString(query);
IWbemClassObject *adapterObj = NULL;
VARIANT ifIndexVal;
hr = WBEM_S_NO_ERROR;
while (WBEM_S_NO_ERROR == hr) {
ULONG adapterCount = 1;
hr = adapterEnum->Next(WBEM_INFINITE, 1, &adapterObj, &adapterCount);
if (0 == adapterCount) {
break;
}
wchar_t *guidString = stringFromWMIProperty(adapterObj, PROP_GUID);
wchar_t *macString = stringFromWMIProperty(adapterObj, PROP_MAC);
if (guidString != NULL && macString != NULL) {
u_char deviceName[FORMATTED_GUID_LEN+1];
guidToString(guidString, deviceName, FORMATTED_GUID_LEN);
u_char mac[13];
wchexToBinary(macString, mac, 13);
SFLAdaptor *adaptor = adaptorListAdd(adaptorList,
(char *)deviceName, mac,
sizeof(HSPAdaptorNIO));
// clear the mark so we don't free it later
adaptor->marked = FALSE;
if (WBEM_S_NO_ERROR == adapterObj->Get(PROP_IFINDEX, 0, &ifIndexVal, 0, 0) &&
(V_VT(&ifIndexVal) == VT_I4 || V_VT(&ifIndexVal) == VT_UI4)) {
adaptor->ifIndex = ifIndexVal.ulVal;
}
HSPAdaptorNIO *userData = (HSPAdaptorNIO *)adaptor->userData;
if (userData->countersInstance != NULL) {
my_free(userData->countersInstance);
}
wchar_t *counterName = stringFromWMIProperty(adapterObj, PROP_NAME);
if (counterName != NULL) {
cleanCounterName(counterName, UTNETWORK_INTERFACE);
userData->countersInstance = counterName;
}
wchar_t *svcName = stringFromWMIProperty(adapterObj, PROP_SVC_NAME);
if (svcName != NULL) {
userData->isVirtual = (_wcsicmp(VMSMP, svcName) == 0);
my_free(svcName);
}
if (getIpAddr) {
userData->ipPriority = IPSP_NONE;
readIpAddressesWin32(pNamespace, adapterObj, adaptor);
}
myLog(LOG_INFO,"ReadInterfacesWin32:\n\tAdapterName:\t%s\n\tifIndex:\t%lu\n\tCounterName:\t%S\n\tisVirtual\t%u",
adaptor->deviceName, adaptor->ifIndex, userData->countersInstance, userData->isVirtual);
}
if (guidString != NULL) {
my_free(guidString);
}
if (macString != NULL) {
my_free(macString);
}
adapterObj->Release();
VariantClear(&ifIndexVal);
}
adapterEnum->Release();
pNamespace->Release();
return TRUE;
}
示例11: CoInitializeEx
bool CSMBiosTable::FetchSMBiosDataByCom( unsigned char ** p )
{
BOOL bRet = FALSE;
HRESULT hres;
// Initialize COM.
hres = CoInitializeEx( 0, COINIT_MULTITHREADED );
if( FAILED(hres) )
{
return FALSE; // Program has failed.
}
// Obtain the initial locator to Windows Management
// on a particular host computer.
IWbemLocator *pLoc = 0;
hres = CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc );
if( FAILED(hres) )
{
CoUninitialize();
return FALSE; // Program has failed.
}
IWbemServices *pSvc = 0;
// Connect to the root\cimv2 namespace with the z
// current user and obtain pointer pSvc
// to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\WMI"), // WMI namespace
NULL, // User name
NULL, // User password
0, // Locale
NULL, // Security flags
0, // Authority
0, // Context object
&pSvc // IWbemServices proxy
);
if( FAILED(hres) )
{
pLoc->Release();
CoUninitialize();
return FALSE; // Program has failed.
}
// Set the IWbemServices proxy so that impersonation
// of the user (client) occurs.
hres = CoSetProxyBlanket(
pSvc, // the proxy to set
RPC_C_AUTHN_WINNT, // authentication service
RPC_C_AUTHZ_NONE, // authorization service
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // authentication level
RPC_C_IMP_LEVEL_IMPERSONATE, // impersonation level
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if( FAILED(hres) )
{
pSvc->Release();
pLoc->Release();
CoUninitialize();
return FALSE; // Program has failed.
}
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->CreateInstanceEnum( L"MSSMBios_RawSMBiosTables", 0, NULL, &pEnumerator);
if( FAILED(hres) )
{
pSvc->Release();
pLoc->Release();
CoUninitialize();
return FALSE; // Program has failed.
}
else
{
do
{
IWbemClassObject* pInstance = NULL;
ULONG dwCount = NULL;
hres = pEnumerator->Next( WBEM_INFINITE, 1, &pInstance, &dwCount);
if( SUCCEEDED(hres) )
{
VARIANT varBIOSData;
VariantInit(&varBIOSData);
CIMTYPE type;
hres = pInstance->Get(bstr_t("SmbiosMajorVersion"),0,&varBIOSData,&type,NULL);
if( FAILED(hres) )
{
VariantClear(&varBIOSData);
}
else
{
m_smbiosbuffer.nSMBIOSMajorVersion = varBIOSData.iVal;
VariantInit(&varBIOSData);
hres = pInstance->Get( bstr_t("SmbiosMinorVersion"), 0, &varBIOSData, &type, NULL );
if( FAILED(hres) )
//.........这里部分代码省略.........
示例12: ExecuteMethod3InInstance
HRESULT
ExecuteMethod3InInstance(
__in IWbemServices* WbemServices,
__in IWbemClassObject* ClassObj,
__in const BSTR InstancePath,
__in ULONG InData1,
__in ULONG InData2
)
{
HRESULT status;
IWbemClassObject* inputParamsObj = NULL;
IWbemClassObject* inputParamsInstanceObj = NULL;
IWbemClassObject* outputParamsInstanceObj = NULL;
const BSTR methodName = SysAllocString(TOASTER_METHOD_3);
VARIANT funcParam;
//
// Get the input parameters class objects for the method.
//
status = ClassObj->GetMethod(methodName, 0, &inputParamsObj, NULL);
if (FAILED(status)) {
goto exit;
}
//
// Spawn an instance of the input parameters class object.
//
status = inputParamsObj->SpawnInstance(0, &inputParamsInstanceObj);
if (FAILED(status)) {
goto exit;
}
//
// Set the input variables values (i.e., inData1, inData2 for ToasterMethod3).
//
funcParam.vt = VT_I4;
funcParam.ulVal = InData1;
status = inputParamsInstanceObj->Put(L"InData1", 0, &funcParam, 0);
if (FAILED(status)) {
goto exit;
}
funcParam.vt = VT_I4;
funcParam.ulVal = InData2;
status = inputParamsInstanceObj->Put(L"InData2", 0, &funcParam, 0);
if (FAILED(status)) {
goto exit;
}
//
// Call the method.
//
printf("\n");
printf("Instance Path .: %ws\n", (wchar_t*)InstancePath);
printf(" Method Name..: %ws\n", (wchar_t*)methodName);
status = WbemServices->ExecMethod(InstancePath,
methodName,
0,
NULL,
inputParamsInstanceObj,
&outputParamsInstanceObj,
NULL);
if (FAILED(status)) {
goto exit;
}
//
// Get the "in" Parameter values from the input parameters object.
//
status = inputParamsInstanceObj->Get(L"InData1", 0, &funcParam, NULL, NULL);
if (FAILED(status)) {
goto exit;
}
printf(" InData1...: %d\n", funcParam.ulVal);
status = inputParamsInstanceObj->Get(L"InData2", 0, &funcParam, NULL, NULL);
if (FAILED(status)) {
goto exit;
}
printf(" InData2...: %d\n", funcParam.ulVal);
//
// Get the "out" Parameter values from the output parameters object.
//
status = outputParamsInstanceObj->Get(L"OutData1", 0, &funcParam, NULL, NULL);
if (FAILED(status)) {
goto exit;
}
printf(" OutData1..: %d\n", funcParam.ulVal);
status = outputParamsInstanceObj->Get(L"OutData2", 0, &funcParam, NULL, NULL);
if (FAILED(status)) {
goto exit;
}
printf(" OutData2..: %d\n", funcParam.ulVal);
//.........这里部分代码省略.........
示例13: ExecuteMethodsInClass
HRESULT
ExecuteMethodsInClass(
__in IWbemServices* WbemServices,
__in_opt PWCHAR UserId,
__in_opt PWCHAR Password,
__in_opt PWCHAR DomainName
)
/*++
Routine Description:
This routine enumerates the instances of the Toaster method class and
executes the methods in the class for each instance.
Arguments:
WbemServices - Pointer to the WBEM services interface used for accessing
the WMI services.
UserId - Pointer to the user id information or NULL.
Password - Pointer to password or NULL. If the user id is not specified,
this parameter is ignored.
DomainName - Pointer to domain name or NULL. If the user id is not specified,
this parameter is ignored.
Return Value:
HRESULT Status code.
--*/
{
HRESULT status = S_OK;
IEnumWbemClassObject* enumerator = NULL;
IWbemClassObject* classObj = NULL;
IWbemClassObject* instanceObj = NULL;
const BSTR className = SysAllocString(TOASTER_METHOD_CLASS);
VARIANT pathVariable;
_bstr_t instancePath;
ULONG nbrObjsSought = 1;
ULONG nbrObjsReturned;
//
// Create an Enumeration object to enumerate the instances of the given class.
//
status = WbemServices->CreateInstanceEnum(className,
WBEM_FLAG_SHALLOW | WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY,
NULL,
&enumerator);
if (FAILED(status)) {
goto exit;
}
//
// Set authentication information for the interface.
//
status = SetInterfaceSecurity(enumerator, UserId, Password, DomainName);
if (FAILED(status)) {
goto exit;
}
//
// Get the class object for the method definition.
//
status = WbemServices->GetObject(className, 0, NULL, &classObj, NULL);
if (FAILED(status)) {
goto exit;
}
do {
//
// Get the instance object for each instance of the class.
//
status = enumerator->Next(WBEM_INFINITE,
nbrObjsSought,
&instanceObj,
&nbrObjsReturned);
if (status == WBEM_S_FALSE) {
status = S_OK;
break;
}
if (FAILED(status)) {
if (status == WBEM_E_INVALID_CLASS) {
printf("ERROR: Toaster driver may not be active on the system.\n");
}
goto exit;
}
//
// To obtain the object path of the object for which the method has to be
// executed, query the "__PATH" property of the WMI instance object.
//.........这里部分代码省略.........
示例14: SysAllocString
bool WMIVideoInfo::_queryPropertyWMI( const PVIQueryType queryType, const U32 adapterId, String *outValue )
{
if( mServices == NULL )
return false;
BSTR bstrWQL = SysAllocString(L"WQL");
BSTR bstrPath = SysAllocString(L"select * from Win32_VideoController");
IEnumWbemClassObject* enumerator;
// Use the IWbemServices pointer to make requests of WMI
HRESULT hr = mServices->ExecQuery(bstrWQL, bstrPath, WBEM_FLAG_FORWARD_ONLY, NULL, &enumerator);
if( FAILED( hr ) )
return false;
IWbemClassObject *adapter = NULL;
ULONG uReturned;
// Get the appropriate adapter.
for ( S32 i = 0; i <= adapterId; i++ )
{
hr = enumerator->Next(WBEM_INFINITE, 1, &adapter, &uReturned );
if ( FAILED( hr ) || uReturned == 0 )
{
enumerator->Release();
return false;
}
}
// Now get the property
VARIANT v;
hr = adapter->Get( smPVIQueryTypeToWMIString[queryType], 0, &v, NULL, NULL );
bool result = SUCCEEDED( hr );
if ( result )
{
switch( v.vt )
{
case VT_I4:
{
LONG longVal = v.lVal;
if( queryType == PVI_VRAM )
longVal = longVal >> 20; // Convert to megabytes
*outValue = String::ToString( (S32)longVal );
break;
}
case VT_UI4:
{
*outValue = String::ToString( (U32)v.ulVal );
break;
}
case VT_BSTR:
{
*outValue = String( v.bstrVal );
break;
}
case VT_LPSTR:
case VT_LPWSTR:
break;
}
}
// Cleanup
adapter->Release();
enumerator->Release();
return result;
}
示例15: OnEnumdisks
// **************************************************************************
//
// CAdvClientDlg::OnEnumdisks()
//
// Description:
// Enumerates all the disks on a machine. Demonstrates getting
// known properties directly.
//
// Parameters:
// None.
//
// Returns:
// Nothing.
//
// Globals accessed:
// None.
//
// Globals modified:
// None.
//
//===========================================================================
void CAdvClientDlg::OnEnumdisks()
{
// these are for enumerating.
HRESULT hRes;
ULONG uReturned;
IWbemClassObject *pStorageDev = NULL;
IEnumWbemClassObject *pEnumStorageDevs = NULL;
// these help get properties.
VARIANT pVal;
BSTR propName = NULL;
CString buf;
// here's what we're looking for.
// NOTE: make sure you specify a class that has a Provider()
// specified for it in the mof file. All other classes are
// abstract and wont be found using HMM_FLAG_SHALLOW. They
// will be found using HMM_FLAG_DEEP.
BSTR className = SysAllocString(L"Win32_LogicalDisk");
if (!className)
{
TRACE(_T("SysAllocString failed: not enough memory\n"));
return;
}
// setup for the "__RELPATH" system property.
propName = SysAllocString(L"__RELPATH");
if (!propName)
{
SysFreeString(className);
TRACE(_T("SysAllocString failed: not enough memory\n"));
return;
}
VariantInit(&pVal);
m_outputList.ResetContent();
m_outputList.AddString(_T("working..."));
TRACE(_T("Going for class\n"));
//---------------------------
// get the list of logical storage devices.
if ((hRes = m_pIWbemServices->CreateInstanceEnum(className,
WBEM_FLAG_SHALLOW,
NULL,
&pEnumStorageDevs)) == S_OK)
{
TRACE(_T("good enumerator\n"));
m_outputList.ResetContent();
uReturned = 1;
while(uReturned == 1)
{
//---------------------------
// enumerate through the resultset.
hRes = pEnumStorageDevs->Next(TIMEOUT,
1,
&pStorageDev,
&uReturned);
TRACE(_T("Next() %d:%s\n"), uReturned, ErrorString(hRes));
// was one found?
if((hRes == S_OK) && (uReturned == 1))
{
TRACE(_T("Got a device class\n"));
VariantClear(&pVal);
// Add the path
if (pStorageDev->Get(propName,
0L,
&pVal,
NULL, NULL) == S_OK)
{
//.........这里部分代码省略.........