Dela via


Hämtar ett objekt-ID från ett beständigt unikt ID

Objektidentifierare garanteras bara vara unika för en viss enhetssession. Om användaren upprättar en ny anslutning kanske identifierare från en tidigare session inte matchar identifierare för den aktuella sessionen. För att lösa det här problemet stöder WPD-API:et beständiga unika identifierare (PUID) som bevaras mellan enhetssessioner.

Vissa enheter lagrar sina PUID:er internt med ett visst objekt. Andra kan generera PUID baserat på en hash av valda objektdata. Andra kan behandla objektidentifierare som PUID (eftersom de kan garantera att dessa identifierare aldrig ändras).

Funktionen GetObjectIdentifierFromPersistentUniqueIdentifier i modulen ContentProperties.cpp visar hämtningen av ett objektidentifierare för ett visst PUID.

Ditt program kan hämta en objektidentifierare för ett motsvarande PUID med hjälp av de gränssnitt som beskrivs i följande tabell.

Gränssnitt Beskrivning
IPortableDeviceContent-gränssnitt Ger åtkomst till hämtningsmetoden.
IPortableDevicePropVariantCollection-gränssnitt Används för att lagra både objektidentifieraren och motsvarande beständiga unika identifierare (PUID).

 

Den första uppgiften som exempelprogrammet utför är att hämta ett PUID från användaren.

// Prompt user to enter an unique identifier to convert to an object idenifier.
printf("Enter the Persistant 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");
}

Därefter hämtar exempelprogrammet ett IPortableDeviceContent--objekt som används för att anropa GetObjectIDsFromPersistentUniqueIDs-metoden.

if (SUCCEEDED(hr))
{
    hr = pDevice->Content(&pContent);
    if (FAILED(hr))
    {
        printf("! Failed to get IPortableDeviceContent from IPortableDevice, hr = 0x%lx\n",hr);
    }
}

Därefter skapas ett IPortableDevicePropVariantCollection-objekt som innehåller det PUID som tillhandahålls av användaren.

hr = CoCreateInstance(CLSID_PortableDevicePropVariantCollection,
                      NULL,
                      CLSCTX_INPROC_SERVER,
                      IID_PPV_ARGS(&pPersistentUniqueIDs));

När de föregående tre stegen har vidtagits är exemplet redo att hämta objektidentifieraren som matchar det PUID som tillhandahålls av användaren. Detta uppnås genom att anropa metoden IPortableDeviceContent::GetObjectIDsFromPersistentUniqueIDs. Innan du anropar den här metoden initierar exemplet en PROPVARIANT-struktur, skriver det PUID som tillhandahålls av användaren till den här strukturen och lägger till det i objektet IPortableDevicePropVariantCollection där pPersistentUniqueIDs pekar. Den här pekaren skickas som det första argumentet till metoden GetObjectIDsFromPersistentUniqueIDs. Det andra argumentet i GetObjectIDsFromPersistentUniqueIDs är ett IPortableDevicePropVariantCollection-objekt som tar emot matchande objektidentifierare.

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);
    }
}

IPortableDevice Interface

IPortableDeviceContent-gränssnitt

IPortableDevicePropVariantCollection-gränssnitt

programmeringsguide