703
社区成员
发帖
与我相关
我的任务
分享
/////////////////////////////////////////////////////////////////////////////
// IDispatchImpl
template <class T, const IID* piid, const GUID* plibid = &CComModule::m_libid, WORD wMajor = 1,
WORD wMinor = 0, class tihclass = CComTypeInfoHolder>
class ATL_NO_VTABLE IDispatchImpl : public T
{
public:
typedef tihclass _tihclass;
// IDispatch
STDMETHOD(GetTypeInfoCount)(UINT* pctinfo)
{
*pctinfo = 1;
return S_OK;
}
STDMETHOD(GetTypeInfo)(UINT itinfo, LCID lcid, ITypeInfo** pptinfo)
{
return _tih.GetTypeInfo(itinfo, lcid, pptinfo);
}
STDMETHOD(GetIDsOfNames)(REFIID riid, LPOLESTR* rgszNames, UINT cNames,
LCID lcid, DISPID* rgdispid)
{
return _tih.GetIDsOfNames(riid, rgszNames, cNames, lcid, rgdispid);
}
STDMETHOD(Invoke)(DISPID dispidMember, REFIID riid,
LCID lcid, WORD wFlags, DISPPARAMS* pdispparams, VARIANT* pvarResult,
EXCEPINFO* pexcepinfo, UINT* puArgErr)
{
return _tih.Invoke((IDispatch*)this, dispidMember, riid, lcid,
wFlags, pdispparams, pvarResult, pexcepinfo, puArgErr);
}
protected:
static _tihclass _tih;
static HRESULT GetTI(LCID lcid, ITypeInfo** ppInfo)
{
return _tih.GetTI(lcid, ppInfo);
}
};
template <class T, const IID* piid, const GUID* plibid, WORD wMajor, WORD wMinor, class tihclass>
IDispatchImpl<T, piid, plibid, wMajor, wMinor, tihclass>::_tihclass
IDispatchImpl<T, piid, plibid, wMajor, wMinor, tihclass>::_tih =
{piid, plibid, wMajor, wMinor, NULL, 0, NULL, 0};
#ifndef ERRORINFOIMPLH
#define ERRORINFOIMPLH
#pragma once
//#include "stdafx.h"
#include <atlbase.h>
extern CComModule _Module;
#include <atlcom.h>
template <class T,const CLSID *pclsid = &CLSID_NULL>
class AG_IErrorInfoImpl : public IDispatchImpl<IErrorInfo, &IID_IErrorInfo>
{
protected:
DWORD m_ErrorDescriptionID;
string m_ErrMsg;
public:
AG_IErrorInfoImpl()
{
m_ErrorDescriptionID = 0;
}
void setErrorString(DWORD id, ...)
{
/*
va_list marker;
va_start(marker, id); //Initialize variable arguments.
string format;
format.LoadString(id);
m_ErrMsg.FormatV(format , marker);
va_end(marker); */
m_ErrorDescriptionID = id;
}
void setErrorString(LPCTSTR str)
{
m_ErrMsg = str;
m_ErrorDescriptionID = 1;
}
public:
STDMETHOD(GetGUID)(GUID *pGUID)
{
if(pGUID == NULL) return E_INVALIDARG;
*pGUID = *pclsid;
return S_OK;
}
STDMETHOD(GetSource)(BSTR *pBstrSource)
{
LPOLESTR ptr = NULL;
if(!FAILED(ProgIDFromCLSID(*pclsid, &ptr)))
{
CComBSTR str = ptr;
CoTaskMemFree(ptr);
*pBstrSource = str.Detach();
}
return S_OK;
}
STDMETHOD(GetDescription)(BSTR *pBstrDescription)
{
if(m_ErrorDescriptionID)
{
// CComBSTR str;
// str.LoadString(m_ErrorDescriptionID);
// *pBstrDescription = str.Detach();
// return S_OK;
//}
CComBSTR str = m_ErrMsg.c_str();
*pBstrDescription = str.Detach();
}
return S_OK;
}
STDMETHOD(GetHelpFile)(BSTR *pBstrHelpFile)
{
return E_NOTIMPL;
}
STDMETHOD(GetHelpContext)(DWORD *pdwHelpContext)
{
return E_NOTIMPL;
}
DWORD showMessageBoxByErrorInfo()
{
return ::MessageBox(NULL, m_ErrMsg, _T("COM error!"), MB_OK | MB_ICONERROR);
}
};
class errorDescription
{
string str;
public:
errorDescription(IUnknown *obj)
{
CComQIPtr<IErrorInfo> inf = obj;
if(inf == NULL) return;
CComBSTR msg;
inf->GetDescription(& msg);
str = W2A(msg);
}
operator LPCTSTR()
{
return str.c_str();
}
};
inline DWORD showMessageBoxByErrorInfo(IUnknown *obj)
{
CComQIPtr<IErrorInfo> inf = obj;
if(inf == NULL) return 0;
CComBSTR msg;
inf->GetDescription(& msg);
return ::MessageBox(NULL, W2A(msg), _T("COM error!"), MB_OK | MB_ICONERROR);
}
#endif