1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
   |  
#pragma once
 
#include <comdef.h>
#include <comdefsp.h>
 
 
#define MCN_CONSTRUCT_COM_PTR_T(theClass, theBaseClass) \
	public: \
		theClass() \
		{ \
		} \
		explicit theClass( \
			const CLSID& clsid,	IUnknown* pOuter = NULL, DWORD dwClsContext = CLSCTX_ALL) \
			:CMcnComPtrT<theBaseClass>(clsid, pOuter, dwClsContext) \
		{ \
		} \
		explicit theClass(  \
			LPCWSTR str, IUnknown* pOuter = NULL, DWORD dwClsContext = CLSCTX_ALL) \
			:CMcnComPtrT<theBaseClass>(str, pOuter, dwClsContext) \
		{ \
		} \
		explicit theClass(  \
			LPCSTR str, IUnknown* pOuter = NULL, DWORD dwClsContext = CLSCTX_ALL) \
			:CMcnComPtrT<theBaseClass>(str, pOuter, dwClsContext) \
		{ \
		} \
		theClass(const theBaseClass& sp) throw() \
			:CMcnComPtrT<theBaseClass>(sp) \
		{ \
		} \
		theClass& operator=(theBaseClass p) \
		{ \
			HRESULT hr = _QueryInterface(p); \
			if (FAILED(hr) && (hr != E_NOINTERFACE)) { \
				_com_issue_error(hr); \
			} \
			return *this; \
		}
 
// T class must inherit directly of indirectly from _com_ptr_t
 
	template< class T >
	class CMcnComPtrT : public T
	{
	public:
		typedef T baseType;
		typedef CMcnComPtrT<T> thisType;
 
		CMcnComPtrT()
		{
		}
 
		explicit CMcnComPtrT(
			const CLSID& clsid,	IUnknown* pOuter = NULL, DWORD dwClsContext = CLSCTX_ALL)
			:T(clsid, pOuter, dwClsContext)
		{
		}
 
		explicit CMcnComPtrT(
			LPCWSTR str, IUnknown* pOuter = NULL, DWORD dwClsContext = CLSCTX_ALL)
			:T(str, pOuter, dwClsContext)
		{
		}
 
		explicit CMcnComPtrT( 
			LPCSTR str, IUnknown* pOuter = NULL, DWORD dwClsContext = CLSCTX_ALL)
			:T(str, pOuter, dwClsContext)
		{
		}
 
		CMcnComPtrT(const T& sp) throw()
			:T(sp)
		{
		}
 
	protected:
		template<typename _InterfacePtr>
		HRESULT _QueryInterface(_InterfacePtr p) throw()
		{
			HRESULT hr;
			if (p != NULL)
			{
				Interface* pInterface;
				hr = p->QueryInterface(GetIID(), reinterpret_cast<void**>(&pInterface));
				baseType::Attach(SUCCEEDED(hr)? pInterface: NULL);
			}
			else
			{
				baseType::operator=(static_cast<Interface*>(NULL));
				hr = E_NOINTERFACE;
			}
			return hr;
		}
 
	}; // class CMcnComPtrT | 
Partager