Nuta
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować się zalogować lub zmienić katalog.
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zmienić katalogi.
Identyfikatory obiektów są gwarantowane tylko jako unikatowe dla danej sesji urządzenia; Jeśli użytkownik ustanawia nowe połączenie, identyfikatory z poprzedniej sesji mogą nie być zgodne z identyfikatorami bieżącej sesji. Aby rozwiązać ten problem, interfejs API WPD obsługuje trwałe unikatowe identyfikatory (PUID), które są utrwalane w sesjach urządzeń.
Niektóre urządzenia przechowują swoje identyfikatory PUID natywnie z danym obiektem. Inne mogą wygenerować identyfikator PUID na podstawie skrótu wybranych danych obiektu. Inne osoby mogą traktować identyfikatory obiektów jako identyfikatory PUID (ponieważ mogą zagwarantować, że te identyfikatory nigdy się nie zmieniają).
Funkcja GetObjectIdentifierFromPersistentUniqueIdentifier w module ContentProperties.cpp demonstruje pobieranie identyfikatora obiektu dla danego identyfikatora PUID.
Aplikacja może pobrać identyfikator obiektu dla odpowiedniego identyfikatora PUID przy użyciu interfejsów opisanych w poniższej tabeli.
| Interfejs | Opis |
|---|---|
| interfejs IPortableDeviceContent | Zapewnia dostęp do metody pobierania. |
| Interfejs IPortableDevicePropVariantCollection | Służy do przechowywania zarówno identyfikatora obiektu, jak i odpowiedniego trwałego unikatowego identyfikatora (PUID). |
Pierwszym zadaniem, które wykonuje przykładowa aplikacja, jest uzyskanie identyfikatora PUID od użytkownika.
// Prompt user to enter an unique identifier to convert to an object identifier.
printf("Enter the Persistent Unique Identifier of the object you wish to convert into an object identifier.\n>");
hr = StringCbGetsW(szSelection,sizeof(szSelection));
if (FAILED(hr))
{
printf("An invalid persistent object identifier was specified, aborting the query operation\n");
}
Następnie przykładowa aplikacja pobiera obiekt IPortableDeviceContent, który będzie używany do wywoływania metody GetObjectIDsFromPersistentUniqueIDs.
if (SUCCEEDED(hr))
{
hr = pDevice->Content(&pContent);
if (FAILED(hr))
{
printf("! Failed to get IPortableDeviceContent from IPortableDevice, hr = 0x%lx\n",hr);
}
}
Następnie tworzy obiekt IPortableDevicePropVariantCollection, który będzie przechowywać identyfikator PUID dostarczony przez użytkownika.
hr = CoCreateInstance(CLSID_PortableDevicePropVariantCollection,
NULL,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&pPersistentUniqueIDs));
Po podjęciu poprzednich trzech kroków przykład jest gotowy do pobrania identyfikatora obiektu zgodnego z identyfikatorem PUID dostarczonym przez użytkownika. Jest to realizowane przez wywołanie metody IPortableDeviceContent::GetObjectIDsFromPersistentUniqueIDs. Przed wywołaniem tej metody próbka inicjuje strukturę PROPVARIANT, zapisuje identyfikator PUID dostarczony przez użytkownika do tej struktury i dodaje go do obiektu IPortableDevicePropVariantCollection, na które wskazuje pPersistentUniqueIDs. Ten wskaźnik jest przekazywany jako pierwszy argument do metody GetObjectIDsFromPersistentUniqueIDs. Drugi argument GetObjectIDsFromPersistentUniqueIDs jest obiektem IPortableDevicePropVariantCollection, który otrzymuje pasujący identyfikator obiektu.
if (SUCCEEDED(hr))
{
if (pPersistentUniqueIDs != NULL)
{
PROPVARIANT pv = {0};
PropVariantInit(&pv);
// Initialize a PROPVARIANT structure with the object identifier string
// that the user selected above. Notice we are allocating memory for the
// PWSTR value. This memory will be freed when PropVariantClear() is
// called below.
pv.vt = VT_LPWSTR;
pv.pwszVal = AtlAllocTaskWideString(szSelection);
if (pv.pwszVal != NULL)
{
// Add the object identifier to the objects-to-delete list
// (We are only deleting 1 in this example)
hr = pPersistentUniqueIDs->Add(&pv);
if (SUCCEEDED(hr))
{
// 3) Attempt to get the unique idenifier for the object from the device
hr = pContent->GetObjectIDsFromPersistentUniqueIDs(pPersistentUniqueIDs,
&pObjectIDs);
if (SUCCEEDED(hr))
{
PROPVARIANT pvId = {0};
hr = pObjectIDs->GetAt(0, &pvId);
if (SUCCEEDED(hr))
{
printf("The persistent unique identifier '%ws' relates to object identifier '%ws' on the device.\n", szSelection, pvId.pwszVal);
}
else
{
printf("! Failed to get the object identifier for '%ws' from the IPortableDevicePropVariantCollection, hr = 0x%lx\n",szSelection, hr);
}
// Free the returned allocated string from the GetAt() call
PropVariantClear(&pvId);
}
else
{
printf("! Failed to get the object identifier from persistent object idenifier '%ws', hr = 0x%lx\n",szSelection, hr);
}
}
else
{
printf("! Failed to get the object identifier from persistent object idenifier because we could no add the persistent object identifier string to the IPortableDevicePropVariantCollection, hr = 0x%lx\n",hr);
}
}
else
{
hr = E_OUTOFMEMORY;
printf("! Failed to get the object identifier because we could no allocate memory for the persistent object identifier string, hr = 0x%lx\n",hr);
}
// Free any allocated values in the PROPVARIANT before exiting
PropVariantClear(&pv);
}
}
Tematy pokrewne
-
Interfejs IPortableDevicePropVariantCollection