本文整理汇总了C++中IWbemLocator::ConnectServer方法的典型用法代码示例。如果您正苦于以下问题:C++ IWbemLocator::ConnectServer方法的具体用法?C++ IWbemLocator::ConnectServer怎么用?C++ IWbemLocator::ConnectServer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IWbemLocator
的用法示例。
在下文中一共展示了IWbemLocator::ConnectServer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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;
}
示例2: 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;
}
示例3: Initialize
bool TDBWMI::Initialize(PGLOBAL g)
{
if (Init)
return false;
// Initialize COM.
Res = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (FAILED(Res)) {
sprintf(g->Message, "Failed to initialize COM library. "
"Error code = %p", Res);
return true; // Program has failed.
} // endif Res
// Obtain the initial locator to Windows Management
// on a particular host computer.
IWbemLocator *loc; // Initial Windows Management locator
Res = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID*) &loc);
if (FAILED(Res)) {
sprintf(g->Message, "Failed to create Locator. "
"Error code = %p", Res);
CoUninitialize();
return true; // Program has failed.
} // endif Res
// Connect to the specified namespace with the
// current user and obtain pointer to Svc
// to make IWbemServices calls.
Res = loc->ConnectServer(_bstr_t(Nspace),
NULL, NULL,0, NULL, 0, 0, &Svc);
if (FAILED(Res)) {
sprintf(g->Message, "Could not connect. Error code = %p", Res);
loc->Release();
CoUninitialize();
return true; // Program has failed.
} // endif hres
loc->Release(); // Not used anymore
// Set the IWbemServices proxy so that impersonation
// of the user (client) occurs.
Res = CoSetProxyBlanket(Svc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE,
NULL, RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
if (FAILED(Res)) {
sprintf(g->Message, "Could not set proxy. Error code = 0x", Res);
Svc->Release();
CoUninitialize();
return true; // Program has failed.
} // endif Res
Init = true;
return false;
} // end of Initialize
示例4: GetNamespace
HRESULT GetNamespace(__in PCWSTR pObjectPath, __out IWbemServices** ppNamespace)
{
HRESULT hr;
IWbemLocator *pLocator = NULL;
IWbemServices *pNamespaceLocal = NULL;
hr = CoCreateInstance(CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
(LPVOID *) &pLocator);
if(HB_FAILED(hr)) {
goto cleanexit;
}
hr = pLocator->ConnectServer(_bstr_t(pObjectPath), // Object path of WMI namespace
NULL, // User name
NULL, // User password
0, // Locale
NULL, // Security flags
0, // Authority
0, // Context object
&pNamespaceLocal
);
if(HB_FAILED(hr)) {
goto cleanexit;
}
hr = CoSetProxyBlanket(pNamespaceLocal,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE
);
if(HB_FAILED(hr)) {
goto cleanexit;
}
*ppNamespace = pNamespaceLocal;
pNamespaceLocal = NULL;
cleanexit:
HB_SAFE_RELEASE(pLocator);
HB_SAFE_RELEASE(pNamespaceLocal);
return hr;
}
示例5: Open
HRESULT WMI::Open(LPCTSTR machine, LPCTSTR user, LPCTSTR pass)
{
IWbemLocator *locator;
wchar_t path[MAX_PATH];
if (wbem) {
result = S_OK;
return result;
}
result =
CoInitializeSecurity(NULL, //Security Descriptor
-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
result = CoCreateInstance(CLSID_WbemLocator,
NULL, /* IUnknown */
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
(LPVOID *)&locator);
if (FAILED(result)) {
return result;
}
if (machine == NULL) {
machine = L".";
}
wsprintf(path, L"\\\\%S\\ROOT\\CIMV2", machine);
result = locator->ConnectServer(bstr_t(path), //Object path of WMI namespace
bstr_t(user), //User name. NULL = current user
bstr_t(pass), //User password. NULL = current
NULL, //Locale. NULL indicates current
0, //Security flags
NULL, //Authority (e.g. Kerberos)
NULL, //Context object
&wbem); //pointer to IWbemServices proxy
locator->Release();
return result;
}
示例6: ConnectToETWNameSpace
HRESULT CConsumer::ConnectToETWNameSpace(BSTR bstrNameSpace)
{
HRESULT hr = S_OK;
IWbemLocator *pLocator = NULL;
hr = CoInitialize(0);
hr = CoCreateInstance(__uuidof(WbemLocator),
0,
CLSCTX_INPROC_SERVER,
__uuidof(IWbemLocator),
(LPVOID*) &pLocator);
if(FAILED(hr))
{
wprintf(L"CoCreateInstance failed with 0x%x\r\n", hr);
goto cleanup;
}
hr = pLocator->ConnectServer(bstrNameSpace,
NULL,NULL,NULL,0L,NULL,NULL,&g_pServices);
if(FAILED(hr))
{
wprintf(L"Connect toServer Failed with 0x%x\r\n",hr);
goto cleanup;
}
hr = CoSetProxyBlanket(g_pServices,
RPC_C_AUTHN_WINNT,RPC_C_AUTHN_NONE,
NULL,
RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE,
NULL, EOAC_NONE);
if(FAILED(hr))
{
wprintf(L"CoSetProxBlanket Failed with 0x%x\r\n", hr);
g_pServices->Release();
g_pServices = NULL;
}
cleanup:
if(pLocator)
pLocator->Release();
return hr;
}
示例7: WpcuWmiConnect
// Connect to specified namespace in WMI
HRESULT WpcuWmiConnect(PCWSTR pcszWpcNamespace, IWbemServices** ppiWmiServices)
{
HRESULT hr = E_INVALIDARG;
if (pcszWpcNamespace && ppiWmiServices)
{
// Obtain the initial locator to WMI
IWbemLocator* pLocator = NULL;
hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLocator);
// Set up to connect to the WPC namespace in WMI on the local machine
if (SUCCEEDED(hr))
{
BSTR bstrWpcNamespace = SysAllocString(pcszWpcNamespace);
if (bstrWpcNamespace == NULL)
{
hr = E_OUTOFMEMORY;
}
else
{
// Connect to the specified namespace with
// the current user and obtain pointer
// to make IWbemServices calls.
hr = pLocator->ConnectServer(
bstrWpcNamespace, // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
NULL, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
NULL, // Context object
ppiWmiServices // pointer to IWbemServices proxy
);
SysFreeString(bstrWpcNamespace);
}
pLocator->Release();
}
}
return(hr);
}
示例8: 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;
}
示例9: InitWMI
PWMIUT InitWMI(PGLOBAL g, char *nsp, char *classname)
{
IWbemLocator *loc;
char *p;
HRESULT res;
PWMIUT wp = (PWMIUT)PlugSubAlloc(g, NULL, sizeof(WMIUTIL));
if (trace)
htrc("WMIColumns class %s space %s\n", SVP(classname), SVP(nsp));
/*********************************************************************/
/* Set default values for the namespace and class name. */
/*********************************************************************/
if (!nsp)
nsp = "root\\cimv2";
if (!classname) {
if (!stricmp(nsp, "root\\cimv2"))
classname = "ComputerSystemProduct";
else if (!stricmp(nsp, "root\\cli"))
classname = "Msft_CliAlias";
else {
strcpy(g->Message, "Missing class name");
return NULL;
} // endif classname
} // endif classname
/*********************************************************************/
/* Initialize WMI. */
/*********************************************************************/
//res = CoInitializeEx(NULL, COINIT_MULTITHREADED);
res = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (FAILED(res)) {
sprintf(g->Message, "Failed to initialize COM library. "
"Error code = %p", res);
return NULL;
} // endif res
#if 0 // irrelevant for a DLL
res = CoInitializeSecurity(NULL, -1, NULL, NULL,
RPC_C_AUTHN_LEVEL_CONNECT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL, EOAC_NONE, NULL);
if (res != RPC_E_TOO_LATE && FAILED(res)) {
sprintf(g->Message, "Failed to initialize security. "
"Error code = %p", res);
CoUninitialize();
return NULL;
} // endif Res
#endif // 0
res = CoCreateInstance(CLSID_WbemLocator, NULL,
CLSCTX_INPROC_SERVER, IID_IWbemLocator,
(void**) &loc);
if (FAILED(res)) {
sprintf(g->Message, "Failed to create Locator. "
"Error code = %p", res);
CoUninitialize();
return NULL;
} // endif res
res = loc->ConnectServer(_bstr_t(nsp),
NULL, NULL, NULL, 0, NULL, NULL, &wp->Svc);
if (FAILED(res)) {
sprintf(g->Message, "Could not connect. Error code = %p", res);
loc->Release();
CoUninitialize();
return NULL;
} // endif res
loc->Release();
if (trace)
htrc("Successfully connected to namespace.\n");
/*********************************************************************/
/* Perform a full class object retrieval. */
/*********************************************************************/
p = (char*)PlugSubAlloc(g, NULL, strlen(classname) + 7);
if (strchr(classname, '_'))
strcpy(p, classname);
else
strcat(strcpy(p, "Win32_"), classname);
res = wp->Svc->GetObject(bstr_t(p), 0, 0, &wp->Cobj, 0);
if (FAILED(res)) {
sprintf(g->Message, "failed GetObject %s in %s\n", classname, nsp);
wp->Svc->Release();
wp->Svc = NULL; // MUST be set to NULL (why?)
return NULL;
} // endif res
return wp;
} // end of InitWMI
示例10: main
int main(int argc, char **argv)
{
HRESULT hres;
// Initialize COM.
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. "
<< "Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
// Initialize
hres = CoInitializeSecurity(
NULL,
-1, // COM negotiates service
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (FAILED(hres))
{
cout << "Failed to initialize security. "
<< "Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // 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))
{
cout << "Failed to create IWbemLocator object. "
<< "Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
IWbemServices *pSvc = 0;
// 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"), // WMI namespace
NULL, // User name
NULL, // User password
0, // Locale
NULL, // Security flags
0, // Authority
0, // Context object
&pSvc // 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;
// 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))
{
//.........这里部分代码省略.........
示例11: WMI_GetMacAddress
int MyMacAddress::WMI_GetMacAddress(int iQueryType, int iSize)
{
ZeroMemory(PermanentAddress, sizeof(PermanentAddress));
ZeroMemory(MACAddress, sizeof(MACAddress));
HRESULT hres; int iTotal = 0;
if( (iQueryType < 0) || (iQueryType >= sizeof(szWQLQuery)/sizeof(T_WQL_QUERY)) )
{
return -1;
}
hres = CoInitializeEx( NULL, COINIT_MULTITHREADED);
if( FAILED(hres) )
{
return -2;
}
hres = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
if( FAILED(hres))
{
CoUninitialize();
return -2;
}
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER, IID_IWbemLocator, reinterpret_cast <LPVOID*>(&pLoc));
if( FAILED(hres))
{
CoUninitialize();
return -2;
}
IWbemServices *pSvc = NULL;
hres = pLoc->ConnectServer(_bstr_t( L"ROOT\\CIMV2" ), NULL, NULL, NULL, 0, NULL, NULL, &pSvc);
if( FAILED(hres))
{
pLoc->Release();
CoUninitialize();
return -2;
}
// 设置请求代理的安全级别
hres = 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(hres))
{
pSvc->Release();
pLoc->Release();
CoUninitialize();
return -2;
}
IEnumWbemClassObject *pEnumerator = NULL;
hres = pSvc->ExecQuery(bstr_t("WQL"), bstr_t( szWQLQuery[iQueryType].szSelect), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);
if(FAILED(hres))
{
pSvc->Release();
pLoc->Release();
CoUninitialize();
return -3;
}
while(pEnumerator)
{
IWbemClassObject *pclsObj = NULL;
ULONG uReturn = 0;
if(iTotal >= iSize)
{
break;
}
pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if( uReturn == 0 )
{
break;
}
VARIANT vtProperty;
TCHAR szProperty[128];
// 获取网卡范例ID
VariantInit( &vtProperty);
pclsObj->Get( szWQLQuery[iQueryType].szProperty, 0, &vtProperty, NULL, NULL );
StringCchCopy( szProperty, sizeof(szProperty)/sizeof(TCHAR), W2T(vtProperty.bstrVal) );
VariantClear( &vtProperty );
if( GetPNPDeviceID( szProperty))
{
iTotal++;
}
pclsObj->Release();
}
// 释放资源
pEnumerator->Release();
pSvc->Release();
pLoc->Release();
CoUninitialize();
return iTotal;
}
示例12: CoInitialize
/*
================
XInputChecker::XInputChecker
================
*/
XInputChecker::XInputChecker() {
// CoInit if needed
HRESULT hr = CoInitialize( OG_NULL );
bool bCleanupCOM = SUCCEEDED( hr );
// Create WMI
IWbemLocator* pIWbemLocator = OG_NULL;
hr = CoCreateInstance( __uuidof( WbemLocator ), OG_NULL, CLSCTX_INPROC_SERVER, __uuidof( IWbemLocator ), ( LPVOID* )&pIWbemLocator );
if( FAILED( hr ) || pIWbemLocator == OG_NULL ) {
if( bCleanupCOM )
CoUninitialize();
return;
}
IEnumWbemClassObject* pEnumDevices = OG_NULL;
IWbemServices* pIWbemServices = OG_NULL;
IWbemClassObject* pDevices[20] = {0};
BSTR bstrDeviceID = OG_NULL;
BSTR bstrClassName = OG_NULL;
BSTR bstrNamespace = OG_NULL;
// Do just once
do {
bstrNamespace = SysAllocString( L"\\\\.\\root\\cimv2" ); if( bstrNamespace == OG_NULL ) break;
bstrDeviceID = SysAllocString( L"DeviceID" ); if( bstrDeviceID == OG_NULL ) break;
bstrClassName = SysAllocString( L"Win32_PNPEntity" ); if( bstrClassName == OG_NULL ) break;
// Connect to WMI
hr = pIWbemLocator->ConnectServer( bstrNamespace, OG_NULL, OG_NULL, 0L, 0L, OG_NULL, OG_NULL, &pIWbemServices );
if( FAILED( hr ) || pIWbemServices == OG_NULL )
break;
// Switch security level to IMPERSONATE
CoSetProxyBlanket( pIWbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, OG_NULL,
RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, OG_NULL, 0 );
// Get list of Win32_PNPEntity devices
hr = pIWbemServices->CreateInstanceEnum( bstrClassName, 0, OG_NULL, &pEnumDevices );
if( FAILED( hr ) || pEnumDevices == OG_NULL )
break;
// Loop over all devices
DWORD uReturned = 0;
VARIANT var;
for( ; ; ) {
// Get 20 at a time
hr = pEnumDevices->Next( 10000, 20, pDevices, &uReturned );
if( FAILED( hr ) || uReturned == 0 )
break;
for( int i = 0; i < uReturned; i++ ) {
// For each device, get its device ID
hr = pDevices[i]->Get( bstrDeviceID, 0L, &var, OG_NULL, OG_NULL );
if( SUCCEEDED( hr ) && var.vt == VT_BSTR && var.bstrVal != OG_NULL ) {
// Check if the device ID contains "IG_". If it does, then it’s 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;
listIds.Alloc() = MAKELONG( dwVid, dwPid );
}
}
SAFE_RELEASE( pDevices[i] );
}
}
}
while( 0 ); // Do just once
if( bstrNamespace )
SysFreeString( bstrNamespace );
if( bstrDeviceID )
SysFreeString( bstrDeviceID );
if( bstrClassName )
SysFreeString( bstrClassName );
for( int i=0; i<20; i++ )
SAFE_RELEASE( pDevices[i] );
SAFE_RELEASE( pEnumDevices );
SAFE_RELEASE( pIWbemLocator );
SAFE_RELEASE( pIWbemServices );
if( bCleanupCOM )
CoUninitialize();
}
示例13: EnumSerialFromWMI
void EnumSerialFromWMI(std::vector<int> &ports, std::vector<std::string> &friendlyNames)
{
ports.clear();
friendlyNames.clear();
HRESULT hr;
IWbemLocator *pLocator = NULL;
IWbemServices *pServicesSystem = NULL;
hr = CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&pLocator);
if (FAILED(hr))
{
return;
}
hr = pLocator->ConnectServer(L"root\\CIMV2", NULL, NULL, NULL, 0, NULL, NULL, &pServicesSystem);
if (pServicesSystem == NULL)
{
pLocator->Release();
return;
}
int dindex = 0;
std::string query = "SELECT * FROM Win32_PnPEntity WHERE ClassGuid=\"{4d36e978-e325-11ce-bfc1-08002be10318}\"";
IEnumWbemClassObject* pEnumerator = NULL;
hr = pServicesSystem->ExecQuery(L"WQL", bstr_t(query.c_str()), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);
if (!FAILED(hr))
{
// Get the data from the query
IWbemClassObject *pclsObj = NULL;
while (pEnumerator)
{
ULONG uReturn = 0;
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if (FAILED(hr) || (0 == uReturn))
{
break;
}
VARIANT vtProp;
VariantInit(&vtProp);
HRESULT hrGet = pclsObj->Get(L"Name", 0, &vtProp, NULL, NULL);
if (SUCCEEDED(hrGet) && (vtProp.vt == VT_BSTR) && (wcslen(vtProp.bstrVal) > 3))
{
std::string itemName = _bstr_t(vtProp.bstrVal);
VariantClear(&vtProp);
size_t spos = itemName.find("(COM");
if (spos != std::string::npos)
{
std::string tmpstr = itemName.substr(spos + 4);
spos = tmpstr.find(")");
if (spos != std::string::npos)
{
tmpstr = tmpstr.substr(0, spos);
int nPort = atoi(tmpstr.c_str());
ports.push_back(nPort);
friendlyNames.push_back(itemName);
}
}
}
pclsObj->Release();
}
pEnumerator->Release();
}
pServicesSystem->Release();
pLocator->Release();
}
示例14: 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
//.........这里部分代码省略.........
示例15: FetchSMBiosDataByCom
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) )
//.........这里部分代码省略.........