枚举服务
WpdServicesApiSample 应用程序包含的代码演示了应用程序如何枚举当前连接到计算机的任何设备上找到的所有联系人服务。
当用户在命令行中选择选项“0”时,应用程序将调用在 ServiceEnumeration.cpp 模块中找到的 EnumerateContactsServices 方法。 此方法显示支持“联系人”服务的所有已连接设备的列表。
例如,如果 WpdServiceSampleDriver 是唯一安装的设备,则应用程序将返回三个数据字段:友好名称 (“示例设备”) 、制造商 (“Windows 可移植设备组”) ,以及说明 (“Contacts Service Device 2000”) 。
EnumerateContactsServices 方法完成以下任务:
- 创建 IPortableDeviceManager 接口来处理已安装设备的枚举。
- 创建 IPortableDeviceServiceManager 接口来处理每个设备上的服务的枚举。
- 循环访问已安装的设备,搜索“联系人”服务,并显示支持此服务的任何设备的设备信息。
以下代码演示 了 EnumerateContactsServices 方法。
// Enumerates all Contacts Services, displaying the associated device of each service.
void EumerateContactsServices(CAtlArray<PWSTR>& ContactsServicePnpIDs)
{
HRESULT hr = S_OK;
DWORD cPnpDeviceIDs = 0;
PWSTR* pPnpDeviceIDs = NULL;
CComPtr<IPortableDeviceManager> pPortableDeviceManager;
CComPtr<IPortableDeviceServiceManager> pServiceManager;
// CoCreate the IPortableDeviceManager interface to enumerate
// portable devices and to get information about them.
hr = CoCreateInstance(CLSID_PortableDeviceManager,
NULL,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&pPortableDeviceManager));
if (FAILED(hr))
{
printf("! Failed to CoCreateInstance CLSID_PortableDeviceManager, hr = 0x%lx\n",hr);
}
// Retrieve the IPortableDeviceServiceManager interface to enumerate device services.
if (SUCCEEDED(hr))
{
hr = pPortableDeviceManager->QueryInterface(IID_PPV_ARGS(&pServiceManager));
if (FAILED(hr))
{
printf("! Failed to QueryInterface IID_IPortableDeviceServiceManager, hr = 0x%lx\n",hr);
}
}
if (SUCCEEDED(hr))
{
// First, pass NULL as the PWSTR array pointer to get the total number
// of devices found on the system.
hr = pPortableDeviceManager->GetDevices(NULL, &cPnpDeviceIDs);
if (FAILED(hr))
{
printf("! Failed to get number of devices on the system, hr = 0x%lx\n",hr);
}
if (SUCCEEDED(hr) && (cPnpDeviceIDs > 0))
{
// Second, allocate an array to hold the PnPDeviceID strings returned from
// the IPortableDeviceManager::GetDevices method
pPnpDeviceIDs = new (std::nothrow) PWSTR[cPnpDeviceIDs];
if (pPnpDeviceIDs != NULL)
{
DWORD dwIndex = 0;
hr = pPortableDeviceManager->GetDevices(pPnpDeviceIDs, &cPnpDeviceIDs);
if (SUCCEEDED(hr))
{
// For each device found, find the contacts service
for (dwIndex = 0; dwIndex < cPnpDeviceIDs; dwIndex++)
{
DWORD cPnpServiceIDs = 0;
PWSTR pPnpServiceID = NULL;
// First, pass NULL as the PWSTR array pointer to get the total number
// of contacts services (SERVICE_Contacts) found on the device.
// To find the total number of all services on the device, use GUID_DEVINTERFACE_WPD_SERVICE.
hr = pServiceManager->GetDeviceServices(pPnpDeviceIDs[dwIndex], SERVICE_Contacts, NULL, &cPnpServiceIDs);
if (SUCCEEDED(hr) && (cPnpServiceIDs > 0))
{
// For simplicity, we are only using the first contacts service on each device
cPnpServiceIDs = 1;
hr = pServiceManager->GetDeviceServices(pPnpDeviceIDs[dwIndex], SERVICE_Contacts, &pPnpServiceID, &cPnpServiceIDs);
if (SUCCEEDED(hr))
{
// We've found the service, display it and save its PnP Identifier
ContactsServicePnpIDs.Add(pPnpServiceID);
printf("[%d] ", static_cast<DWORD>(ContactsServicePnpIDs.GetCount()-1));
// Display information about the device that contains this service.
DisplayDeviceInformation(pServiceManager, pPnpServiceID);
// ContactsServicePnpIDs now owns the memory for this string
pPnpServiceID = NULL;
}
else
{
printf("! Failed to get the first contacts service from '%ws, hr = 0x%lx\n",pPnpDeviceIDs[dwIndex],hr);
}
}
}
}
// Free all returned PnPDeviceID strings
FreePortableDevicePnPIDs(pPnpDeviceIDs, cPnpDeviceIDs);
// Delete the array of PWSTR pointers
delete [] pPnpDeviceIDs;
pPnpDeviceIDs = NULL;
}
else
{
printf("! Failed to allocate memory for PWSTR array\n");
}
}
}
printf("\n%d Contacts Device Service(s) found on the system\n\n", static_cast<DWORD>(ContactsServicePnpIDs.GetCount()));
}
相关主题