Abrufen von Geräteattributen
Windows Media Player speichert Informationen zu Geräten, die mit dem Player verbunden sind. Einige Attribute sind durch Aufrufen einzelner Methoden von IWMPSyncDevice verfügbar, einige können mithilfe von IWMPSyncDevice::getItemInfo abgerufen werden, und einige können mit beiden Methoden abgerufen werden.
Im folgenden Beispielcode wird ein Listenfeld-Steuerelement mit den verfügbaren Attributen für das angegebene Gerät auffüllt. Der erste Teil der Funktion ruft die verfügbaren Eigenschaften mithilfe bestimmter Methoden ab. Der zweite Teil der Funktion ruft Attributwerte mithilfe von IWMPSyncDevice::getItemInfo ab. Der Funktionsparameter lIndex ist der Index für das Gerät in Ihrem benutzerdefinierten Gerätearray, auf das m _ ppWMPDevices zeigt.
STDMETHODIMP CMainDlg::ShowDeviceAttributes(long lIndex)
{
HRESULT hr = S_OK;
if(!m_ppWMPDevices)
return S_FALSE;
// Array of attribute names for devices.
const TCHAR *szDeviceAttributeNames[16] = {
_T("Connected"),
_T("FreeSpace"),
_T("FriendlyName"),
_T("LastSyncErrorCount"),
_T("LastSyncNoFitCount"),
_T("LastSyncTime"),
_T("Name"),
_T("SerialNumber"),
_T("SupportsAudio"),
_T("SupportsPhoto"),
_T("SupportsVideo"),
_T("SyncIndex"),
_T("SyncItemCount"),
_T("SyncPercentComplete"),
_T("SyncRelationship"),
_T("TotalSpace")};
// Array of device status strings.
static const TCHAR *szDeviceStatus[6] = {
_T("Unknown"),
_T("Partnership exists"),
_T("Partnership declined by the user"),
_T("Partnership with another computer or user"),
_T("Device only supports manual transfer"),
_T("New device")};
// Handle to the list box that displays the information.
HWND hwndStats = GetDlgItem(IDC_STATS);
CComBSTR bstrStatistics; // Contains the display string for each property.
CComBSTR bstrTemp; // Contains the retrieved value for each property.
// Retrieve a pointer to the current device.
CComPtr<IWMPSyncDevice> spDevice(m_ppWMPDevices[GetSelectedDeviceIndex()]);
VARIANT_BOOL vbIsConnected = VARIANT_FALSE;
WMPDeviceStatus wmpDS = wmpdsUnknown;
SendMessage(hwndStats, LB_RESETCONTENT, 0, 0);
if(NULL == spDevice.p)
{
return E_POINTER;
}
// Status
spDevice->get_status(&wmpDS);
bstrStatistics = "Status: ";
bstrTemp = szDeviceStatus[wmpDS];
bstrStatistics += bstrTemp;
SendMessage(hwndStats, LB_ADDSTRING, 0,(LPARAM)(LPCTSTR)COLE2T(bstrStatistics));
bstrTemp.Empty();
// Connected?
spDevice->get_connected(&vbIsConnected);
bstrStatistics = "Connected: ";
bstrTemp = (vbIsConnected == VARIANT_TRUE)?"True":"False";
bstrStatistics += bstrTemp;
SendMessage(hwndStats, LB_ADDSTRING, 0,(LPARAM)(LPCTSTR)COLE2T(bstrStatistics));
bstrTemp.Empty();
// Device ID
spDevice->get_deviceId(&bstrTemp);
bstrStatistics = "Device ID: ";
bstrStatistics += bstrTemp;
// Calculate the list box width based on text metrics.
// This assumes Device ID is likely to be the longest string.
HDC hDC = GetDC();
SIZE sizeText = {0, 0};
GetTextExtentPoint32(hDC, (LPCTSTR)COLE2T(bstrStatistics), bstrStatistics.Length(), &sizeText);
ReleaseDC(hDC);
SendMessage(hwndStats, LB_ADDSTRING, 0,(LPARAM)(LPCTSTR)COLE2T(bstrStatistics));
SendMessage(hwndStats, LB_SETHORIZONTALEXTENT, sizeText.cx, 0);
bstrTemp.Empty();
// Friendly name
spDevice->get_friendlyName(&bstrTemp);
bstrStatistics = "Friendly name: ";
bstrStatistics += bstrTemp;
SendMessage(hwndStats, LB_ADDSTRING, 0,(LPARAM)(LPCTSTR)COLE2T(bstrStatistics));
bstrTemp.Empty();
// Skip a line and label the getItemInfo attributes.
bstrStatistics = "";
SendMessage(hwndStats, LB_ADDSTRING, 0,(LPARAM)(LPCTSTR)COLE2T(bstrStatistics));
bstrStatistics = "--getItemInfo Attributes--";
SendMessage(hwndStats, LB_ADDSTRING, 0,(LPARAM)(LPCTSTR)COLE2T(bstrStatistics));
// Show the getItemInfo attributes.
int iArrayBound = sizeof szDeviceAttributeNames / sizeof szDeviceAttributeNames[0];
for(int i = 0; i < iArrayBound; i++)
{
CComBSTR bstrName(szDeviceAttributeNames[i]);
CComBSTR bstrVal;
CComBSTR bstrDisplayString;
hr = spDevice->getItemInfo(bstrName, &bstrVal);
if(FAILED(hr))
{
bstrVal.Append("Error");
}
bstrName.Append(L": ");
bstrDisplayString.AppendBSTR(bstrName);
bstrDisplayString += bstrVal;
SendMessage(hwndStats, LB_ADDSTRING, 0, (LPARAM)(LPCTSTR)COLE2T(bstrDisplayString));
}
return hr;
}