Share via


Impostazione delle proprietà per un singolo oggetto

Dopo che l'applicazione recupera un identificatore di oggetto (vedere l'argomento Enumerating Content ) per un determinato oggetto, può impostare le proprietà per tale oggetto chiamando i metodi nelle interfacce descritte nella tabella seguente.

Interfaccia Descrizione
Interfaccia IPortableDeviceProperties Utilizzato per determinare se una determinata proprietà può essere scritta e per inviare l'operazione di scrittura.
Interfaccia IPortableDeviceContent Fornisce l'accesso ai metodi specifici del contenuto.
Interfaccia IPortableDeviceValues Utilizzato per contenere i valori da scrivere, determinare i risultati dell'operazione di scrittura e recuperare gli attributi delle proprietà (quando si determina la funzionalità di scrittura).

 

Le applicazioni impostano le proprietà su un oggetto creando innanzitutto un contenitore delle proprietà che specifica i nuovi valori usando l'interfaccia IPortableDeviceValues. Dopo aver creato il contenitore delle proprietà, un'applicazione imposta tali proprietà chiamando il metodo IPortableDeviceProperties::SetValues .

La WriteContentProperties funzione nel modulo ContentProperties.cpp dell'applicazione di esempio illustra come un'applicazione potrebbe impostare una nuova proprietà object-name per un oggetto selezionato.

La prima attività eseguita dalla funzione consiste nel WriteContentProperties chiedere all'utente di immettere un identificatore di oggetto per l'oggetto per cui l'applicazione scriverà il nuovo nome.

HRESULT                               hr                   = S_OK;
WCHAR                                 szSelection[81]      = {0};
WCHAR                                 szNewObjectName[81]  = {0};
CComPtr<IPortableDeviceProperties>    pProperties;
CComPtr<IPortableDeviceContent>       pContent;
CComPtr<IPortableDeviceValues>        pObjectPropertiesToWrite;
CComPtr<IPortableDeviceValues>        pPropertyWriteResults;
CComPtr<IPortableDeviceValues>        pAttributes;
BOOL                                  bCanWrite            = FALSE;

// Prompt user to enter an object identifier on the device to write properties on.
printf("Enter the identifer of the object you wish to write properties on.\n>");
hr = StringCbGetsW(szSelection,sizeof(szSelection));
if (FAILED(hr))
{
    printf("An invalid object identifier was specified, aborting property reading\n");
}

Successivamente, l'applicazione recupera il valore WPD_PROPERTY_ATTRIBUTE_CAN_WRITE per la proprietà WPD_OBJECT_NAME per determinare se la proprietà può essere scritta. Si noti che l'applicazione può impostare qualsiasi proprietà per la quale il valore WPD_PROPERTY_ATTRIBUTE_CAN_WRITE è true.

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

// 2) Get an IPortableDeviceProperties interface from the IPortableDeviceContent interface
// to access the property-specific methods.
if (SUCCEEDED(hr))
{
    hr = pContent->Properties(&pProperties);
    if (FAILED(hr))
    {
        printf("! Failed to get IPortableDeviceProperties from IPortableDevice, hr = 0x%lx\n",hr);
    }
}

// 3) Check the property attributes to see if we can write/change the WPD_OBJECT_NAME property.
if (SUCCEEDED(hr))
{
    hr = pProperties->GetPropertyAttributes(szSelection,
                                            WPD_OBJECT_NAME,
                                            &pAttributes);
    if (SUCCEEDED(hr))
    {
        hr = pAttributes->GetBoolValue(WPD_PROPERTY_ATTRIBUTE_CAN_WRITE, &bCanWrite);
        if (SUCCEEDED(hr))
        {
            if (bCanWrite)
            {
                printf("The attribute WPD_PROPERTY_ATTRIBUTE_CAN_WRITE for the WPD_OBJECT_NAME reports TRUE\nThis means that the property can be changed/updated\n\n");
            }
            else
            {
                printf("The attribute WPD_PROPERTY_ATTRIBUTE_CAN_WRITE for the WPD_OBJECT_NAME reports FALSE\nThis means that the property cannot be changed/updated\n\n");
            }
        }
        else
        {
            printf("! Failed to get the WPD_PROPERTY_ATTRIBUTE_CAN_WRITE value from WPD_OBJECT_NAME on object '%ws', hr = 0x%lx\n",szSelection, hr);
        }
    }
}

Il passaggio successivo consiste nel richiedere all'utente la nuova stringa di nome. Si noti che la chiamata al metodo IPortableDeviceValues::SetStringValue crea semplicemente il contenitore delle proprietà; la proprietà effettiva non è ancora stata scritta.

if (bCanWrite)
{
    printf("Enter the new WPD_OBJECT_NAME for the object '%ws'.\n>",szSelection);
    hr = StringCbGetsW(szNewObjectName,sizeof(szNewObjectName));
    if (FAILED(hr))
    {
        printf("An invalid object name was specified, aborting property writing\n");
    }

    // 5) CoCreate an IPortableDeviceValues interface to hold the property values
    // we wish to write.
    if (SUCCEEDED(hr))
    {
        hr = CoCreateInstance(CLSID_PortableDeviceValues,
                              NULL,
                              CLSCTX_INPROC_SERVER,
                              IID_PPV_ARGS(&pObjectPropertiesToWrite));
        if (SUCCEEDED(hr))
        {
            if (pObjectPropertiesToWrite != NULL)
            {
                hr = pObjectPropertiesToWrite->SetStringValue(WPD_OBJECT_NAME, szNewObjectName);
                if (FAILED(hr))
                {
                    printf("! Failed to add WPD_OBJECT_NAME to IPortableDeviceValues, hr= 0x%lx\n", hr);
                }
            }
        }
    }

Infine, il nuovo valore, specificato dall'utente, viene applicato all'oggetto .

if (SUCCEEDED(hr))
{
    hr = pProperties->SetValues(szSelection,                // The object whose properties we are reading
                                pObjectPropertiesToWrite,   // The properties we want to read
                                &pPropertyWriteResults);    // Driver supplied property result values for the property read operation
    if (FAILED(hr))
    {
        printf("! Failed to set properties for object '%ws', hr= 0x%lx\n", szSelection, hr);
    }
    else
    {
        printf("The WPD_OBJECT_NAME property on object '%ws' was written successfully (Read the properties again to see the updated value)\n", szSelection);
    }
}

Interfaccia IPortableDevice

Interfaccia IPortableDeviceContent

Interfaccia IPortableDeviceKeyCollection

Interfaccia IPortableDeviceProperties

Interfaccia IPortableDevicePropertiesBulk

Interfaccia IPortableDevicePropVariantCollection

Guida alla programmazione