다음을 통해 공유


영구 고유 ID에서 개체 ID 검색

개체 식별자는 지정된 디바이스 세션에 대해서만 고유하도록 보장됩니다. 사용자가 새 연결을 설정하면 이전 세션의 식별자가 현재 세션의 식별자와 일치하지 않을 수 있습니다. 이 문제를 resolve 위해 WPD API는 디바이스 세션 간에 지속되는 PUID(영구 고유 식별자)를 지원합니다.

일부 디바이스는 지정된 개체를 사용하여 기본적으로 PUID를 저장합니다. 다른 사용자는 선택한 개체 데이터의 해시를 기반으로 PUID를 생성할 수 있습니다. 다른 사용자는 개체 식별자를 PUID로 처리할 수 있습니다(이러한 식별자가 변경되지 않도록 보장할 수 있기 때문).

ContentProperties.cpp 모듈의 GetObjectIdentifierFromPersistentUniqueIdentifier 함수는 지정된 PUID에 대한 개체 식별자를 검색하는 방법을 보여 줍니다.

애플리케이션은 다음 표에 설명된 인터페이스를 사용하여 해당 PUID에 대한 개체 식별자를 검색할 수 있습니다.

인터페이스 Description
IPortableDeviceContent 인터페이스 검색 메서드에 대한 액세스를 제공합니다.
IPortableDevicePropVariantCollection 인터페이스 개체 식별자와 해당 PUID(영구 고유 식별자)를 모두 저장하는 데 사용됩니다.

 

샘플 애플리케이션이 수행하는 첫 번째 작업은 사용자로부터 PUID를 가져오는 것입니다.

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

그런 다음 샘플 애플리케이션은 GetObjectIDsFromPersistentUniqueIDs 메서드를 호출하는 데 사용할 IPortableDeviceContent 개체를 검색합니다.

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

다음으로, 사용자가 제공한 PUID를 보유할 IPortableDevicePropVariantCollection 개체를 만듭니다.

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

이전 세 단계를 수행하면 샘플은 사용자가 제공한 PUID와 일치하는 개체 식별자를 검색할 준비가 됩니다. 이 작업은 IPortableDeviceContent::GetObjectIDsFromPersistentUniqueIDs 메서드를 호출하여 수행됩니다. 이 메서드를 호출하기 전에 샘플은 PROPVARIANT 구조를 초기화하고, 사용자가 제공한 PUID를 이 구조체에 쓰고, pPersistentUniqueIDs가 가리키는 IPortableDevicePropVariantCollection 개체에 추가합니다. 이 포인터는 GetObjectIDsFromPersistentUniqueIDs 메서드에 첫 번째 인수로 전달됩니다. GetObjectIDsFromPersistentUniqueIDs의 두 번째 인수는 일치하는 개체 식별자를 수신하는 IPortableDevicePropVariantCollection 개체입니다.

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 인터페이스

IPortableDeviceContent 인터페이스

IPortableDevicePropVariantCollection 인터페이스

프로그래밍 가이드