MSIデータベースのレコード(hRecord)を扱う単純なラッパクラス

(2005.01.14)
#pragma once
 
// '04.12.15 : 新規作成 (sug)
// '05.01.06 : テンプレート化 (sug)
 
#include <Msi.h>
#pragma comment (lib, "Msi.lib")
#include <MsiDefs.h>
#include <MsiQuery.h>
 
 
/////////////////////////////////////////////////////////////////////////////
// CMsiRecordT
 
template <bool t_bManaged>
class CMsiRecordT
{
public:
    CMsiRecordT()
    {
        m_hRecord = 0;
    }
    CMsiRecordT(MSIHANDLE hRecord)
    {
        m_hRecord = hRecord;
    }
    virtual ~CMsiRecordT()
    {
        if (t_bManaged && m_hRecord != 0)
        {
            this->CloseHandle();
        }
    }
 
public:
    bool IsValid()
    {
        if (m_hRecord == 0)
        {
            return false;
        }
 
        return true;
    }
    
public:
    void Attach(MSIHANDLE hRecord)
    {
        ATLASSERT(m_hRecord == 0);
        ATLASSERT(hRecord != 0);
        m_hRecord = hRecord;
    }
    MSIHANDLE Detach()
    {
        ATLASSERT(m_hRecord != 0);
        MSIHANDLE hRecord = m_hRecord;
        m_hRecord = 0;
        return hRecord;
    }
 
public:
    CMsiRecordT& operator=(MSIHANDLE hRecord)
    {
        m_hRecord = hRecord;
        return *this;
    }
    operator MSIHANDLE()
    { 
        return m_hRecord; 
    }
 
public:
    UINT CloseHandle()
    {
        ATLASSERT(m_hRecord != 0);
        MSIHANDLE hRecord = m_hRecord;
        m_hRecord = 0;
        return ::MsiCloseHandle(hRecord);
    }
 
public:
    UINT ClearData()
    {
        ATLASSERT(m_hRecord != 0);
        return ::MsiRecordClearData(m_hRecord);
    }
    BOOL IsNull(UINT iField)
    {
        ATLASSERT(m_hRecord != 0);
        return ::MsiRecordIsNull(m_hRecord, iField);
    }
    UINT DataSize(UINT iField)
    {
        ATLASSERT(m_hRecord != 0);
        return ::MsiRecordDataSize(m_hRecord, iField);
    }
    UINT SetInteger(UINT iField, int iValue)
    {
        ATLASSERT(m_hRecord != 0);
        return ::MsiRecordSetInteger(m_hRecord, iField, iValue);
    }
    UINT SetString(UINT iField, LPCSTR szValue)
    {
        ATLASSERT(m_hRecord != 0);
        return ::MsiRecordSetString(m_hRecord, iField, szValue);
    }
    int GetInteger(UINT iField)
    {
        ATLASSERT(m_hRecord != 0);
        return ::MsiRecordGetInteger(m_hRecord, iField);
    }
#if defined(_WTL_USE_CSTRING) || defined(__ATLSTR_H__)
    UINT GetString(UINT iField, CString& strValueBuf)
    {
        ATLASSERT(m_hRecord != 0);
 
        strValueBuf.Empty();
        
        DWORD dwValueBuff = 0;
        UINT nr = ::MsiRecordGetString(m_hRecord, iField, _T(""), &dwValueBuff);
        if (nr != ERROR_MORE_DATA)
        {
            return nr;
        }
        dwValueBuff += 1;
 
        CString strTempBuf;
        strTempBuf.GetBufferSetLength(dwValueBuff);
        nr = ::MsiRecordGetString(m_hRecord, iField, strTempBuf.GetBuffer(), &dwValueBuff);
        if (nr != ERROR_SUCCESS)
        {
            strTempBuf.ReleaseBuffer();
            return nr;
        }
 
        strTempBuf.ReleaseBuffer();
        strValueBuf = strTempBuf;
 
        return nr;
    }
#endif // defined(_WTL_USE_CSTRING) || defined(__ATLSTR_H__)
    UINT GetString(UINT iField, LPSTR szValueBuf, DWORD *pcchValueBuf)
    {
        ATLASSERT(m_hRecord != 0);
        return ::MsiRecordGetString(m_hRecord, iField, szValueBuf, pcchValueBuf);
    }
    UINT GetFieldCount()
    {
        ATLASSERT(m_hRecord != 0);
        return ::MsiRecordGetFieldCount(m_hRecord);
    }
    UINT SetStream(UINT iField, LPCSTR szFilePath)
    {
        ATLASSERT(m_hRecord != 0);
        return ::MsiRecordSetStream(m_hRecord, iField, szFilePath);
    }
    UINT ReadStream(UINT iField, char *szDataBuf, DWORD *pcbDataBuf)
    {
        ATLASSERT(m_hRecord != 0);
        return ::MsiRecordReadStream(m_hRecord, iField, szDataBuf, pcbDataBuf);
    }
 
public:    
   MSIHANDLE m_hRecord;  
};
 
typedef CMsiRecordT<false>            CMsiRecordHandle;
typedef CMsiRecordT<true>            CMsiRecord;
一覧に戻る
© 2003 WAC.com All Right Reserved.