ドライブ名の列挙 (CEnumDriveNames)

(2003.09.18)
#pragma once
 
// WAC.com Class Liblary for Visual C++
// Copyright (C) WAC.com Inc. All rights reserved.
//
// This file is a part of the WAC.com Class Liblary.
// The use and distribution terms for this software are covered by the
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
// which can be found in the file CPL.TXT at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by
// the terms of this license. You must not remove this notice, or
// any other, from this software.
 
// '04.12.30 : コードの微調整 (sha)
// '06.08.14 : UNICODEビルド対応 (sha)
// '07.04.11 : コードの整備 (sha)
// '07.05.17 : コードの整備 (sha)
 
#include <vector>
 
 
/////////////////////////////////////////////////////////////////////////////
// CEnumDriveNames
 
class CEnumDriveNames
{
protected:
    typedef CEnumDriveNames                        thisClass;
 
protected:
    CEnumDriveNames();                            // 実装しない
    virtual ~CEnumDriveNames();
 
public:
#if 0
    static HRESULT EnumDriveNames(std::vector<CString>& strDriveNames)
    {
        strDriveNames.clear();
 
        int nPos = 0;
        DWORD dwDriveBits = ::GetLogicalDrives();
        while (dwDriveBits)
        {
            if (dwDriveBits & 1)
            {
                TCHAR szDriveName[_MAX_PATH] = { 0 };
                ::PathBuildRoot(szDriveName, nPos);                                        // SHLWAPI 4.71
 
                strDriveNames.push_back(szDrive);
            }
 
            dwDriveBits >>= 1;
            nPos++;
        }
 
        return S_OK;
    }
#else
    // 戻り値のドライブ名は、"C:\"の形式です。
    static HRESULT EnumDriveNames(std::vector<CString>& strDriveNames)
    {
        strDriveNames.clear();
 
        // バッファの確保
        DWORD dwBufferSize = ::GetLogicalDriveStrings(0, NULL);
        TCHAR* pBuffer = new TCHAR[dwBufferSize];
        if (pBuffer == NULL)
        {
            return E_OUTOFMEMORY;
        }
 
        ::GetLogicalDriveStrings(dwBufferSize, pBuffer);
 
        TCHAR* szDrive = pBuffer;
        size_t lLength = _tcslen(szDrive);
        while (lLength > 0)
        {
            strDriveNames.push_back(szDrive);
//            ATLTRACE(_T("%s\n"), szDrive);
 
            szDrive += (lLength + 1);
            lLength = _tcslen(szDrive);
        }
 
        // バッファの開放
        delete [] pBuffer;
 
        return S_OK;
    }
#endif
};
一覧に戻る
© 2003 WAC.com All Right Reserved.