示例#1
0
    IFACEMETHODIMP OnEvent(
        _In_ IPortableDeviceValues* eventParameters)
    {
        if (eventParameters != nullptr)
        {
            wprintf(L"***************************\n** Device event received **\n***************************\n");
            DisplayStringProperty(eventParameters, WPD_EVENT_PARAMETER_PNP_DEVICE_ID, L"WPD_EVENT_PARAMETER_PNP_DEVICE_ID");
            DisplayGuidProperty(eventParameters, WPD_EVENT_PARAMETER_EVENT_ID, L"WPD_EVENT_PARAMETER_EVENT_ID");
        }

        return S_OK;
    }
    HRESULT __stdcall OnEvent(
        IPortableDeviceValues* pEventParameters)
    {
        if (pEventParameters != NULL)
        {
            printf("***************************\n** Device event received **\n***************************\n");
            DisplayStringProperty(pEventParameters, WPD_EVENT_PARAMETER_PNP_DEVICE_ID, L"WPD_EVENT_PARAMETER_PNP_DEVICE_ID");
            DisplayGuidProperty(pEventParameters, WPD_EVENT_PARAMETER_EVENT_ID, L"WPD_EVENT_PARAMETER_EVENT_ID");
        }

        return S_OK;
    }
    HRESULT __stdcall OnProgress(
        REFGUID                            Context,
        IPortableDeviceValuesCollection*   pValues)
    {
        DWORD   dwNumElements = 0;
        HRESULT hr = pValues->GetCount(&dwNumElements);
        if (FAILED(hr))
        {
            printf("! Failed to get number of elements from IPortableDeviceValuesCollection, hr = 0x%lx\n",hr);
        }

        printf("Received next batch of %d object value elements..., Context = %ws\n", dwNumElements, (PWSTR)CGuidToString(Context));

        // Display the returned properties to the user.
        // NOTE: We are reading for expected properties, which were setup in the
        // QueueGetXXXXXX bulk operation call.
        if (SUCCEEDED(hr))
        {
            for (DWORD dwIndex = 0; dwIndex < dwNumElements; dwIndex++)
            {
                CComPtr<IPortableDeviceValues> pObjectProperties;
                hr = pValues->GetAt(dwIndex, &pObjectProperties);
                if (SUCCEEDED(hr))
                {
                    DisplayStringProperty(pObjectProperties, WPD_OBJECT_PARENT_ID,            L"WPD_OBJECT_PARENT_ID");
                    DisplayStringProperty(pObjectProperties, WPD_OBJECT_NAME,                 L"WPD_OBJECT_NAME");
                    DisplayStringProperty(pObjectProperties, WPD_OBJECT_PERSISTENT_UNIQUE_ID, L"WPD_OBJECT_PERSISTENT_UNIQUE_ID");
                    DisplayGuidProperty  (pObjectProperties, WPD_OBJECT_CONTENT_TYPE,         L"WPD_OBJECT_CONTENT_TYPE");
                    DisplayGuidProperty  (pObjectProperties, WPD_OBJECT_FORMAT,               L"WPD_OBJECT_FORMAT");
                    printf("\n\n");
                }
                else
                {
                    printf("! Failed to get IPortableDeviceValues from IPortableDeviceValuesCollection at index '%d'\n", dwIndex);
                }
            }
        }

        return hr;
    }
// Reads properties for the user specified object.
void ReadContentProperties(
    IPortableDevice*    pDevice)
{
    if (pDevice == NULL)
    {
        printf("! A NULL IPortableDevice interface pointer was received\n");
        return;
    }

    HRESULT                               hr               = S_OK;
    WCHAR                                 szSelection[81]  = {0};
    CComPtr<IPortableDeviceProperties>    pProperties;
    CComPtr<IPortableDeviceValues>        pObjectProperties;
    CComPtr<IPortableDeviceContent>       pContent;
    CComPtr<IPortableDeviceKeyCollection> pPropertiesToRead;

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

    // 1) Get an IPortableDeviceContent interface from the IPortableDevice interface to
    // access the content-specific methods.
    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) CoCreate an IPortableDeviceKeyCollection interface to hold the the property keys
    // we wish to read.
	//<SnippetContentProp1>
    hr = CoCreateInstance(CLSID_PortableDeviceKeyCollection,
                          NULL,
                          CLSCTX_INPROC_SERVER,
                          IID_PPV_ARGS(&pPropertiesToRead));
    if (SUCCEEDED(hr))
    {
        // 4) Populate the IPortableDeviceKeyCollection with the keys we wish to read.
        // NOTE: We are not handling any special error cases here so we can proceed with
        // adding as many of the target properties as we can.
        if (pPropertiesToRead != NULL)
        {
            HRESULT hrTemp = S_OK;
            hrTemp = pPropertiesToRead->Add(WPD_OBJECT_PARENT_ID);
            if (FAILED(hrTemp))
            {
                printf("! Failed to add WPD_OBJECT_PARENT_ID to IPortableDeviceKeyCollection, hr= 0x%lx\n", hrTemp);
            }

            hrTemp = pPropertiesToRead->Add(WPD_OBJECT_NAME);
            if (FAILED(hrTemp))
            {
                printf("! Failed to add WPD_OBJECT_NAME to IPortableDeviceKeyCollection, hr= 0x%lx\n", hrTemp);
            }

            hrTemp = pPropertiesToRead->Add(WPD_OBJECT_PERSISTENT_UNIQUE_ID);
            if (FAILED(hrTemp))
            {
                printf("! Failed to add WPD_OBJECT_PERSISTENT_UNIQUE_ID to IPortableDeviceKeyCollection, hr= 0x%lx\n", hrTemp);
            }

            hrTemp = pPropertiesToRead->Add(WPD_OBJECT_FORMAT);
            if (FAILED(hrTemp))
            {
                printf("! Failed to add WPD_OBJECT_FORMAT to IPortableDeviceKeyCollection, hr= 0x%lx\n", hrTemp);
            }

            hrTemp = pPropertiesToRead->Add(WPD_OBJECT_CONTENT_TYPE);
            if (FAILED(hrTemp))
            {
                printf("! Failed to add WPD_OBJECT_CONTENT_TYPE to IPortableDeviceKeyCollection, hr= 0x%lx\n", hrTemp);
            }
        }
    }
	//</SnippetContentProp1>
	// 5) Call GetValues() passing the collection of specified PROPERTYKEYs.
	//<SnippetContentProp2>
    if (SUCCEEDED(hr))
    {
        hr = pProperties->GetValues(szSelection,         // The object whose properties we are reading
                                    pPropertiesToRead,   // The properties we want to read
                                    &pObjectProperties); // Driver supplied property values for the specified object
        if (FAILED(hr))
        {
            printf("! Failed to get all properties for object '%ws', hr= 0x%lx\n", szSelection, hr);
        }
    }
	//</SnippetContentProp2>
    // 6) Display the returned property values to the user
    if (SUCCEEDED(hr))
    {
        DisplayStringProperty(pObjectProperties, WPD_OBJECT_PARENT_ID,            L"WPD_OBJECT_PARENT_ID");
        DisplayStringProperty(pObjectProperties, WPD_OBJECT_NAME,                 L"WPD_OBJECT_NAME");
        DisplayStringProperty(pObjectProperties, WPD_OBJECT_PERSISTENT_UNIQUE_ID, L"WPD_OBJECT_PERSISTENT_UNIQUE_ID");
        DisplayGuidProperty  (pObjectProperties, WPD_OBJECT_CONTENT_TYPE,         L"WPD_OBJECT_CONTENT_TYPE");
        DisplayGuidProperty  (pObjectProperties, WPD_OBJECT_FORMAT,               L"WPD_OBJECT_FORMAT");
    }
}