rlm@1: // MyCom.h rlm@1: rlm@1: #ifndef __MYCOM_H rlm@1: #define __MYCOM_H rlm@1: rlm@1: #include "MyWindows.h" rlm@1: rlm@1: #ifndef RINOK rlm@1: #define RINOK(x) { HRESULT __result_ = (x); if (__result_ != S_OK) return __result_; } rlm@1: #endif rlm@1: rlm@1: template rlm@1: class CMyComPtr rlm@1: { rlm@1: T* _p; rlm@1: public: rlm@1: // typedef T _PtrClass; rlm@1: CMyComPtr() { _p = NULL;} rlm@1: CMyComPtr(T* p) {if ((_p = p) != NULL) p->AddRef(); } rlm@1: CMyComPtr(const CMyComPtr& lp) rlm@1: { rlm@1: if ((_p = lp._p) != NULL) rlm@1: _p->AddRef(); rlm@1: } rlm@1: ~CMyComPtr() { if (_p) _p->Release(); } rlm@1: void Release() { if (_p) { _p->Release(); _p = NULL; } } rlm@1: operator T*() const { return (T*)_p; } rlm@1: // T& operator*() const { return *_p; } rlm@1: T** operator&() { return &_p; } rlm@1: T* operator->() const { return _p; } rlm@1: T* operator=(T* p) rlm@1: { rlm@1: if (p != 0) rlm@1: p->AddRef(); rlm@1: if (_p) rlm@1: _p->Release(); rlm@1: _p = p; rlm@1: return p; rlm@1: } rlm@1: T* operator=(const CMyComPtr& lp) { return (*this = lp._p); } rlm@1: bool operator!() const { return (_p == NULL); } rlm@1: // bool operator==(T* pT) const { return _p == pT; } rlm@1: // Compare two objects for equivalence rlm@1: void Attach(T* p2) rlm@1: { rlm@1: Release(); rlm@1: _p = p2; rlm@1: } rlm@1: T* Detach() rlm@1: { rlm@1: T* pt = _p; rlm@1: _p = NULL; rlm@1: return pt; rlm@1: } rlm@1: #ifdef _WIN32 rlm@1: HRESULT CoCreateInstance(REFCLSID rclsid, REFIID iid, LPUNKNOWN pUnkOuter = NULL, DWORD dwClsContext = CLSCTX_ALL) rlm@1: { rlm@1: return ::CoCreateInstance(rclsid, pUnkOuter, dwClsContext, iid, (void**)&_p); rlm@1: } rlm@1: #endif rlm@1: /* rlm@1: HRESULT CoCreateInstance(LPCOLESTR szProgID, LPUNKNOWN pUnkOuter = NULL, DWORD dwClsContext = CLSCTX_ALL) rlm@1: { rlm@1: CLSID clsid; rlm@1: HRESULT hr = CLSIDFromProgID(szProgID, &clsid); rlm@1: ATLASSERT(_p == NULL); rlm@1: if (SUCCEEDED(hr)) rlm@1: hr = ::CoCreateInstance(clsid, pUnkOuter, dwClsContext, __uuidof(T), (void**)&_p); rlm@1: return hr; rlm@1: } rlm@1: */ rlm@1: template rlm@1: HRESULT QueryInterface(REFGUID iid, Q** pp) const rlm@1: { rlm@1: return _p->QueryInterface(iid, (void**)pp); rlm@1: } rlm@1: }; rlm@1: rlm@1: ////////////////////////////////////////////////////////// rlm@1: rlm@1: inline HRESULT StringToBstr(LPCOLESTR src, BSTR *bstr) rlm@1: { rlm@1: *bstr = ::SysAllocString(src); rlm@1: return (*bstr != 0) ? S_OK : E_OUTOFMEMORY; rlm@1: } rlm@1: rlm@1: class CMyComBSTR rlm@1: { rlm@1: public: rlm@1: BSTR m_str; rlm@1: CMyComBSTR(): m_str(NULL) {} rlm@1: CMyComBSTR(LPCOLESTR src) { m_str = ::SysAllocString(src); } rlm@1: // CMyComBSTR(int nSize) { m_str = ::SysAllocStringLen(NULL, nSize); } rlm@1: // CMyComBSTR(int nSize, LPCOLESTR sz) { m_str = ::SysAllocStringLen(sz, nSize); } rlm@1: CMyComBSTR(const CMyComBSTR& src) { m_str = src.MyCopy(); } rlm@1: /* rlm@1: CMyComBSTR(REFGUID src) rlm@1: { rlm@1: LPOLESTR szGuid; rlm@1: StringFromCLSID(src, &szGuid); rlm@1: m_str = ::SysAllocString(szGuid); rlm@1: CoTaskMemFree(szGuid); rlm@1: } rlm@1: */ rlm@1: ~CMyComBSTR() { ::SysFreeString(m_str); } rlm@1: CMyComBSTR& operator=(const CMyComBSTR& src) rlm@1: { rlm@1: if (m_str != src.m_str) rlm@1: { rlm@1: if (m_str) rlm@1: ::SysFreeString(m_str); rlm@1: m_str = src.MyCopy(); rlm@1: } rlm@1: return *this; rlm@1: } rlm@1: CMyComBSTR& operator=(LPCOLESTR src) rlm@1: { rlm@1: ::SysFreeString(m_str); rlm@1: m_str = ::SysAllocString(src); rlm@1: return *this; rlm@1: } rlm@1: unsigned int Length() const { return ::SysStringLen(m_str); } rlm@1: operator BSTR() const { return m_str; } rlm@1: BSTR* operator&() { return &m_str; } rlm@1: BSTR MyCopy() const rlm@1: { rlm@1: int byteLen = ::SysStringByteLen(m_str); rlm@1: BSTR res = ::SysAllocStringByteLen(NULL, byteLen); rlm@1: memcpy(res, m_str, byteLen); rlm@1: return res; rlm@1: } rlm@1: /* rlm@1: void Attach(BSTR src) { m_str = src; } rlm@1: BSTR Detach() rlm@1: { rlm@1: BSTR s = m_str; rlm@1: m_str = NULL; rlm@1: return s; rlm@1: } rlm@1: */ rlm@1: void Empty() rlm@1: { rlm@1: ::SysFreeString(m_str); rlm@1: m_str = NULL; rlm@1: } rlm@1: bool operator!() const { return (m_str == NULL); } rlm@1: }; rlm@1: rlm@1: ////////////////////////////////////////////////////////// rlm@1: rlm@1: class CMyUnknownImp rlm@1: { rlm@1: public: rlm@1: ULONG __m_RefCount; rlm@1: CMyUnknownImp(): __m_RefCount(0) {} rlm@1: }; rlm@1: rlm@1: #define MY_QUERYINTERFACE_BEGIN STDMETHOD(QueryInterface) \ rlm@1: (REFGUID iid, void **outObject) { rlm@1: rlm@1: #define MY_QUERYINTERFACE_ENTRY(i) if (iid == IID_ ## i) \ rlm@1: { *outObject = (void *)(i *)this; AddRef(); return S_OK; } rlm@1: rlm@1: #define MY_QUERYINTERFACE_ENTRY_UNKNOWN(i) if (iid == IID_IUnknown) \ rlm@1: { *outObject = (void *)(IUnknown *)(i *)this; AddRef(); return S_OK; } rlm@1: rlm@1: #define MY_QUERYINTERFACE_BEGIN2(i) MY_QUERYINTERFACE_BEGIN \ rlm@1: MY_QUERYINTERFACE_ENTRY_UNKNOWN(i) \ rlm@1: MY_QUERYINTERFACE_ENTRY(i) rlm@1: rlm@1: #define MY_QUERYINTERFACE_END return E_NOINTERFACE; } rlm@1: rlm@1: #define MY_ADDREF_RELEASE \ rlm@1: STDMETHOD_(ULONG, AddRef)() { return ++__m_RefCount; } \ rlm@1: STDMETHOD_(ULONG, Release)() { if (--__m_RefCount != 0) \ rlm@1: return __m_RefCount; delete this; return 0; } rlm@1: rlm@1: #define MY_UNKNOWN_IMP_SPEC(i) \ rlm@1: MY_QUERYINTERFACE_BEGIN \ rlm@1: i \ rlm@1: MY_QUERYINTERFACE_END \ rlm@1: MY_ADDREF_RELEASE rlm@1: rlm@1: rlm@1: #define MY_UNKNOWN_IMP MY_QUERYINTERFACE_BEGIN \ rlm@1: MY_QUERYINTERFACE_ENTRY_UNKNOWN(IUnknown) \ rlm@1: MY_QUERYINTERFACE_END \ rlm@1: MY_ADDREF_RELEASE rlm@1: rlm@1: #define MY_UNKNOWN_IMP1(i) MY_UNKNOWN_IMP_SPEC( \ rlm@1: MY_QUERYINTERFACE_ENTRY_UNKNOWN(i) \ rlm@1: MY_QUERYINTERFACE_ENTRY(i) \ rlm@1: ) rlm@1: rlm@1: #define MY_UNKNOWN_IMP2(i1, i2) MY_UNKNOWN_IMP_SPEC( \ rlm@1: MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \ rlm@1: MY_QUERYINTERFACE_ENTRY(i1) \ rlm@1: MY_QUERYINTERFACE_ENTRY(i2) \ rlm@1: ) rlm@1: rlm@1: #define MY_UNKNOWN_IMP3(i1, i2, i3) MY_UNKNOWN_IMP_SPEC( \ rlm@1: MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \ rlm@1: MY_QUERYINTERFACE_ENTRY(i1) \ rlm@1: MY_QUERYINTERFACE_ENTRY(i2) \ rlm@1: MY_QUERYINTERFACE_ENTRY(i3) \ rlm@1: ) rlm@1: rlm@1: #define MY_UNKNOWN_IMP4(i1, i2, i3, i4) MY_UNKNOWN_IMP_SPEC( \ rlm@1: MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \ rlm@1: MY_QUERYINTERFACE_ENTRY(i1) \ rlm@1: MY_QUERYINTERFACE_ENTRY(i2) \ rlm@1: MY_QUERYINTERFACE_ENTRY(i3) \ rlm@1: MY_QUERYINTERFACE_ENTRY(i4) \ rlm@1: ) rlm@1: rlm@1: #define MY_UNKNOWN_IMP5(i1, i2, i3, i4, i5) MY_UNKNOWN_IMP_SPEC( \ rlm@1: MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \ rlm@1: MY_QUERYINTERFACE_ENTRY(i1) \ rlm@1: MY_QUERYINTERFACE_ENTRY(i2) \ rlm@1: MY_QUERYINTERFACE_ENTRY(i3) \ rlm@1: MY_QUERYINTERFACE_ENTRY(i4) \ rlm@1: MY_QUERYINTERFACE_ENTRY(i5) \ rlm@1: ) rlm@1: rlm@1: #endif