本文整理汇总了C++中IWbemServices类的典型用法代码示例。如果您正苦于以下问题:C++ IWbemServices类的具体用法?C++ IWbemServices怎么用?C++ IWbemServices使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了IWbemServices类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: CoInitialize
/*
* Solution by: Simon Mourier
* From: http://stackoverflow.com/questions/5327203/how-to-access-cpus-heat-sensors
*
* For linking use: Wbemuuid.lib
* Running info: application must run under administrator privileges!
*/
double SystemInfo::DiagnosticMgr::getCpuTemp()
{
LONG result;
LPLONG pTemperature = &result;
*pTemperature = -1;
HRESULT ci = CoInitialize(NULL); // needs comdef.h
HRESULT hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE,
NULL, EOAC_NONE, NULL);
if (SUCCEEDED(hr))
{
IWbemLocator *pLocator; // needs Wbemidl.h & Wbemuuid.lib
hr = CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER , IID_IWbemLocator, (LPVOID*)&pLocator);
if (SUCCEEDED(hr))
{
IWbemServices *pServices;
BSTR ns = SysAllocString(L"root\\WMI");
hr = pLocator->ConnectServer(ns, NULL, NULL, NULL, 0, NULL, NULL, &pServices);
pLocator->Release();
SysFreeString(ns);
if (SUCCEEDED(hr))
{
BSTR query = SysAllocString(L"SELECT * FROM MSAcpi_ThermalZoneTemperature");
BSTR wql = SysAllocString(L"WQL");
IEnumWbemClassObject *pEnum;
hr = pServices->ExecQuery(wql, query, WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY, NULL, &pEnum);
SysFreeString(wql);
SysFreeString(query);
pServices->Release();
if (SUCCEEDED(hr))
{
IWbemClassObject *pObject;
ULONG returned;
hr = pEnum->Next(-1, 1, &pObject, &returned);
pEnum->Release();
if (SUCCEEDED(hr))
{
BSTR temp = SysAllocString(L"CurrentTemperature");
VARIANT v;
VariantInit(&v);
hr = pObject->Get(temp, 0, &v, NULL, NULL);
pObject->Release();
SysFreeString(temp);
if (SUCCEEDED(hr))
{
*pTemperature = V_I4(&v);
}
VariantClear(&v);
}
}
}
if (ci == S_OK)
{
CoUninitialize();
}
}
}
double tempInTensOfKelvins = (double)*pTemperature;
return (tempInTensOfKelvins > 0.0) ? ((double)*pTemperature / 10.0) - 273.15 : 0.0;
}
示例2: GetVideoCardDriverVersion
// Adapted mostly as-is from http://www.gamedev.net/topic/495075-how-to-retrieve-info-about-videocard/?view=findpost&p=4229170
// so credit goes to that post's author, and in turn, the author of the site mentioned in that post (which seems to be down?).
std::string GetVideoCardDriverVersion() {
std::string retvalue = "";
HRESULT hr;
hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (FAILED(hr)) {
return retvalue;
}
IWbemLocator *pIWbemLocator = NULL;
hr = CoCreateInstance(__uuidof(WbemLocator), NULL, CLSCTX_INPROC_SERVER,
__uuidof(IWbemLocator), (LPVOID *)&pIWbemLocator);
if (FAILED(hr)) {
CoUninitialize();
return retvalue;
}
BSTR bstrServer = SysAllocString(L"\\\\.\\root\\cimv2");
IWbemServices *pIWbemServices;
hr = pIWbemLocator->ConnectServer(bstrServer, NULL, NULL, 0L, 0L, NULL, NULL, &pIWbemServices);
if (FAILED(hr)) {
pIWbemLocator->Release();
SysFreeString(bstrServer);
CoUninitialize();
return retvalue;
}
hr = CoSetProxyBlanket(pIWbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE,
NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL,EOAC_DEFAULT);
BSTR bstrWQL = SysAllocString(L"WQL");
BSTR bstrPath = SysAllocString(L"select * from Win32_VideoController");
IEnumWbemClassObject* pEnum;
hr = pIWbemServices->ExecQuery(bstrWQL, bstrPath, WBEM_FLAG_FORWARD_ONLY, NULL, &pEnum);
ULONG uReturned;
VARIANT var;
IWbemClassObject* pObj = NULL;
if (!FAILED(hr)) {
hr = pEnum->Next(WBEM_INFINITE, 1, &pObj, &uReturned);
}
if (!FAILED(hr) && uReturned) {
hr = pObj->Get(L"DriverVersion", 0, &var, NULL, NULL);
if (SUCCEEDED(hr)) {
char str[MAX_PATH];
WideCharToMultiByte(CP_ACP, 0, var.bstrVal, -1, str, sizeof(str), NULL, NULL);
retvalue = str;
}
}
pEnum->Release();
SysFreeString(bstrPath);
SysFreeString(bstrWQL);
pIWbemServices->Release();
pIWbemLocator->Release();
SysFreeString(bstrServer);
CoUninitialize();
return retvalue;
}
示例3: MappingUsbstorToDeviceNumber
// (Usually internal) method to perform usb drive search
bool DeviceFinder::MappingUsbstorToDeviceNumber(std::map<std::string, int>& usbStorToDeviceMap)
{
HRESULT hr;
// Clear list of drive names
usbStorToDeviceMap.clear();
// Ensure pSvc is initialized
if (!Initialize()) { Log(0, "ERROR: Unable to initialize for usbstor-deviceNumber mapping.\n"); return false; }
// Use the IWbemServices pointer to make a WMI request for Win32_SerialPort
IEnumWbemClassObject* pEnumerator = NULL;
IWbemServices *pServices = (IWbemServices *)pSvc; // Cast required as the public API doesn't include the type information
if (pServices == NULL) { Log(0, "ERROR: Unable to get usbstor-deviceNumber mapping.\n"); return false; }
hr = pServices->ExecQuery(bstr_t("WQL"), bstr_t("SELECT PNPDeviceID, DeviceID FROM Win32_DiskDrive WHERE InterfaceType='USB'"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);
if (FAILED(hr)) { Log(0, "ERROR: Query for Win32_DiskDrive has failed: 0x%08x\n", hr); return false; }
// Get the data from the query
if (pEnumerator)
{
for (;;)
{
// Get next item in enumeration
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
// If no more items, exit loop
if (uReturn == 0) { break; }
// Get value of the "PNPDeviceID" property
VARIANT vtProp;
hr = pclsObj->Get(L"PNPDeviceID", 0, &vtProp, 0, 0);
char usbstorId[256] = { 0 };
wcstombs_s(NULL, usbstorId, vtProp.bstrVal, 255); // Convert to ASCII
VariantClear(&vtProp);
//cerr << "[DRIVE:USBSTOR] " << usbstorId << endl;
// Get value of the "DeviceID" property
hr = pclsObj->Get(L"DeviceID", 0, &vtProp, 0, 0);
char deviceId[256] = { 0 };
wcstombs_s(NULL, deviceId, vtProp.bstrVal, 255); // Convert to ASCII
unsigned int deviceNumber = GetDeviceNumber(vtProp.bstrVal);
VariantClear(&vtProp);
//cerr << "[DRIVE:DEVICEID] " << deviceId << endl;
//cerr << "[DRIVE:DEVICENUMBER] " << deviceNumber << endl;
//DiskDriveToLogicalDrive(deviceId);
#ifdef DEBUG_PRINT
Log(3, "[USBSTOR->DEVICEID->DEVICENUMBER] %s -> %s -> %u\n", usbstorId, deviceId, deviceNumber);
#endif
usbStorToDeviceMap[usbstorId] = deviceNumber;
pclsObj->Release();
}
pEnumerator->Release();
}
return true;
}
示例4: 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;
}
示例5: 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;
}
示例6:
JNIEXPORT void JNICALL Java_ru_acs_monitoring_utils_WMI_CloseHandle
(JNIEnv *env, jobject, jint nativeHandle)
{
try
{
// The handle is actually the IWbemServices pointer
IWbemServices *pSvc = (IWbemServices *)nativeHandle;
if (pSvc)
pSvc->Release();
}
catch (...)
{
// If this is a Java exception we threw, just let it through
// Otherwise it's an access violation or something: convert to Java exception
if (!env->ExceptionOccurred())
throwException(env, "Internal error");
}
}
示例7: OnQueryWMI
void QtWMI::OnQueryWMI()
{
CString strQuery;
strQuery.Format(L"select * from Win32_%s", ui.cboWMIClasses->currentText().toStdWString().c_str() );
IWbemLocator *pLoc = NULL;
IWbemServices *pSvc = NULL;
IEnumWbemClassObject* pEnumerator = NULL;
IWbemClassObject *pclsObj=NULL;
HRESULT hr=E_FAIL;
do
{
hr=CoCreateInstance(CLSID_WbemLocator,0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hr) ) break;
hr=pLoc->ConnectServer( _bstr_t(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pSvc);
if (FAILED(hr) ) break;
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) ) break;
hr=pSvc->ExecQuery( _bstr_t(L"WQL"), bstr_t(strQuery),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);
if (FAILED(hr) ) break;
ULONG uReturn=0;
variant_t vtProp;
vtProp.Clear();
SAFEARRAY* psaNames=NULL;
LONG nLower=0, nUpper=0;
_bstr_t PropName;
CString s;
while (pEnumerator)
{
pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if(!uReturn) break;
hr=pclsObj->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_FLAG_NONSYSTEM_ONLY, NULL, &psaNames);
if (hr==WBEM_S_NO_ERROR )
{
nLower=nUpper=0;
SafeArrayGetLBound(psaNames, 1, &nLower);
SafeArrayGetUBound(psaNames, 1, &nUpper);
ui.lbDebug->clear();
for (long i = nLower; i <= nUpper; i++)
{
SafeArrayGetElement(psaNames, &i, &PropName);
s.Format(L"%s", PropName);
hr = pclsObj->Get(s, 0, &vtProp, 0, 0);
if (hr==WBEM_S_NO_ERROR)
DisplayVariantValues(s, vtProp);
vtProp.Clear();
}
}
if (psaNames)
::SafeArrayDestroy(psaNames);
SAFE_RELEASE(pclsObj);
}
} while (0);
SAFE_RELEASE(pEnumerator);
SAFE_RELEASE(pclsObj);
SAFE_RELEASE(pSvc);
SAFE_RELEASE(pLoc);
}
示例8: CoInitialize
tstring CCliMgr::GetWmiInfo( LPCTSTR lpszClass,LPCTSTR lpszField)
{
tstring SysInfo,strtmp;
HRESULT ret;
ret = CoInitialize(NULL);
ret = CoInitializeSecurity( NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_PKT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
0
);
if (ret == S_OK || ret== RPC_E_TOO_LATE )
{
IWbemLocator * pIWbemLocator = NULL;
IWbemServices * pWbemServices = NULL;
IEnumWbemClassObject * pEnumObject = NULL;
BSTR bstrNamespace = (L"root\\cimv2");// 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息
if(CoCreateInstance (CLSID_WbemAdministrativeLocator, NULL,
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, IID_IUnknown ,(void**)&pIWbemLocator)
== S_OK)
{
if(pIWbemLocator->ConnectServer(
bstrNamespace, // Namespace
NULL, // Userid
NULL, // PW
NULL, // Locale
0, // flags
NULL, // Authority
NULL, // Context
&pWbemServices
) == S_OK)
{
HRESULT hRes;
_bstr_t strQuery = (L"Select * from Win32_OperatingSystem");
hRes = pWbemServices->ExecQuery(_bstr_t("WQL"), strQuery,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject);
if(hRes == S_OK)
{
ULONG uCount = 1, uReturned;
IWbemClassObject * pClassObject = NULL;
hRes = pEnumObject->Reset();
if(hRes == S_OK)
{
hRes = pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject, &uReturned);
if(hRes == S_OK)
{
strtmp = "操作系统的名称: "+_getWmiInfo(pClassObject,"Caption")+"\r\n";
SysInfo = SysInfo + strtmp;
}
if (pClassObject != NULL)
pClassObject->Release();
}
}
strQuery = (L"Select * from Win32_DiskDrive");
hRes = pWbemServices->ExecQuery(_bstr_t("WQL"), strQuery,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject);
if(hRes == S_OK)
{
ULONG uCount = 1, uReturned;
IWbemClassObject * pClassObject = NULL;
hRes = pEnumObject->Reset();
if(hRes == S_OK)
{
hRes = pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject, &uReturned);
if(hRes == S_OK)
{
strtmp = "硬盘的Model: "+_getWmiInfo(pClassObject,"Model")+"\r\n";
SysInfo = SysInfo + strtmp;
m_pLogger->WriteLog(LM_INFO, TEXT("硬盘的Model: %s"),_getWmiInfo(pClassObject,"Model").c_str());
}
if (pClassObject != NULL)
pClassObject->Release();
}
}
strQuery = (L"Select * from Win32_LogicalDisk");
hRes = pWbemServices->ExecQuery(_bstr_t("WQL"), strQuery,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject);
if(hRes == S_OK)
{
ULONG uCount = 1, uReturned;
IWbemClassObject * pClassObject = NULL;
hRes = pEnumObject->Reset();
if(hRes == S_OK)
{
while( pEnumObject->Next( WBEM_INFINITE,
uCount, &pClassObject, &uReturned) == S_OK )
{
if (atoi(_getWmiInfo(pClassObject,"DriveType").c_str()) != 3)
continue;
tstring str;
str =_getWmiInfo(pClassObject,"Name");
strtmp = "盘符: " +str+" ";
m_pLogger->WriteLog(LM_INFO, TEXT("盘符: %s"),str.c_str());
tstring drv;
drv += toupper(str[0]);
if(m_Hy.IsDriverProtected((char *)drv.c_str()))
{
strtmp = strtmp+ "还原状态: 保护 ";
m_pLogger->WriteLog(LM_INFO, TEXT("还原状态: 保护 "));
//.........这里部分代码省略.........
示例9: main
int main(int argc, char **argv)
{
HRESULT hres;
// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
hres = CoInitializeSecurity(
nullptr,
-1, // COM authentication
nullptr, // Authentication services
nullptr, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
nullptr, // Authentication info
EOAC_NONE, // Additional capabilities
nullptr // Reserved
);
if (FAILED(hres))
{
cout << "Failed to initialize security. Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
IWbemLocator *pLoc = nullptr;
hres = CoCreateInstance(
CLSID_WbemLocator,
nullptr,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, reinterpret_cast<LPVOID *>(&pLoc));
if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object."
<< " Err code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
IWbemServices *pSvc = nullptr;
// Connect to the root\cimv2 namespace with
// the current user and obtain pointer pSvc
// to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
nullptr, // User name. NULL = current user
nullptr, // User password. NULL = current
nullptr, // Locale. NULL indicates current
NULL, // Security flags.
nullptr, // Authority (for example, Kerberos)
nullptr, // Context object
&pSvc // pointer to IWbemServices proxy
);
if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
//cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;
// Step 5: --------------------------------------------------
// Set security levels on the proxy -------------------------
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
nullptr, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
//.........这里部分代码省略.........
示例10: TimeLimitGetHourlyRestrictions
//-------------------------------------------------------------------------------------
// Name: TimeLimitGetHourlyRestrictions()
//-------------------------------------------------------------------------------------
HRESULT TimeLimitGetHourlyRestrictions( HourlyRestrictions* pRestrictions )
{
// initialize the structure to all hours allowed
for( int i = 0; i < 7; ++i )
{
pRestrictions->days[i] = 0x00ffffff;
}
HRESULT hr = S_OK;
if( NULL == pRestrictions )
{
hr = E_INVALIDARG;
}
hr = Init();
if( SUCCEEDED( hr ) )
{
IWbemServices* pWmi = NULL;
hr = ConnectToWpc( &pWmi );
if( SUCCEEDED( hr ) )
{
// get the user settings instance
IWbemClassObject* pSettings = NULL;
PWSTR pwszSID = NULL;
hr = GetSidStringForCurrentUser( &pwszSID );
if( SUCCEEDED( hr ) )
{
hr = GetUserSettings( pWmi, pwszSID, &pSettings );
if( SUCCEEDED( hr ) )
{
// determine if time restrictiosn are enabled
BOOL fTimeRestrictions = FALSE;
hr = TimeRestrictionsEnabled( pSettings, &fTimeRestrictions );
if( SUCCEEDED( hr ) && fTimeRestrictions )
{
VARIANT var;
VariantInit( &var );
// get the actual logon hours data from the user settings
hr = pSettings->Get( L"LogonHours", 0, &var, NULL, NULL );
if( SUCCEEDED( hr ) && var.vt == ( VT_I4 | VT_ARRAY ) )
{
long lUpper = 0;
long lLower = 0;
hr = SafeArrayGetUBound( var.parray, 1, &lUpper );
if( SUCCEEDED( hr ) )
{
hr = SafeArrayGetLBound( var.parray, 1, &lLower );
if( SUCCEEDED( hr ) )
{
long* pData;
hr = SafeArrayAccessData( var.parray, ( void HUGEP** )( &pData ) );
if( SUCCEEDED( hr ) )
{
for( long i = lLower; i <= lUpper; ++i )
{
pRestrictions->days[i] = pData[i];
}
SafeArrayUnaccessData( var.parray );
}
}
}
}
VariantClear( &var );
}
if( FALSE == fTimeRestrictions )
{
hr = E_FAIL;
}
// release the settings instance
pSettings->Release();
}
LocalFree( pwszSID );
pwszSID = NULL;
}
// release the WMI connection
pWmi->Release();
}
}
return hr;
}
示例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: SetupForIsXInputDevice
//-----------------------------------------------------------------------------
// Enum each PNP device using WMI and check each device ID to see if it contains
// "IG_" (ex. "VID_045E&PID_028E&IG_00"). If it does, then itӳ an XInput device
// Unfortunately this information can not be found by just using DirectInput.
// Checking against a VID/PID of 0x028E/0x045E won't find 3rd party or future
// XInput devices.
//
// This function stores the list of xinput devices in a linked list
// at g_pXInputDeviceList, and IsXInputDevice() searchs that linked list
//-----------------------------------------------------------------------------
HRESULT SetupForIsXInputDevice()
{
IWbemServices* pIWbemServices = NULL;
IEnumWbemClassObject* pEnumDevices = NULL;
IWbemLocator* pIWbemLocator = NULL;
IWbemClassObject* pDevices[20] = {0};
BSTR bstrDeviceID = NULL;
BSTR bstrClassName = NULL;
BSTR bstrNamespace = NULL;
DWORD uReturned = 0;
bool bCleanupCOM = false;
UINT iDevice = 0;
VARIANT var;
HRESULT hr;
// CoInit if needed
hr = CoInitialize( NULL );
bCleanupCOM = SUCCEEDED( hr );
// Create WMI
hr = CoCreateInstance( __uuidof( WbemLocator ),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof( IWbemLocator ),
( LPVOID* )&pIWbemLocator );
if( VP_FAILED( hr ) || pIWbemLocator == NULL )
goto LCleanup;
// Create BSTRs for WMI
bstrNamespace = SysAllocString( L"\\\\.\\root\\cimv2" ); if( bstrNamespace == NULL ) goto LCleanup;
bstrDeviceID = SysAllocString( L"DeviceID" ); if( bstrDeviceID == NULL ) goto LCleanup;
bstrClassName = SysAllocString( L"Win32_PNPEntity" ); if( bstrClassName == NULL ) goto LCleanup;
// Connect to WMI
hr = pIWbemLocator->ConnectServer( bstrNamespace, NULL, NULL, 0L,
0L, NULL, NULL, &pIWbemServices );
if( VP_FAILED( hr ) || pIWbemServices == NULL )
goto LCleanup;
// Switch security level to IMPERSONATE
CoSetProxyBlanket( pIWbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL,
RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, 0 );
// Get list of Win32_PNPEntity devices
hr = pIWbemServices->CreateInstanceEnum( bstrClassName, 0, NULL, &pEnumDevices );
if( VP_FAILED( hr ) || pEnumDevices == NULL )
goto LCleanup;
// Loop over all devices
for(; ; )
{
// Get 20 at a time
hr = pEnumDevices->Next( 10000, 20, pDevices, &uReturned );
if( VP_FAILED( hr ) )
goto LCleanup;
if( uReturned == 0 )
break;
for( iDevice = 0; iDevice < uReturned; iDevice++ )
{
// For each device, get its device ID
hr = pDevices[iDevice]->Get( bstrDeviceID, 0L, &var, NULL, NULL );
if( SUCCEEDED( hr ) && var.vt == VT_BSTR && var.bstrVal != NULL )
{
// Check if the device ID contains "IG_". If it does, then itӳ an XInput device
// Unfortunately this information can not be found by just using DirectInput
if( wcsstr( var.bstrVal, L"IG_" ) )
{
// If it does, then get the VID/PID from var.bstrVal
DWORD dwPid = 0, dwVid = 0;
WCHAR* strVid = wcsstr( var.bstrVal, L"VID_" );
if( strVid && swscanf( strVid, L"VID_%4X", &dwVid ) != 1 )
dwVid = 0;
WCHAR* strPid = wcsstr( var.bstrVal, L"PID_" );
if( strPid && swscanf( strPid, L"PID_%4X", &dwPid ) != 1 )
dwPid = 0;
DWORD dwVidPid = MAKELONG( dwVid, dwPid );
// Add the VID/PID to a linked list
XINPUT_DEVICE_NODE* pNewNode = new XINPUT_DEVICE_NODE;
if( pNewNode )
{
pNewNode->dwVidPid = dwVidPid;
pNewNode->pNext = g_pXInputDeviceList;
g_pXInputDeviceList = pNewNode;
}
}
}
SAFE_RELEASE( pDevices[iDevice] );
//.........这里部分代码省略.........
示例13: getclock
static unsigned int getclock(void)
{
unsigned int clock;
#ifdef KONOHA_ON_MACOSX
int cpu_sels[2] = { CTL_HW, HW_CPU_FREQ };
size_t len = sizeof(int);
sysctl(cpu_sels, 2, &clock, &len, NULL, 0);
clock = (unsigned int) clock / (1000 * 1000);
#elif defined(KONOHA_ON_WINDOWS)
HRESULT hres;
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
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
);
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
IWbemServices *pSvc = NULL;
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices proxy
);
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_Processor"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
while (pEnumerator) {
HRESULT hr = pEnumerator->Next(
WBEM_INFINITE,
1,
&pclsObj,
&uReturn
);
if (uReturn == 0) { break; }
VARIANT vtProp;
hr = pclsObj->Get(L"MaxClockSpeed", 0, &vtProp, 0, 0);
clock = (unsigned int)vtProp.bstrVal;
VariantClear(&vtProp);
pclsObj->Release();
}
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
CoUninitialize();
#elif defined(KONOHA_ON_LINUX)
char buf[64] = {'\0'}, *data = buf;
const char *cpumhz = "cpu MHz";
size_t len = strlen(cpumhz);
FILE *fp = fopen("/proc/cpuinfo","r");
while (fgets( buf, 64, fp ) != NULL){
if ( strncmp(buf, cpumhz, len) == 0){
data = strstr(buf, cpumhz);
break;
}
}
while (!isdigit(*data)) {
data++;
}
clock = (unsigned int) atoi(data) / 1000;
fclose(fp);
#else
TODO();
#endif
return clock;
}
示例14: CoCreateInstance
BOOL CEnumerateSerial::UsingWMI(CSimpleArray<UINT>& ports, CSimpleArray<CString>& friendlyNames)
#endif
{
//Make sure we clear out any elements which may already be in the array(s)
#if defined CENUMERATESERIAL_USE_STL
ports.clear();
friendlyNames.clear();
#else
ports.RemoveAll();
friendlyNames.RemoveAll();
#endif
//What will be the return value
BOOL bSuccess = FALSE;
//Create the WBEM locator
IWbemLocator* pLocator = NULL;
HRESULT hr = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER, IID_IWbemLocator, reinterpret_cast<void**>(&pLocator));
if (SUCCEEDED(hr))
{
IWbemServices* pServices = NULL;
hr = pLocator->ConnectServer(_bstr_t("\\\\.\\root\\cimv2"), NULL, NULL, NULL, 0, NULL, NULL, &pServices);
if (SUCCEEDED(hr))
{
//Execute the query
IEnumWbemClassObject* pClassObject = NULL;
hr = pServices->CreateInstanceEnum(_bstr_t("Win32_SerialPort"), WBEM_FLAG_RETURN_WBEM_COMPLETE, NULL, &pClassObject);
if (SUCCEEDED(hr))
{
bSuccess = TRUE;
//Now enumerate all the ports
hr = WBEM_S_NO_ERROR;
//Final Next will return WBEM_S_FALSE
while (hr == WBEM_S_NO_ERROR)
{
ULONG uReturned = 0;
IWbemClassObject* apObj[10];
memset(apObj, 0, sizeof(apObj));
hr = pClassObject->Next(WBEM_INFINITE, 10, reinterpret_cast<IWbemClassObject**>(apObj), &uReturned);
if (SUCCEEDED(hr))
{
for (ULONG n=0; n<uReturned; n++)
{
VARIANT varProperty1;
VariantInit(&varProperty1);
HRESULT hrGet = apObj[n]->Get(L"DeviceID", 0, &varProperty1, NULL, NULL);
if (SUCCEEDED(hrGet) && (varProperty1.vt == VT_BSTR) && (wcslen(varProperty1.bstrVal) > 3))
{
//If it looks like "COMX" then add it to the array which will be returned
if ((_wcsnicmp(varProperty1.bstrVal, L"COM", 3) == 0) && IsNumeric(&(varProperty1.bstrVal[3]), TRUE))
{
//Work out the port number
int nPort = _wtoi(&(varProperty1.bstrVal[3]));
#if defined CENUMERATESERIAL_USE_STL
ports.push_back(nPort);
#else
ports.Add(nPort);
#endif
//Also get the friendly name of the port
VARIANT varProperty2;
VariantInit(&varProperty2);
if (SUCCEEDED(apObj[n]->Get(L"Name", 0, &varProperty2, NULL, NULL)) && (varProperty2.vt == VT_BSTR))
{
#if defined CENUMERATESERIAL_USE_STL
#if defined _UNICODE
std::wstring szName(varProperty2.bstrVal);
#else
CAutoHeapAlloc szAsciiValue;
int nLengthA = WideCharToMultiByte(CP_ACP, 0, varProperty2.bstrVal, -1, NULL, 0, NULL, NULL);
if (nLengthA)
{
if (szAsciiValue.Allocate(nLengthA))
WideCharToMultiByte(CP_ACP, 0, varProperty2.bstrVal, -1, static_cast<LPSTR>(szAsciiValue.m_pData), nLengthA, NULL, NULL);
}
std::string szName(static_cast<LPSTR>(szAsciiValue.m_pData));
#endif
friendlyNames.push_back(szName);
#else
friendlyNames.Add(CString(varProperty2.bstrVal));
#endif
}
else
{
#if defined CENUMERATESERIAL_USE_STL
friendlyNames.push_back(_T(""));
#else
friendlyNames.Add(_T(""));
#endif
}
//Free up the variant;
VariantClear(&varProperty2);
}
}
//Free up the variant;
//.........这里部分代码省略.........
示例15: 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;
}