⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wiahelpers.cpp

📁 winddk src目录下的WDM源码压缩!
💻 CPP
📖 第 1 页 / 共 5 页
字号:

/**
 * This function initializes child item properties
 * needed for this WIA driver's storage item.  The
 * WIA_DRIVER_ITEM_CONTEXT structure stored as the
 * the WIA driver item context will be used to
 * properly set the WIA_IPA_FILENAME_EXTENSION property
 * value.
 *
 * @param pWiasContext
 *                  Pointer to the WIA item context
 * @param bRootItem TRUE - Legacy WIA properties that belong on the root item
 *                  of the device will be added.
 *                  FALSE - Child item WIA properties will be added to the item.
 * @param bFolderItem 
 *                  TRUE - storage folder (WIA_CATEGORY_FOLDER)
 *                  FALSE - finished file (WIA_CATEGORY_FINISHED_FILE) 
 * @return
 */
HRESULT InitializeWIAStorageItemProperties(
    __in    BYTE        *pWiasContext,
            BOOL        bRootItem,
            BOOL        bFolderItem)
{
    HRESULT hr = E_INVALIDARG;
    if(pWiasContext)
    {
        CWIAPropertyManager PropertyManager;
        LONG                lItemType = 0;
        hr = wiasGetItemType(pWiasContext,&lItemType);
        if(SUCCEEDED(hr))
        {
            GUID guidItemCategory = bFolderItem ? WIA_CATEGORY_FOLDER : WIA_CATEGORY_FINISHED_FILE;
            hr = PropertyManager.AddProperty(WIA_IPA_ITEM_CATEGORY,WIA_IPA_ITEM_CATEGORY_STR,RN,guidItemCategory);
            if(SUCCEEDED(hr))
            {
                if(!(lItemType & WiaItemTypeGenerated))
                {
                    WIA_DRIVER_ITEM_CONTEXT *pWiaDriverItemContext = NULL;
                    hr = wiasGetDriverItemPrivateContext(pWiasContext,(BYTE**)&pWiaDriverItemContext);
                    if(SUCCEEDED(hr))
                    {
                        if(lItemType & WiaItemTypeStorage)
                        {
                            //
                            // This is the parent storage item, update the number of items stored and
                            // proper access rights
                            //

                            LONG lAccessRights = WIA_ITEM_READ;
                            hr = PropertyManager.AddProperty(WIA_IPA_ACCESS_RIGHTS ,WIA_IPA_ACCESS_RIGHTS_STR ,RN,(LONG) 0);
                            if(SUCCEEDED(hr))
                            {
                                hr = PropertyManager.AddProperty(WIA_IPA_ITEMS_STORED,
                                                             WIA_IPA_ITEMS_STORED_STR,
                                                             RN,
                                                             pWiaDriverItemContext->lNumItemsStored);
                                if(FAILED(hr))
                                {
                                    WIAS_ERROR((g_hInst, "Failed to add WIA_IPA_ITEMS_STORED property to the property manager, hr = 0x%lx",hr));
                                }
                            }
                            else
                            {
                                WIAS_ERROR((g_hInst, "Failed to add WIA_IPA_ACCESS_RIGHTS property to the property manager, hr = 0x%lx",hr));
                            }

                            // 
                            // Support creation of child items underneath the root storage item:
                            //

                            BOOL bChildItemCreation = TRUE;
                            hr = PropertyManager.AddProperty(WIA_IPS_SUPPORTS_CHILD_ITEM_CREATION, WIA_IPS_SUPPORTS_CHILD_ITEM_CREATION_STR, RN, bChildItemCreation);
                            if(FAILED(hr))
                            {
                                WIAS_ERROR((g_hInst, "Failed to add WIA_IPS_SUPPORTS_CHILD_ITEM_CREATION(TRUE) property to the property manager, hr = 0x%lx",hr));
                            }
                        }
                        else
                        {
                            //
                            // This must be a child item of some kind
                            //

                            if(SUCCEEDED(hr))
                            {
                                LONG lItemSize = 0;
                                hr = PropertyManager.AddProperty(WIA_IPA_ITEM_SIZE ,WIA_IPA_ITEM_SIZE_STR ,RN,lItemSize);
                            }

                            if(SUCCEEDED(hr))
                            {
                                GUID guidPreferredFormat = WiaImgFmt_UNDEFINED;
                                hr = PropertyManager.AddProperty(WIA_IPA_PREFERRED_FORMAT ,WIA_IPA_PREFERRED_FORMAT_STR ,RN,guidPreferredFormat);
                            }

                            if(SUCCEEDED(hr))
                            {
                                CBasicDynamicArray<GUID> guidFormatArray;
                                guidFormatArray.Append(WiaImgFmt_UNDEFINED);
                                hr = PropertyManager.AddProperty(WIA_IPA_FORMAT ,WIA_IPA_FORMAT_STR ,RWL,guidFormatArray[0],guidFormatArray[0],&guidFormatArray);
                            }

                            if(SUCCEEDED(hr))
                            {
                                CBasicDynamicArray<LONG> lTymedArray;
                                lTymedArray.Append(TYMED_FILE);
                                hr = PropertyManager.AddProperty(WIA_IPA_TYMED ,WIA_IPA_TYMED_STR ,RWL,lTymedArray[0],lTymedArray[0],&lTymedArray);
                            }

                            if(SUCCEEDED(hr))
                            {
                                // TBD: A small buffer size was used here to allow slower transfers with more progress.  Real
                                //      drivers should use a higher value to increase performance.
                                LONG lBufferSize = DEFAULT_BUFFER_SIZE;
                                hr = PropertyManager.AddProperty(WIA_IPA_BUFFER_SIZE ,WIA_IPA_BUFFER_SIZE_STR ,RN,lBufferSize);
                            }

                            if(SUCCEEDED(hr))
                            {
                                BSTR bstrFileExtension = NULL;
                                if(pWiaDriverItemContext->bstrStorageDataPath)
                                {
                                    hr = GetFileExtensionFromPath(pWiaDriverItemContext->bstrStorageDataPath, &bstrFileExtension);
                                }
                                else
                                {
                                    bstrFileExtension = SysAllocString(L"UNDEFINED");
                                    hr = S_OK;
                                }
                                if(SUCCEEDED(hr))
                                {
                                    if(bstrFileExtension)
                                    {
                                        hr = PropertyManager.AddProperty(WIA_IPA_FILENAME_EXTENSION ,WIA_IPA_FILENAME_EXTENSION_STR ,RN,bstrFileExtension);
                                        SysFreeString(bstrFileExtension);
                                        bstrFileExtension = NULL;
                                    }
                                    else
                                    {
                                        hr = E_OUTOFMEMORY;
                                        WIAS_ERROR((g_hInst, "Could not allocate the file name extension property value, hr = 0x%lx.",hr));
                                    }
                                }
                                else
                                {
                                    WIAS_ERROR((g_hInst, "Failed to extract file extension from path (%ws), hr = 0x%lx",pWiaDriverItemContext->bstrStorageDataPath,hr));
                                }
                            }

                            if (SUCCEEDED(hr))
                            {
                                // 
                                // Support creation of child items underneath folder items:
                                //

                                if (bFolderItem)
                                {
                                    BOOL bChildItemCreation = TRUE;
                                    hr = PropertyManager.AddProperty(WIA_IPS_SUPPORTS_CHILD_ITEM_CREATION, WIA_IPS_SUPPORTS_CHILD_ITEM_CREATION_STR, RN, bChildItemCreation);
                                    if(FAILED(hr))
                                    {
                                        WIAS_ERROR((g_hInst, "Failed to add WIA_IPS_SUPPORTS_CHILD_ITEM_CREATION property to the property manager, hr = 0x%lx",hr));
                                    }
                                }
                                else
                                {
                                    // 
                                    // This is a child item (image item). So we add some image only properties here.
                                    // 
                                    // This sample driver supports only 24-bpp color data. A real driver would have to consider separate WIA_IPA_DEPTH
                                    // values for each supported WIA_IPA_DATATYPE and update the available and current WIA_IPA_DEPTH values
                                    // every time the current WIA_IPA_DATATYPE is changed.
                                    //

                                    CBasicDynamicArray<LONG> lDataTypeArray;
                                    lDataTypeArray.Append(WIA_DATA_COLOR);
                                    hr = PropertyManager.AddProperty(WIA_IPA_DATATYPE ,WIA_IPA_DATATYPE_STR ,RWL,lDataTypeArray[0],lDataTypeArray[0],&lDataTypeArray);
                                    if(FAILED(hr))
                                    {
                                        WIAS_ERROR((g_hInst, "Failed to add WIA_IPA_DATATYPE property to the property manager, hr = 0x%lx",hr));
                                    }

                                    CBasicDynamicArray<LONG> lBitDepthArray;
                                    lBitDepthArray.Append(24);
                                    hr = PropertyManager.AddProperty(WIA_IPA_DEPTH ,WIA_IPA_DEPTH_STR ,RWLC,lBitDepthArray[0],lBitDepthArray[0],&lBitDepthArray);
                                    if(FAILED(hr))
                                    {
                                        WIAS_ERROR((g_hInst, "Failed to add WIA_IPA_DEPTH property to the property manager, hr = 0x%lx",hr));
                                    }
                                }

                            }
                        }
                    }
                    else
                    {
                        WIAS_ERROR((g_hInst, "Failed to obtain the WIA_DRIVER_ITEM_CONTEXT structure from the WIA driver item, hr = 0x%lx",hr));
                    }
                }
                else
                {
                    WIAS_TRACE((g_hInst,"WIA item was created by application"));

                    // 
                    // Support creation of child items underneath generated folder items:
                    //
                    if (bFolderItem)
                    {
                        BOOL bChildItemCreation = TRUE;
                        hr = PropertyManager.AddProperty(WIA_IPS_SUPPORTS_CHILD_ITEM_CREATION, WIA_IPS_SUPPORTS_CHILD_ITEM_CREATION_STR, RN, bChildItemCreation);
                        if(FAILED(hr))
                        {
                            WIAS_ERROR((g_hInst, "Failed to add WIA_IPS_SUPPORTS_CHILD_ITEM_CREATION property to the property manager, hr = 0x%lx",hr));
                        }
                    }
                }
            }
            else
            {
                WIAS_ERROR((g_hInst, "Failed to add WIA_IPA_ITEM_CATEGORY property to the property manager, hr = 0x%lx",hr));
            }
        }
        else
        {
            WIAS_ERROR((g_hInst, "Failed to get the WIA item type, hr = 0x%lx",hr));
        }

        if(SUCCEEDED(hr))
        {
            hr = PropertyManager.SetItemProperties(pWiasContext);
            if(FAILED(hr))
            {
                WIAS_ERROR((g_hInst, "CWIAPropertyManager::SetItemProperties failed to set WIA storage item properties, hr = 0x%lx",hr));
            }
        }
    }
    else
    {
        WIAS_ERROR((g_hInst, "Invalid parameters were passed"));
    }
    return hr;
}

/**
 * This function returns the WIA driver item context
 * data stored with the driver item.  NOT ALL DRIVER ITEMS
 * HAVE CONTEXTS STORED WITH THEM.  The context is initialized
 * and stored at WIA item creation.  See CreateWIAChildItem
 * function.
 *
 * @param pWiasContext
 *               Pointer to the WIA item context
 * @param ppWiaDriverItemContext
 *               Pointer to the WIA driver item context data
 * @return
 */
HRESULT wiasGetDriverItemPrivateContext(
    __in    BYTE    *pWiasContext,
    __out   BYTE    **ppWiaDriverItemContext)
{
    HRESULT hr = E_INVALIDARG;
    if((pWiasContext)&&(ppWiaDriverItemContext))
    {
        IWiaDrvItem *pIWiaDrvItem   = NULL;
        hr = wiasGetDrvItem(pWiasContext, &pIWiaDrvItem);
        if(SUCCEEDED(hr))
        {
            hr = pIWiaDrvItem->GetDeviceSpecContext(ppWiaDriverItemContext);
            //
            // The caller will handle the failure case.  A failure, may mean that the
            // the WIA item does not have a private device specific context
            // stored.  This is OK, because is is not required.
            //
        }
        else
        {
            WIAS_ERROR((g_hInst, "Failed to get the WIA driver item from the application item, hr = 0x%lx",hr));
        }
    }
    else
    {
        WIAS_ERROR((g_hInst, "Invalid parameters were passed"));
    }
    return hr;
}

/**
 * This function returns the application item's
 * parent WIA item context.  The returned context
 * can be used to access the parent's WIA property
 * set.
 *
 * @param pWiasContext
 *               Pointer to the WIA item context
 * @param ppWiasContext
 *               Pointer to the parent WIA item context
 * @return
 */
HRESULT wiasGetAppItemParent(
    __in    BYTE    *pWiasContext,
    __out   BYTE    **ppWiasContext)
{
    HRESULT hr = E_INVALIDARG;
    if((pWiasContext) && (ppWiasContext))
    {
        // FIX! This helper function is actually getting the backing driver
        //      item and returning it as the parent.  This will make the
        //      driver always associate the newly created child item with its
        //      proper backing driver item.  This function should be fixed to
        //      return the parent application item.
        //

        IWiaDrvItem *pIWiaDrvItemParent = NULL;
        hr = wiasGetDrvItem(pWiasContext,&pIWiaDrvItemParent);
        if(SUCCEEDED(hr))
        {
            BSTR bstrFullItemName = NULL;
            hr = pIWiaDrvItemParent->GetFullItemName(&bstrFullItemName);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -