📄 comobjlib.h
字号:
template <class Itf> class CComPtr { Itf* m_ptr; public: CComPtr () : m_ptr (0) {} CComPtr (Itf* p) : m_ptr (p) { if (m_ptr) m_ptr->AddRef (); } CComPtr (const CComPtr<Itf>& sp) : m_ptr (sp.m_ptr) { if (m_ptr) m_ptr->AddRef (); } ~CComPtr () { if (m_ptr) m_ptr->Release (); m_ptr = 0; } void Release () { if (m_ptr) m_ptr->Release (); m_ptr = 0; } operator Itf* () const { return m_ptr; } Itf** operator& () { return &m_ptr; } Itf* operator-> () { return m_ptr; } const Itf* operator-> () const { return m_ptr; } Itf* operator= (Itf* p) { if (p) p->AddRef (); if (m_ptr) m_ptr->Release (); m_ptr = p; return m_ptr; } Itf* operator= (const CComPtr<Itf>& sp) { if (sp.m_ptr) sp.m_ptr->AddRef (); if (m_ptr) m_ptr->Release (); m_ptr = sp.m_ptr; return m_ptr; } bool operator! () const { return (m_ptr == 0); } void Attach (Itf *p2) { if (0 != m_ptr) { m_ptr->Release (); } m_ptr = p2; } Itf *Detach() { Itf *pt = m_ptr; m_ptr = 0; return pt; } HRESULT CopyTo (Itf **ppT) { if (0 == ppT) { return E_POINTER; } *ppT = m_ptr; if (0 != p) { p->AddRef (); } return S_OK; } };/////////////////////////////////////////////////////////////////////////// CComBSTR -- a class that wraps up the BSTR data type...//class CComBSTR { BSTR m_str; public: CComBSTR () : m_str (0) {} explicit CComBSTR (int nSize, LPCOLESTR sz = 0) { m_str = ::SysAllocStringLen (sz, nSize); } CComBSTR (const char * pstr) { OLECHAR * wsz = new OLECHAR [(strlen (pstr) + 1) * 2]; comAsciiToWide (wsz, pstr, strlen (pstr) + 1); m_str = ::SysAllocString (wsz); delete [] wsz; } explicit CComBSTR (LPCOLESTR psz) { m_str = ::SysAllocString (psz); } explicit CComBSTR (const CComBSTR& src) { m_str = src.Copy (); } CComBSTR& operator= (const CComBSTR& cbs); CComBSTR& operator= (LPCOLESTR pSrc); ~CComBSTR() { Empty (); } unsigned int Length () const { return ::SysStringLen (m_str); } operator BSTR () const { return m_str; } BSTR* operator& () { return &m_str; } BSTR Copy() const { return ::SysAllocStringLen (m_str, ::SysStringLen (m_str)); } void Attach (BSTR src) { m_str = src; } BSTR Detach () { BSTR s = m_str; m_str = 0; return s; } void Empty () { if (m_str) ::SysFreeString (m_str); m_str = 0; } bool operator! () const { return (m_str == NULL); } void Append (const CComBSTR& cbs) { Append (cbs.m_str, ::SysStringLen (cbs.m_str)); } void Append (LPCOLESTR lpsz) { Append (lpsz, ::comWideStrLen (lpsz)); } void AppendBSTR (BSTR bs) { Append (bs, ::SysStringLen (bs)); } void Append (LPCOLESTR lpsz, int nLen); CComBSTR& operator+= (const CComBSTR& cbs) { AppendBSTR (cbs.m_str); return *this; } };////////////////////////////////////////////////////////////////////////////// CComVariant support//class CComVariant : public tagVARIANT{// Constructorspublic: CComVariant() { ::VariantInit(this); } ~CComVariant() { Clear(); } CComVariant(const VARIANT& varSrc) { ::VariantInit(this); InternalCopy(&varSrc); } CComVariant(const CComVariant& varSrc) { ::VariantInit(this); InternalCopy(&varSrc); } CComVariant(BSTR bstrSrc) { ::VariantInit(this); *this = bstrSrc; } CComVariant(LPCOLESTR lpszSrc) { ::VariantInit(this); *this = lpszSrc; } CComVariant(bool bSrc) { ::VariantInit(this); vt = VT_BOOL; boolVal = bSrc ? VARIANT_TRUE : VARIANT_FALSE; } CComVariant(int nSrc) { ::VariantInit(this); vt = VT_I4; lVal = nSrc; } CComVariant(BYTE nSrc) { ::VariantInit(this); vt = VT_UI1; bVal = nSrc; } CComVariant(short nSrc) { ::VariantInit(this); vt = VT_I2; iVal = nSrc; } CComVariant(long nSrc, VARTYPE vtSrc = VT_I4) { ::VariantInit(this); vt = vtSrc; lVal = nSrc; } CComVariant(float fltSrc) { ::VariantInit(this); vt = VT_R4; fltVal = fltSrc; } CComVariant(double dblSrc) { ::VariantInit(this); vt = VT_R8; dblVal = dblSrc; } CComVariant(CY cySrc) { ::VariantInit(this); vt = VT_CY; cyVal = cySrc; } CComVariant(IUnknown* pSrc) { ::VariantInit(this); vt = VT_UNKNOWN; punkVal = pSrc; if (punkVal != NULL) { punkVal->AddRef(); } } CComVariant& operator=(const CComVariant& varSrc) { InternalCopy(&varSrc); return *this; } CComVariant& operator=(const VARIANT& varSrc) { InternalCopy(&varSrc); return *this; } CComVariant& operator=(BSTR bstrSrc); CComVariant& operator=(LPCOLESTR lpszSrc); CComVariant& operator=(bool bSrc); CComVariant& operator=(int nSrc); CComVariant& operator=(BYTE nSrc); CComVariant& operator=(short nSrc); CComVariant& operator=(long nSrc); CComVariant& operator=(float fltSrc); CComVariant& operator=(double dblSrc); CComVariant& operator=(CY cySrc); CComVariant& operator=(IUnknown* pSrc); bool operator==(const VARIANT& varSrc); bool operator!=(const VARIANT& varSrc) {return !operator==(varSrc);} HRESULT Clear() { return ::VariantClear(this); } HRESULT Copy(const VARIANT* pSrc) { return ::VariantCopy(this, const_cast<VARIANT*>(pSrc)); } HRESULT Attach(VARIANT* pSrc); HRESULT Detach(VARIANT* pDest); HRESULT ChangeType(VARTYPE vtNew, const VARIANT* pSrc = NULL); HRESULT InternalClear(); void InternalCopy(const VARIANT* pSrc);};////////////////////////////////////////////////////////////////////////////// Macros for making QueryInterface work.//// The COM_MAP macros define a function called _qi_impl() which does// runtime casts to obtain the requested interface pointer.//// Other than that, the layout of the COM_MAP in the user's class is// identical to an ATL map, but only allows for COM_INTERFACE_ENTRY// and COM_INTERFACE_ENTRY_IID entries, not any of the other more// fanciful types.//// The default IUnknown is always the first COM_INTERFACE_ENTRY in the// table, so (by definition) this must be derived from IUnknown or the// static_cast() will fail...//#define BEGIN_COM_MAP(cls) HRESULT _qi_impl (REFIID riid, void** ppv) { \ COMTRACK_ADD_CLS(cls);#define COM_INTERFACE_ENTRY(itf) \ if (ppv == NULL) \ {COMTRACK_ADD_IID (itf);} else { \ if ((IID_##itf == riid) || (IID_IUnknown == riid)) { \ *ppv = static_cast<itf*> (this); \ InternalAddRef (); \ COMTRACK_UPDATE (); \ return S_OK; } }#define COM_INTERFACE_ENTRY_IID(iid,itf) \ if (ppv == NULL) \ {COMTRACK_ADD_IID (itf);} else { \ if (riid == iid) { \ *ppv = static_cast<itf*> (this); \ InternalAddRef (); \ COMTRACK_UPDATE (); \ return S_OK; }} #define END_COM_MAP() \ if (ppv != 0) *ppv = 0; \ return E_NOINTERFACE; }////////////////////////////////////////////////////////////////////////////// AUTOREGISTER_COCLASS(cls,name)//// This macro must be included in the object-implementation C++ source// file after the object's class definition. It associates the C++// class 'cls' with the CLSID and registers the module// name in the VxRegistry, against the object's CLSID.////////////////////////////////////////////////////////////////////////////#define AUTOREGISTER_COCLASS(cls,priorityScheme, priority) \ struct cls ## _autoreg { \ cls ## _autoreg () { \ CComObject<cls>::classRegister (priorityScheme, \ priority); } }; \ static cls ## _autoreg __autoreg_ ## cls;////////////////////////////////////////////////////////////////////////////// BEGIN_COCLASS_TABLE, EXPORT_COCLASS, END_COCLASS_TABLE//// Macros for exporting coclasses from shared-libraries. They should// be used like so:-//// BEGIN_COCLASS_TABLE// EXPORT_COCLASS(CoMyClass)// END_COCLASS_TABLE//// where CoMyClass is a coclass (i.e. inherits CComCoClass) and thus// has a static method 'classObjectGet' conforming to the typedef// PFN_GETCLASSOBJECT as defined in comCoreLib.h////////////////////////////////////////////////////////////////////////////#define BEGIN_COCLASS_TABLE \extern "C" HRESULT DllGetClassObject \ (REFCLSID clsid, REFIID iid, void** ppv) {#define EXPORT_COCLASS(coclass) \ if (clsid == coclass::GetObjectCLSID ()) \ { return coclass::classObjectGet (clsid, iid, ppv); }#define END_COCLASS_TABLE \ return CLASS_E_CLASSNOTAVAILABLE; }#endif /* __INCcomObjLib_h__ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -