Bagikan melalui


Mentransfer Objek Properties-Only ke Perangkat

Sementara contoh dalam topik sebelumnya menunjukkan pembuatan konten pada perangkat yang terdiri dari properti dan data, topik ini berfokus pada pembuatan objek khusus properti.

Transfer khusus properti dilakukan menggunakan antarmuka yang dijelaskan dalam tabel berikut.

Antarmuka Deskripsi
Antarmuka IPortableDeviceContent Menyediakan akses ke metode khusus konten.
Antarmuka IPortableDeviceValues Digunakan untuk mengambil properti yang menjelaskan konten.

 

Fungsi TransferContactToDevice dalam modul ContentTransfer.cpp aplikasi sampel menunjukkan bagaimana aplikasi dapat mentransfer informasi kontak dari PC ke perangkat yang terhubung. Dalam sampel khusus ini, nama kontak yang ditransfer adalah "John Kane" yang dikodekan secara permanen dan nomor telepon kontak yang ditransfer selalu "425-555-0123".

Tugas pertama yang diselesaikan oleh TransferContactToDevice fungsi adalah meminta pengguna untuk memasukkan pengidentifikasi objek untuk objek induk pada perangkat (di mana konten akan ditransfer).

HRESULT                             hr = S_OK;
WCHAR                               szSelection[81]        = {0};
CComPtr<IPortableDeviceValues>      pFinalObjectProperties;
CComPtr<IPortableDeviceContent>     pContent;

// Prompt user to enter an object identifier for the parent object on the device to transfer.
printf("Enter the identifer of the parent object which the contact will be transferred under.\n>");
hr = StringCbGetsW(szSelection,sizeof(szSelection));
if (FAILED(hr))
{
    printf("An invalid object identifier was specified, aborting content transfer\n");
}

Langkah selanjutnya adalah pengambilan properti kontak, yang akan digunakan ketika sampel menulis kontak baru ke perangkat. Pengambilan properti kontak dilakukan oleh fungsi pembantu GetRequiredPropertiesForPropertiesOnlyContact .

// 2) Get the properties that describe the object being created on the device

if (SUCCEEDED(hr))
{
    hr = GetRequiredPropertiesForPropertiesOnlyContact(szSelection,              // Parent to transfer the data under
                                                       &pFinalObjectProperties);  // Returned properties describing the data
    if (FAILED(hr))
    {
        printf("! Failed to get required properties needed to transfer an image file to the device, hr = 0x%lx\n", hr);
    }
}

Fungsi ini menulis data berikut ke objek IPortableDeviceValues .

  • Pengidentifikasi objek untuk induk kontak pada perangkat. (Ini adalah tujuan di mana perangkat akan menyimpan objek.)
  • Jenis objek (kontak).
  • Nama kontak (string berkode keras "John Kane").
  • Nomor telepon kontak (string yang dikodekan secara permanen "425-555-0123").

Langkah terakhir membuat objek khusus properti pada perangkat (menggunakan properti yang diatur oleh fungsi pembantu GetRequiredPropertiesForPropertiesOnlyContact ). Ini dicapai dengan memanggil metode IPortableDeviceContent::CreateObjectWithPropertiesOnly .

if (SUCCEEDED(hr))
{
    PWSTR pszNewlyCreatedObject = NULL;
    hr = pContent->CreateObjectWithPropertiesOnly(pFinalObjectProperties,    // Properties describing the object data
                                                  &pszNewlyCreatedObject);
    if (SUCCEEDED(hr))
    {
        printf("The contact was transferred to the device.\nThe newly created object's ID is '%ws'\n",pszNewlyCreatedObject);
    }

    if (FAILED(hr))
    {
        printf("! Failed to transfer contact object to the device, hr = 0x%lx\n",hr);
    }

    // Free the object identifier string returned from CreateObjectWithPropertiesOnly
    CoTaskMemFree(pszNewlyCreatedObject);
    pszNewlyCreatedObject = NULL;
}

Antarmuka IPortableDevice

Antarmuka IPortableDeviceContent

Antarmuka IPortableDeviceValues

Panduan Pemrograman