ファイルからストリーム(IStream)を作成 (Emulate_SHCreateStreamOnFile)

IE5.0以上にあるSHLWAPIの同名の関数と互換
(2007.05.17)
#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.05.08 : CreateStreamFromBuffer()を追加 (sha)
// '04.08.02 : GetStreamSize()を改良 (abu)
// '04.08.02 : CRestoreSeekを追加 (abu)
// '04.08.06 : Emulate_IStream_Read()等を追加 (sha)
// '04.08.06 : Emulate_SHCreateStreamOnFile()をShlwapiHelper.hから移行 (sha)
// '04.08.06 : RewindSeekPosition()から、ResetSeekPosition()に変更 (sha)
// '04.11.14 : コメントアウトされていた不要なコードを削除 (sha)
// '05.01.08 : 関数群を整理 (sha)
// '05.07.11 : CResetSeekを追加 (sha)
// '05.07.11 : CRestoreSeekをCRestoreSeekに名称変更 (sha)
// '05.07.11 : 関数群を整理 (sha)
// '05.07.12 : コードの整備 (sha)
// '05.07.12 : CompareStreamを、CompareStream.hに分離 (sha)
// '05.10.07 : Emulete_SHIsEmptyStream()を追加 (sha)
// '05.11.26 : Emulete_SHIsEmptyStream()の不具合を修正 (yam)
// '06.01.24 : Emulete_SHIsEmptyStream()のコード修正 (sug)
// '06.04.05 : Emulete_IStream_Copy()を追加 (sha)
// '06.05.04 : Emulete_IStream_Write() を追加 (sha)
// '06.05.04 : STREAMHELPER_USE_EMULATE を定義 (sha)
// '06.05.04 : ReadStream(), WriteStream() を定義 (sha)
// '07.03.23 : SHCreateMemStream() のコード修正 (his)
// '07.04.06 : コードの整備 (sha)
// '07.04.11 : コードの整備 (sha)
// '07.04.12 : コードの整備 & CreateStreamFromResource()を追加 (sha)
// '07.05.17 : コードの整備 (sha)
 
#include <atlfile.h>
 
#define _STREAMHELPER_H
 
#ifndef _STREAMHELPER_USE_EMULATE
#define _STREAMHELPER_USE_EMULATE
#endif
 
 
/////////////////////////////////////////////////////////////////////////////
// CStreamHelper
 
class CStreamHelper
{
protected:
    typedef CStreamHelper                        thisClass;
 
protected:
    CStreamHelper();                            // 実装しない
    virtual ~CStreamHelper();
 
    // 省略。。
 
public:
    // ファイルからIStream*を作成する
    //  → 現在は、読み込みのみサポート
    // SHLWAPI 5.00 互換
    static HRESULT Emulate_SHCreateStreamOnFile(LPCTSTR szFileName, DWORD grfMode, IStream **ppstm)
    {
#ifdef _STREAMHELPER_USE_EMULATE
        if (grfMode != STGM_READ)
        {
            ATLASSERT(0);
            return E_NOTIMPL;
        }
        if (ppstm == NULL)
        {
            return E_POINTER;
        }
 
        HRESULT hr = S_OK;
 
        *ppstm = NULL;
 
        CAtlFile file;
 
        hr = file.Create(szFileName, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING);
        if (FAILED(hr))
        {
            return hr;
        }
 
        ULONGLONG ullSize = 0;
        hr = file.GetSize(ullSize);
        if (FAILED(hr))
        {
            return hr;
        }
 
        if (ullSize == 0)
        {
            return E_FAIL;
        }
 
        CHeapPtr<BYTE, CGlobalAllocator> buffer;
        bool br = buffer.AllocateBytes((size_t)ullSize);
        if (br != true)
        {
            return E_FAIL;
        }
 
        DWORD dwReadBytes = 0;
        hr = file.Read(buffer, (DWORD)ullSize, dwReadBytes);
        if (FAILED(hr))
        {
            return hr;
        }
        ATLASSERT(ullSize == dwReadBytes);
 
        file.Close();
 
        IStreamPtr spStream = NULL;
        hr = ::CreateStreamOnHGlobal((HGLOBAL)buffer, TRUE, &spStream);
        if (FAILED(hr))
        {
            return hr;
        }
 
        *ppstm = spStream.Detach();
 
        return hr;
#else
        return ::SHCreateStreamOnFile(szFileName, grfMode, ppstm);
#endif
    }
 
 
    // 省略。。
};
一覧に戻る
© 2003 WAC.com All Right Reserved.