MSIの製品情報(hProduct)を扱う単純なラッパクラス

(2005.01.14)
#pragma once
 
// '05.01.13 : 新規作成 (sug)
 
#include <Msi.h>
#pragma comment (lib, "Msi.lib")
#include <MsiDefs.h>
 
 
/////////////////////////////////////////////////////////////////////////////
// CMsiProductT
 
template <bool t_bManaged>
class CMsiProductT
{
public:
    CMsiProductT()
    {
        m_hProduct = 0;
    }
    CMsiProductT(MSIHANDLE hProduct)
    {
        m_hProduct = hProduct;
    }
    virtual ~CMsiProductT()
    {
        if (t_bManaged && m_hProduct != 0)
        {
            this->CloseHandle();
        }
    }
 
public:
    bool IsValid()
    {
        if (m_hProduct == 0)
        {
            return false;
        }
 
        return true;
    }
    
public:
    void Attach(MSIHANDLE hProduct)
    {
        ATLASSERT(m_hProduct == 0);
        ATLASSERT(hProduct != 0);
        m_hProduct = hProduct;
    }
    MSIHANDLE Detach()
    {    
        ATLASSERT(m_hProduct != 0);
        MSIHANDLE hProduct = m_hProduct;
        m_hProduct = 0;
        return hProduct;
    }
 
public:
    CMsiProductT& operator=(MSIHANDLE hProduct)
    {
        m_hProduct = hProduct;
        return *this;
    }
    operator MSIHANDLE()
    { 
        return m_hProduct; 
    }
 
public:
    UINT CloseHandle()
    {
        ATLASSERT(m_hProduct != 0);
        MSIHANDLE hProduct = m_hProduct;
        m_hProduct = 0;
        return ::MsiCloseHandle(hProduct);
    }
 
public:
#if defined(_WTL_USE_CSTRING) || defined(__ATLSTR_H__)
    HRESULT GetProperty(LPCTSTR szProperty, CString& strValueBuf)
    {
        ATLASSERT(m_hProduct != 0);
 
        strValueBuf.Empty();
 
        DWORD dwValueBuff = 0;
        UINT nr = ::MsiGetProductProperty(m_hProduct, szProperty, _T(""), &dwValueBuff);
        if (nr != ERROR_MORE_DATA)
        {
            return nr;
        }
 
        dwValueBuff += 1;
 
        strValueBuf.GetBufferSetLength(dwValueBuff);
        nr = ::MsiGetProductProperty(m_hProduct, szProperty, strValueBuf.GetBuffer(), &dwValueBuff);
        if (nr != ERROR_SUCCESS)
        {
            strValueBuf.ReleaseBuffer();
            return nr;
        }
 
        strValueBuf.ReleaseBuffer();
        
        return nr;
    }
#endif // defined(_WTL_USE_CSTRING) || defined(__ATLSTR_H__)
    UINT GetProperty(LPCSTR szProperty, LPSTR lpValueBuf, DWORD* pcchValueBuf)
    {
        ATLASSERT(m_hProduct != 0);
        return ::MsiGetProperty(m_hProduct, szProperty, lpValueBuf, pcchValueBuf);
    }
    UINT GetFeatureInfo(LPCSTR szFeature, DWORD* lpAttributes, LPSTR lpTitleBuf, DWORD* pcchTitleBuf, LPSTR lpHelpBuf, DWORD* pcchHelpBuf)
    {
        ATLASSERT(m_hProduct != 0);
        return ::MsiGetFeatureInfo(szFeature, lpAttributes, pTitleBuf, pcchTitleBuf, lpHelpBuf, pcchHelpBuf);
    }
 
public:
    static UINT OpenProduct(LPCTSTR szProduct, MSIHANDLE* hProduct)
    {
        ATLASSERT(hProduct != NULL);
        ATLASSERT(*hProduct == 0);
        return ::MsiOpenProduct(szProduct, hProduct);
    }
    static UINT OpenPackage(LPCSTR szPackagePath, MSIHANDLE* hProduct)
    {
        ATLASSERT(hProduct != NULL);
        ATLASSERT(*hProduct == 0);
        return ::MsiOpenPackage(szPackagePath, hProduct);
    }
    static UINT OpenPackageEx(LPCWSTR szPackagePath, DWORD dwOptions, MSIHANDLE* hProduct)
    {
        ATLASSERT(hProduct != NULL);
        ATLASSERT(*hProduct == 0);
        return ::MsiOpenPackageEx(szPackagePath, dwOptions, hProduct);
    }
 
public:
   MSIHANDLE m_hProduct;  
};
 
typedef CMsiProductT<false>            CMsiProductHandle;
typedef CMsiProductT<true>            CMsiProduct;
一覧に戻る
© 2003 WAC.com All Right Reserved.