本文整理汇总了C++中atl::CComPtr::ConnectServer方法的典型用法代码示例。如果您正苦于以下问题:C++ CComPtr::ConnectServer方法的具体用法?C++ CComPtr::ConnectServer怎么用?C++ CComPtr::ConnectServer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类atl::CComPtr
的用法示例。
在下文中一共展示了CComPtr::ConnectServer方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: UsingWMI
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
ATL::CComPtr<IWbemLocator> locator;
HRESULT hr = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER, IID_IWbemLocator, reinterpret_cast<void**>(&locator));
if (SUCCEEDED(hr))
{
ATL::CComPtr<IWbemServices> services;
hr = locator->ConnectServer(_bstr_t("\\\\.\\root\\cimv2"), NULL, NULL, NULL, 0, NULL, NULL, &services);
if (SUCCEEDED(hr))
{
//Execute the query
ATL::CComPtr<IEnumWbemClassObject> classObject;
hr = services->CreateInstanceEnum(_bstr_t("Win32_SerialPort"), WBEM_FLAG_RETURN_WBEM_COMPLETE, NULL, &classObject);
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;
ATL::CComPtr<IWbemClassObject> apObj[10];
hr = classObject->Next(WBEM_INFINITE, 10, reinterpret_cast<IWbemClassObject**>(apObj), &uReturned);
if (SUCCEEDED(hr))
{
for (ULONG n=0; n<uReturned; n++)
{
ATL::CComVariant 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
ATL::CComVariant 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
std::string szName(ATL::CW2A(varProperty2.bstrVal));
#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
}
}
}
}
}
}
}
}
}
return bSuccess;
}