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

📄 ringtoneiterator.cpp

📁 一个WinCE6。0下的IP phone的源代码
💻 CPP
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
#include "ringtoneiterator.h"

// The FindFirst/NextFile filter for ring tone files
const WCHAR c_wszRingToneFindFmt[] = L"\\Windows\\*.wav";

// The sprintf format to use when reconstructing the path
const WCHAR c_wszRingTonePathFmt[] = L"\\Windows\\%s.wav";

const WCHAR c_wszFileType[] = L"wav";

/*------------------------------------------------------------------------------
    CRingtoneIterator::CRingtoneIterator
    
    Ctor - gets all the installed ringtones
------------------------------------------------------------------------------*/
CRingtoneIterator::CRingtoneIterator()
{
    HRESULT hr              = S_OK;

    //The handle to use for the file search
    HANDLE hfind            = NULL;
    WCHAR wszPath[MAX_PATH] = L"";

    WIN32_FIND_DATA ffd = {0};

    // Search the ring tone path for items
    hfind = FindFirstFile(
        c_wszRingToneFindFmt,
        &ffd
        );
        
    if(hfind == INVALID_HANDLE_VALUE)
    {
        hr = HRESULT_FROM_WIN32(GetLastError());
    }

    if (SUCCEEDED(hr))
    {
        do
        {
            BSTR bstrRingTonePath = NULL;
            WCHAR wszFileName[MAX_PATH] = L"";

            //Get the path from the filename
            hr = PathToFileName(
                ffd.cFileName, 
                wszFileName, 
                _countof(wszFileName)
                );
            if (FAILED(hr))
            {
                continue;
            }

            hr = ConstructPath(
                wszFileName, 
                wszPath, 
                _countof(wszPath)
                );
            if (FAILED(hr))
            {    
                continue;
            }
        
            // Allocate a bstr to store this string as
            bstrRingTonePath = SysAllocString(wszPath);
            if (bstrRingTonePath == NULL)
            {
                hr = E_OUTOFMEMORY;
            }

            //put the path in the vector
            if (SUCCEEDED(hr))
            {
                if (!m_rlRingTones.push_back(bstrRingTonePath))
                {
                    hr = E_OUTOFMEMORY;
                }
            }        
        
        }
        while(SUCCEEDED(hr) && FindNextFile(hfind, &ffd));
    }

    if(SUCCEEDED(hr) && GetLastError() != ERROR_NO_MORE_FILES)
    {
        hr = HRESULT_FROM_WIN32(GetLastError());
    }

    // Clean up
    if(hfind != NULL)
    {
        FindClose(hfind);
        hfind = NULL;
    }
}


/*------------------------------------------------------------------------------
    CRingtoneIterator::~CRingtoneIterator
    
    Destructor, cleans up the vector
------------------------------------------------------------------------------*/
CRingtoneIterator::~CRingtoneIterator()
{
    RingtoneList::iterator  it;
    for (it = m_rlRingTones.begin(); it != m_rlRingTones.end(); it++)
    {
        //Each member of the list is an allocated bstr.
        //it needs to be freed
        SysFreeString(*it);
        *it = NULL;
    }

    //clear out the pointers
    m_rlRingTones.clear();
}

/*------------------------------------------------------------------------------
    CRingtoneIterator::GetRingtoneCount
    
    Returns the number of ringtones retrieved
------------------------------------------------------------------------------*/
INT CRingtoneIterator::GetRingtoneCount()
{
    return m_rlRingTones.size();
}

/*------------------------------------------------------------------------------
    CRingtoneIterator::GetRingtoneAt
    
    Returns a specified ringtone, formatted
------------------------------------------------------------------------------*/
HRESULT CRingtoneIterator::GetRingtoneAt(
    UINT idxRingtone, 
    WCHAR * wszOutBuf, 
    UINT cchOutBuf
    )
{
    if (idxRingtone >= m_rlRingTones.size())
    {
        ASSERT(FALSE);
        return E_UNEXPECTED;
    }

    return PathToFileName(
        m_rlRingTones[idxRingtone],
        wszOutBuf, 
        cchOutBuf
        );
}

/*------------------------------------------------------------------------------
    CRingtoneIterator::PathToFileName
    
    Converts a fully qualified path to a filename
    
    Parameters:
        wszFileName: outparam - the filename
        cchFileName: length of the filename buffer
------------------------------------------------------------------------------*/
HRESULT  CRingtoneIterator::PathToFileName(
    const WCHAR *c_wszPath, 
    WCHAR *wszFileName, 
    UINT cchFileName
    )
{
    //Trusted function
    PREFAST_ASSERT(c_wszPath != NULL && wszFileName != NULL && cchFileName <= MAX_PATH);

    //Copy the path over so it doesn't get modified
    WCHAR   wszBuffer[MAX_PATH] = L"";

    StringCchCopy(
        wszBuffer, 
        _countof(wszBuffer),
        c_wszPath
        );

    // Strip out the path for this file
    WCHAR *wszName = wcsrchr(wszBuffer, L'\\');
    if(! wszName)
    {
        wszName = (WCHAR*)wszBuffer;
    }
    else
    {
        wszName++;
    }

    // Strip the extension, if any
    WCHAR *wszExtension = wcsrchr(wszName, L'.');
    if(! wszExtension)
    {
        ASSERT(FALSE);
        return E_UNEXPECTED;
    }

    *wszExtension = L'\0';
    if (wcsicmp(wszExtension + 1, c_wszFileType) != 0)
    {
        ASSERT(FALSE);
        return E_UNEXPECTED;
    }

    StringCchCopy(
        wszFileName, 
        cchFileName, 
        wszName
        );
    return S_OK;
}

/*------------------------------------------------------------------------------
    CRingtoneIterator::ConstructPath
    
    Turns a file name back into the fully-qualified path name 
    for playing/setting the ringtone
------------------------------------------------------------------------------*/
HRESULT CRingtoneIterator::ConstructPath(
    const WCHAR * c_wszFile, 
    WCHAR * wszOutBuf, 
    UINT cchOutBuf
    )
{
    if (! c_wszFile || ! c_wszFile[0])
    {
        ASSERT(FALSE);
        return E_INVALIDARG;
    }

    StringCchPrintf(
        wszOutBuf, 
        cchOutBuf, 
        c_wszRingTonePathFmt, 
        c_wszFile
        );
    return S_OK;
}

⌨️ 快捷键说明

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