Nota
O acesso a esta página requer autorização. Podes tentar iniciar sessão ou mudar de diretório.
O acesso a esta página requer autorização. Podes tentar mudar de diretório.
Os identificadores de objeto só têm a garantia de serem exclusivos para uma determinada sessão de dispositivo; Se o usuário estabelecer uma nova conexão, os identificadores de uma sessão anterior podem não corresponder aos identificadores da sessão atual. Para resolver esse problema, a API WPD oferece suporte a identificadores exclusivos persistentes (PUIDs), que persistem nas sessões do dispositivo.
Alguns dispositivos armazenam seus PUIDs nativamente com um determinado objeto. Outros podem gerar o PUID com base em um hash de dados de objeto selecionados. Outros podem tratar identificadores de objeto como PUIDs (porque eles podem garantir que esses identificadores nunca sejam alterados).
A função GetObjectIdentifierFromPersistentUniqueIdentifier no módulo ContentProperties.cpp demonstra a recuperação de um identificador de objeto para um determinado PUID.
Seu aplicativo pode recuperar um identificador de objeto para um PUID correspondente usando as interfaces descritas na tabela a seguir.
| Interface | Descrição |
|---|---|
| Interface IPortableDeviceContent | Fornece acesso ao método de recuperação. |
| Interface IPortableDevicePropVariantCollection | Usado para armazenar o identificador de objeto e o identificador único persistente (PUID) correspondente. |
A primeira tarefa que o aplicativo de exemplo realiza é obter um PUID do usuário.
// 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");
}
Depois disso, o aplicativo de exemplo recupera um IPortableDeviceContent objeto, que ele usará para invocar o GetObjectIDsFromPersistentUniqueIDs método.
if (SUCCEEDED(hr))
{
hr = pDevice->Content(&pContent);
if (FAILED(hr))
{
printf("! Failed to get IPortableDeviceContent from IPortableDevice, hr = 0x%lx\n",hr);
}
}
Em seguida, ele cria um IPortableDevicePropVariantCollection objeto que conterá o PUID fornecido pelo usuário.
hr = CoCreateInstance(CLSID_PortableDevicePropVariantCollection,
NULL,
CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&pPersistentUniqueIDs));
Depois que as três etapas anteriores forem executadas, o exemplo estará pronto para recuperar o identificador de objeto que corresponde ao PUID fornecido pelo usuário. Isso é feito chamando o IPortableDeviceContent::GetObjectIDsFromPersistentUniqueIDs método. Antes de chamar esse método, o exemplo inicializa uma estrutura PROPVARIANT, grava o PUID fornecido pelo usuário nessa estrutura e o adiciona ao objeto IPortableDevicePropVariantCollection no qual pPersistentUniqueIDs aponta. Esse ponteiro é passado como o primeiro argumento para o método GetObjectIDsFromPersistentUniqueIDs. O segundo argumento de GetObjectIDsFromPersistentUniqueIDs é um objeto IPortableDevicePropVariantCollection que recebe o identificador de objeto correspondente.
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);
}
}
Tópicos relacionados