다음을 통해 공유


디바이스 속성을 읽는 코드 예제

다음 코드 예제에서는 속성 저장소에서 디바이스 속성을 읽는 방법을 보여줍니다. 현재 Function Instance 개체에 대한 속성 저장소를 여는 방법에 대한 예제는 속성 저장소 열기에 대한 코드 예제를 참조하세요.

/**************************************************************************\
* CWSDDevice::ReadDeviceProperty
*
* Reads a LPWTSR device property from the device metadata and returns 
* a copy of its value as a BSTR character string. 
*
*
* Arguments:
*
*    pPropertyStore        - IPropertyStore interface, optional
*                            (if not specified a new property store
*                            Is opened and closed for each call)
*    pPropertyKey          - property key (for example, PKEY_PNPX_FirmwareVersion)
*    pbstrPropertyValue    - Returns the property value; this value must be 
*                            freed by caller using SysFreeString
*
* Return Value:
*
*     S_OK if operation is successful, an error HRESULT otherwise
*
\**************************************************************************/

HRESULT
CWSDDevice::ReadDeviceProperty(
    __in_opt IPropertyStore *pPropertyStore,
    __in const PROPERTYKEY  *pPropertyKey,
    __out BSTR              *pbstrPropertyValue)
{
    HRESULT hr = S_OK;
    IPropertyStore *pThisPropertyStore = NULL;
    BOOL bClosePropertyStore = FALSE;
    PROPVARIANT propvar = {0};

    if ((!pbstrPropertyValue) || (!pPropertyKey))
    {
        hr = E_INVALIDARG;
        WIAS_ERROR((g_hInst, "Invalid argument, hr = 0x%08X", hr));
    }

    if ((!m_pFunctionInstance) || (!m_pScanProxy))
    {
        hr = E_UNEXPECTED;
        WIAS_ERROR((g_hInst, "ScanProxy not initialized, hr = 0x%08X", hr));
    }

    if (SUCCEEDED(hr))
    {
        pThisPropertyStore = pPropertyStore;
 
        if (!pThisPropertyStore)
        {
            hr = m_pFunctionInstance->OpenPropertyStore(STGM_READ, &pThisPropertyStore);
            if ((SUCCEEDED(hr)) && (!pThisPropertyStore))
            {
                hr = E_POINTER;
                WIAS_ERROR((g_hInst, 
                    "IFunctionInstance::OpenPropertyStore returned hr = 0x%08X with a NULL property store interface, hr = 0x%08X", hr));
            }
            if (FAILED(hr))
            {
                WIAS_ERROR((g_hInst, "IFunctionInstance::OpenPropertyStore failed, hr = 0x%08X", hr));
            }

            if (pThisPropertyStore)
            {
                bClosePropertyStore = TRUE;
            }
        }    
    }
 
    PropVariantInit(&propvar);

    if (SUCCEEDED (hr))
    {
        hr = pThisPropertyStore->GetValue(*pPropertyKey, &propvar);
        if (FAILED(hr))
        {
            WIAS_ERROR((g_hInst, "IPropertyStore::GetValue failed, hr = 0x%08X", hr));
        }
    }

    if ((SUCCEEDED(hr)) && (VT_EMPTY == propvar.vt))
    {
        hr = E_FAIL;
        WIAS_ERROR((g_hInst, "IPropertyStore::GetValue returned no value (VT_EMPTY), hr = 0x%08X", hr));
    }

    if ((SUCCEEDED(hr)) && (VT_LPWSTR != propvar.vt))
    {
        hr = E_UNEXPECTED;
        WIAS_ERROR((g_hInst, "IPropertyStore::GetValue returned an unexpected PROPVARIANT type, hr = 0x%08X", hr));
    }

    if (SUCCEEDED (hr))
    {
        *pbstrPropertyValue = SysAllocString(propvar.pwszVal);
        if (!(*pbstrPropertyValue))
        {
            hr = E_OUTOFMEMORY;
            WIAS_ERROR((g_hInst, 
                "Cannot make a copy of the value returned by IPropertyStore::GetValue, %ws, hr = 0x%08X", propvar.pwszVal, hr));
        }
    }

    PropVariantClear(&propvar);

    if ((bClosePropertyStore) && (pThisPropertyStore))
    {
        pThisPropertyStore->Release();
        pThisPropertyStore = NULL;
    }

    return hr;
}