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

📄 utility.cpp

📁 Windows CE 6.0 Server 源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft shared
// source or premium shared source license agreement under which you licensed
// this source code. If you did not accept the terms of the license agreement,
// you are not authorized to use this source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the SOURCE.RTF on your install media or the root of your tools installation.
// THE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
//+---------------------------------------------------------------------------------
//
//
// File:
//      Utility.cpp
//
// Contents:
//
//      Implementation of various utilities.
//
//----------------------------------------------------------------------------------

#include "Headers.h"
#include "strsafe.h"

#ifdef UNDER_CE
#include "WinCEUtils.h"
#endif


////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: HRESULT QueryInterfaceRider(const InterfaceRider *pRider, void *pvThis, REFIID iid, void **ppvObject)
//
//  parameters:
//          
//  description:
//          Performs table driven query interface
//  returns:
//          
////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT QueryInterfaceRider(const InterfaceRider *pRider, void *pvThis, REFIID iid, void **ppvObject)
{
    if(! ppvObject)
        return E_INVALIDARG;
    *ppvObject = 0;

    const InterfaceRiderElement *pElem = pRider->elv;
    for(int iElem = 0; iElem < pRider->elc; iElem ++, pElem ++)
    {
        if(*pElem->pIID == iid)
        {
            *ppvObject = reinterpret_cast<void*>(reinterpret_cast<BYTE*>(pvThis) + pElem->offs);
            ASSERT(*ppvObject);
            reinterpret_cast<IUnknown*>(*ppvObject)->AddRef();
            return S_OK;
        }
    }
    return E_NOINTERFACE;
}
////////////////////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: HRESULT FindFactoryProduct(REFCLSID rclsid, const FactoryRider *pFactoryRider, const FactoryProduct **ppFactProd)
//
//  parameters:
//          
//  description:
//          Finds matching factory product in FactoryRider table
//  returns:
//          
////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT FindFactoryProduct(REFCLSID rclsid, const FactoryRider *pFactoryRider, const FactoryProduct **ppFactProd)
{
    ASSERT(ppFactProd);

    const FactoryProduct *pElem = pFactoryRider->elv;
    for(int iElem = 0; iElem < pFactoryRider->elc; iElem ++, pElem ++)
    {
        if(*pElem->pCLSID == rclsid)
        {
            *ppFactProd = pElem;
            return S_OK;
        }
    }

    return CLASS_E_CLASSNOTAVAILABLE;
}
////////////////////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: HRESULT FreeAndCopyBSTR(BSTR &pbstrDst, const WCHAR *pszSrc)
//
//  parameters:
//
//  description:
//      Frees bstr at pbstrDst location and stores the copy of pszSrc there
//  returns: 
//
////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT FreeAndCopyBSTR(BSTR &pbstrDst, const WCHAR *pszSrc)
{
    BSTR bstr   = 0;
    int  length = 0;

    if(pszSrc != 0 && (length = wcslen(pszSrc)) > 0)
    {
        bstr = ::SysAllocStringLen(pszSrc, length);

        if(bstr == 0)
        {
            return E_OUTOFMEMORY;
        }
    }

    ::SysFreeString(pbstrDst);

    pbstrDst = bstr;

    return S_OK;
}
////////////////////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: HRESULT FreeAndCopyBSTR(BSTR &pbstrDst, const WCHAR *pszSrc, int length)
//
//  parameters:
//          
//  description:
//          Frees bstr at pbstrDst location and stores the copy of pszSrc there (length)
//  returns:
//          
////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT FreeAndCopyBSTR(BSTR &pbstrDst, const WCHAR *pszSrc, int length)
{
    BSTR bstr = 0;

    if(pszSrc != 0 && length > 0)
    {
        bstr = ::SysAllocStringLen(pszSrc, length);

        if(bstr == 0)
        {
            return E_OUTOFMEMORY;
        }
    }

    ::SysFreeString(pbstrDst);

    pbstrDst = bstr;

    return S_OK;
}
////////////////////////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: HRESULT CopyBSTR(BSTR &pbstrDst, const WCHAR *pszSrc, int length)
//
//  parameters:
//          
//  description:
//          Creates copy of BSTR w/ length
//  returns:
//          
////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT CopyBSTR(BSTR &pbstrDst, const WCHAR *pszSrc, int length)
{
    ASSERT(pbstrDst == 0);
    BSTR bstr = 0;

    if(pszSrc != 0 && length > 0)
    {
        bstr = ::SysAllocStringLen(pszSrc, length);

        if(bstr == 0)
        {
            return E_OUTOFMEMORY;
        }
    }

    pbstrDst = bstr;
    return S_OK;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: HRESULT CopyBSTR(BSTR &pbstrDst, const WCHAR *pszSrc)
//
//  parameters:
//          
//  description:
//          Creates copy of BSTR.
//  returns:
//          
////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT CopyBSTR(BSTR &pbstrDst, const WCHAR *pszSrc)
{
    ASSERT(pbstrDst == 0);

    BSTR bstr   = 0;
    int  length = 0;

    if(pszSrc != 0 && (length = wcslen(pszSrc)) > 0)
    {
        bstr = ::SysAllocStringLen(pszSrc, length);

        if(bstr == 0)
        {
            return E_OUTOFMEMORY;
        }
    }

    pbstrDst = bstr;
    return S_OK;
}


////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: HRESULT CatBSTRs(BSTR &pbstrDst, const WCHAR *pStr1, int len1, const WCHAR *pStr2, int len2)
//
//  parameters:
//
//  description:
//        Concatenates two BSTRs, storing the result in pbstrDst.
//  returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT CatBSTRs(BSTR &pbstrDst, const WCHAR *pStr1, int len1, const WCHAR *pStr2, int len2)
{
    ASSERT(pbstrDst == 0);

    BSTR bstr   = 0;

    if ((pStr1 != 0 && len1 > 0) || (pStr2 != 0 && len2 > 0))
    {
        bstr = ::SysAllocStringLen(0, len1 + len2);

        if (bstr == 0)
        {
            return E_OUTOFMEMORY;
        }

        if (pStr1 != 0 && len1 > 0)
        {
            wcsncpy(bstr, pStr1, len1);
        }
        else
        {
            len1 = 0;
        }

        if (pStr2 != 0 && len2 > 0)
        {
            wcsncpy(bstr + len1, pStr2, len2);
        }
        else
        {
            len2 = 0;
        }

        bstr[len1 + len2] = '\0';

    }

    pbstrDst = bstr;
    return S_OK;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: HRESULT FreeAndStoreBSTR(BSTR &bstrDst, const BSTR bstrSrc)
//
//  parameters:
//          
//  description:
//          Frees BSTR stored at bstrDst and assigns bstrSrc into that location
//  returns:
//          
////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT FreeAndStoreBSTR(BSTR &bstrDst, const BSTR bstrSrc)
{
    BSTR bstr = reinterpret_cast<BSTR>(InterlockedExchangePointer(&bstrDst, bstrSrc));
    ::SysFreeString(bstr);
    return S_OK;
}


////////////////////////////////////////////////////////////////////////////////////////////////////
//  function: HRESULT AtomicFreeAndCopyBSTR(BSTR &pbstrDst, const WCHAR *pszSrc)
//
//  parameters:
//
//  description:
//
//  returns: 
//

⌨️ 快捷键说明

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