现在的位置: 首页 > 综合 > 正文

import引用组件类型库,自动生成.tli文件

2013年03月22日 ⁄ 综合 ⁄ 共 4973字 ⁄ 字号 评论关闭

#import引用组件类型库,自动生成.tli文件。
inline long IA::GetnValue ( ) {
    long _result = 0;
    HRESULT _hr = get_nValue(&_result);
    if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
    return _result;
}

inline void IA::PutnValue ( long pVal ) {
    HRESULT _hr = put_nValue(pVal);
    if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
}

这样,就可以在使用组件接口时抛出异常,并且自动获得返回值。

抛出的异常_com_error定义如下:

class _com_error {
public
:
    
//
 Constructors
    
//
    _com_error(HRESULT hr,
               IErrorInfo
* perrinfo =
 NULL,
               
bool fAddRef = falsethrow
();
    _com_error(
const _com_error& that) throw
();

    // Destructor
    
//
    virtual ~_com_error() throw
();

    // Assignment operator
    
//
    _com_error& operator=(const _com_error& that) throw
();

    // Accessors
    
//
    HRESULT Error() const throw
();
    WORD WCode() 
const throw
();
    IErrorInfo 
* ErrorInfo() const throw
();

    // IErrorInfo method accessors
    
//
    _bstr_t Description() const throw
(_com_error);
    DWORD HelpContext() 
const throw
();
    _bstr_t HelpFile() 
const throw
(_com_error);
    _bstr_t Source() 
const throw
(_com_error);
    GUID GUID() 
const throw
();

    // FormatMessage accessors
    const TCHAR * ErrorMessage() const throw();

    // EXCEPINFO.wCode <-> HRESULT mappers
    static HRESULT WCodeToHRESULT(WORD wCode) throw();
    
static WORD HRESULTToWCode(HRESULT hr) throw
();

private:
    
enum
 {
        WCODE_HRESULT_FIRST 
= MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 0x200
),
        WCODE_HRESULT_LAST 
= MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF+10- 1

    };
    
const HRESULT           m_hresult;
    IErrorInfo 
*
            m_perrinfo;
    mutable TCHAR 
*
         m_pszMsg;
};

inline _com_error::_com_error(HRESULT hr,
                              IErrorInfo* perrinfo,
                              bool fAddRef) throw()
    : m_hresult(hr), m_perrinfo(perrinfo), m_pszMsg(NULL)
{
    if (m_perrinfo != NULL && fAddRef) {
        m_perrinfo->AddRef();
    }
}
inline _com_error::_com_error(const _com_error& that) throw()
    : m_hresult(that.m_hresult), m_perrinfo(that.m_perrinfo), m_pszMsg(NULL)
{
    if (m_perrinfo != NULL) {
        m_perrinfo->AddRef();
    }
}
inline _com_error::~_com_error() throw()
{
    if (m_perrinfo != NULL) {
        m_perrinfo->Release();
    }
    if (m_pszMsg != NULL) {
        LocalFree((HLOCAL)m_pszMsg);
    }
}
inline _com_error& _com_error::operator=(const _com_error& that) throw()
{
    if (this != &that) {
        this->_com_error::~_com_error();
        this->_com_error::_com_error(that);
    }
    return *this;
}
inline HRESULT _com_error::Error() const throw()
{
    return m_hresult;
}
inline WORD _com_error::WCode() const throw()
{
    return HRESULTToWCode(m_hresult);
}
inline IErrorInfo * _com_error::ErrorInfo() const throw()
{
    if (m_perrinfo != NULL) {
        m_perrinfo->AddRef();
    }
    return m_perrinfo;
}
inline _bstr_t _com_error::Description() const throw(_com_error)
{
    BSTR bstr = NULL;
    if (m_perrinfo != NULL) {
        m_perrinfo->GetDescription(&bstr);
    }
    return _bstr_t(bstr, false);
}
inline DWORD _com_error::HelpContext() const throw()
{
    DWORD dwHelpContext = 0;
    if (m_perrinfo != NULL) {
        m_perrinfo->GetHelpContext(&dwHelpContext);
    }
    return dwHelpContext;
}
inline _bstr_t _com_error::HelpFile() const throw(_com_error)
{
    BSTR bstr = NULL;
    if (m_perrinfo != NULL) {
        m_perrinfo->GetHelpFile(&bstr);
    }
    return _bstr_t(bstr, false);
}
inline _bstr_t _com_error::Source() const throw(_com_error)
{
    BSTR bstr = NULL;
    if (m_perrinfo != NULL) {
        m_perrinfo->GetSource(&bstr);
    }
    return _bstr_t(bstr, false);
}
inline _GUID _com_error::GUID() const throw()
{
    _GUID guid;
    memcpy(&guid, &__uuidof(NULL), sizeof(_GUID));
    if (m_perrinfo != NULL) {
        m_perrinfo->GetGUID(&guid);
    }
    return guid;
}
inline const TCHAR * _com_error::ErrorMessage() const throw()
{
    if (m_pszMsg == NULL) {
        FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
                      NULL,
                      m_hresult,
                      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                      (LPTSTR)&m_pszMsg,
                      0,
                      NULL );
        if (m_pszMsg != NULL) {
            int nLen = lstrlen(m_pszMsg);
            if (nLen > 1 && m_pszMsg[nLen - 1] == '/n') {
                m_pszMsg[nLen - 1] = 0;
                if (m_pszMsg[nLen - 2] == '/r') {
                        m_pszMsg[nLen - 2] = 0;
                }
            }
        } else {
            m_pszMsg = (LPTSTR)LocalAlloc(0, 32 * sizeof(TCHAR));
            if (m_pszMsg != NULL) {
                WORD wCode = WCode();
                if (wCode != 0) {
                    wsprintf(m_pszMsg, TEXT("IDispatch error #%d"), wCode);
                } else {
                    wsprintf(m_pszMsg, TEXT("Unknown error 0x%0lX"), m_hresult);
                }
            }
        }
    }
    return m_pszMsg;
}
inline HRESULT _com_error::WCodeToHRESULT(WORD wCode) throw()
{
    return wCode >= 0xFE00 ? WCODE_HRESULT_LAST : WCODE_HRESULT_FIRST + wCode;
}
inline WORD _com_error::HRESULTToWCode(HRESULT hr) throw()
{
    return (hr >= WCODE_HRESULT_FIRST && hr <= WCODE_HRESULT_LAST)
        ? WORD(hr - WCODE_HRESULT_FIRST)
        : 0;
}

 

比如:

 //使用#import使用类型库
#import   "../b/debug/a.tlb"
Method1()
{
        try{
        v   =   IAPtr-> Value;
        ....      
        }catch(_com_error) {..}
}

抱歉!评论已关闭.