• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // MyCom.h
2 
3 #ifndef __MY_COM_H
4 #define __MY_COM_H
5 
6 #include "MyWindows.h"
7 #include "NewHandler.h"
8 
9 #ifndef RINOK
10 #define RINOK(x) { HRESULT __result_ = (x); if (__result_ != S_OK) return __result_; }
11 #endif
12 
13 template <class T>
14 class CMyComPtr
15 {
16   T* _p;
17 public:
CMyComPtr()18   CMyComPtr(): _p(NULL) {}
throw()19   CMyComPtr(T* p) throw() { if ((_p = p) != NULL) p->AddRef(); }
throw()20   CMyComPtr(const CMyComPtr<T>& lp) throw() { if ((_p = lp._p) != NULL) _p->AddRef(); }
~CMyComPtr()21   ~CMyComPtr() { if (_p) _p->Release(); }
Release()22   void Release() { if (_p) { _p->Release(); _p = NULL; } }
23   operator T*() const {  return (T*)_p;  }
24   // T& operator*() const {  return *_p; }
25   T** operator&() { return &_p; }
26   T* operator->() const { return _p; }
27   T* operator=(T* p)
28   {
29     if (p)
30       p->AddRef();
31     if (_p)
32       _p->Release();
33     _p = p;
34     return p;
35   }
36   T* operator=(const CMyComPtr<T>& lp) { return (*this = lp._p); }
37   bool operator!() const { return (_p == NULL); }
38   // bool operator==(T* pT) const {  return _p == pT; }
Attach(T * p2)39   void Attach(T* p2)
40   {
41     Release();
42     _p = p2;
43   }
Detach()44   T* Detach()
45   {
46     T* pt = _p;
47     _p = NULL;
48     return pt;
49   }
50   #ifdef _WIN32
51   HRESULT CoCreateInstance(REFCLSID rclsid, REFIID iid, LPUNKNOWN pUnkOuter = NULL, DWORD dwClsContext = CLSCTX_ALL)
52   {
53     return ::CoCreateInstance(rclsid, pUnkOuter, dwClsContext, iid, (void**)&_p);
54   }
55   #endif
56   /*
57   HRESULT CoCreateInstance(LPCOLESTR szProgID, LPUNKNOWN pUnkOuter = NULL, DWORD dwClsContext = CLSCTX_ALL)
58   {
59     CLSID clsid;
60     HRESULT hr = CLSIDFromProgID(szProgID, &clsid);
61     ATLASSERT(_p == NULL);
62     if (SUCCEEDED(hr))
63       hr = ::CoCreateInstance(clsid, pUnkOuter, dwClsContext, __uuidof(T), (void**)&_p);
64     return hr;
65   }
66   */
67   template <class Q>
QueryInterface(REFGUID iid,Q ** pp)68   HRESULT QueryInterface(REFGUID iid, Q** pp) const throw()
69   {
70     return _p->QueryInterface(iid, (void**)pp);
71   }
72 };
73 
74 //////////////////////////////////////////////////////////
75 
StringToBstr(LPCOLESTR src,BSTR * bstr)76 inline HRESULT StringToBstr(LPCOLESTR src, BSTR *bstr)
77 {
78   *bstr = ::SysAllocString(src);
79   return (*bstr) ? S_OK : E_OUTOFMEMORY;
80 }
81 
82 class CMyComBSTR
83 {
84   BSTR m_str;
85 
86 public:
CMyComBSTR()87   CMyComBSTR(): m_str(NULL) {}
~CMyComBSTR()88   ~CMyComBSTR() { ::SysFreeString(m_str); }
89   BSTR* operator&() { return &m_str; }
LPCOLESTR()90   operator LPCOLESTR() const { return m_str; }
91   // operator bool() const { return m_str != NULL; }
92   // bool operator!() const { return m_str == NULL; }
93 private:
94   // operator BSTR() const { return m_str; }
95 
CMyComBSTR(LPCOLESTR src)96   CMyComBSTR(LPCOLESTR src) { m_str = ::SysAllocString(src); }
97   // CMyComBSTR(int nSize) { m_str = ::SysAllocStringLen(NULL, nSize); }
98   // CMyComBSTR(int nSize, LPCOLESTR sz) { m_str = ::SysAllocStringLen(sz, nSize);  }
CMyComBSTR(const CMyComBSTR & src)99   CMyComBSTR(const CMyComBSTR& src) { m_str = src.MyCopy(); }
100 
101   /*
102   CMyComBSTR(REFGUID src)
103   {
104     LPOLESTR szGuid;
105     StringFromCLSID(src, &szGuid);
106     m_str = ::SysAllocString(szGuid);
107     CoTaskMemFree(szGuid);
108   }
109   */
110 
111   CMyComBSTR& operator=(const CMyComBSTR& src)
112   {
113     if (m_str != src.m_str)
114     {
115       if (m_str)
116         ::SysFreeString(m_str);
117       m_str = src.MyCopy();
118     }
119     return *this;
120   }
121 
122   CMyComBSTR& operator=(LPCOLESTR src)
123   {
124     ::SysFreeString(m_str);
125     m_str = ::SysAllocString(src);
126     return *this;
127   }
128 
Len()129   unsigned Len() const { return ::SysStringLen(m_str); }
130 
MyCopy()131   BSTR MyCopy() const
132   {
133     // We don't support Byte BSTRs here
134     return ::SysAllocStringLen(m_str, ::SysStringLen(m_str));
135     /*
136     UINT byteLen = ::SysStringByteLen(m_str);
137     BSTR res = ::SysAllocStringByteLen(NULL, byteLen);
138     if (res && byteLen != 0 && m_str)
139       memcpy(res, m_str, byteLen);
140     return res;
141     */
142   }
143 
144   /*
145   void Attach(BSTR src) { m_str = src; }
146   BSTR Detach()
147   {
148     BSTR s = m_str;
149     m_str = NULL;
150     return s;
151   }
152   */
153 
Empty()154   void Empty()
155   {
156     ::SysFreeString(m_str);
157     m_str = NULL;
158   }
159 };
160 
161 //////////////////////////////////////////////////////////
162 
163 class CMyUnknownImp
164 {
165 public:
166   ULONG __m_RefCount;
CMyUnknownImp()167   CMyUnknownImp(): __m_RefCount(0) {}
168 
169   // virtual ~CMyUnknownImp() {};
170 };
171 
172 #define MY_QUERYINTERFACE_BEGIN STDMETHOD(QueryInterface) \
173 (REFGUID iid, void **outObject) throw() { *outObject = NULL;
174 
175 #define MY_QUERYINTERFACE_ENTRY(i) else if (iid == IID_ ## i) \
176     { *outObject = (void *)(i *)this; }
177 
178 #define MY_QUERYINTERFACE_ENTRY_UNKNOWN(i) if (iid == IID_IUnknown) \
179     { *outObject = (void *)(IUnknown *)(i *)this; }
180 
181 #define MY_QUERYINTERFACE_BEGIN2(i) MY_QUERYINTERFACE_BEGIN \
182     MY_QUERYINTERFACE_ENTRY_UNKNOWN(i) \
183     MY_QUERYINTERFACE_ENTRY(i)
184 
185 #define MY_QUERYINTERFACE_END else return E_NOINTERFACE; ++__m_RefCount; /* AddRef(); */ return S_OK; }
186 
187 #define MY_ADDREF_RELEASE \
188 STDMETHOD_(ULONG, AddRef)() throw() { return ++__m_RefCount; } \
189 STDMETHOD_(ULONG, Release)() { if (--__m_RefCount != 0)  \
190   return __m_RefCount; delete this; return 0; }
191 
192 #define MY_UNKNOWN_IMP_SPEC(i) \
193   MY_QUERYINTERFACE_BEGIN \
194   i \
195   MY_QUERYINTERFACE_END \
196   MY_ADDREF_RELEASE
197 
198 
199 #define MY_UNKNOWN_IMP MY_QUERYINTERFACE_BEGIN \
200   MY_QUERYINTERFACE_ENTRY_UNKNOWN(IUnknown) \
201   MY_QUERYINTERFACE_END \
202   MY_ADDREF_RELEASE
203 
204 #define MY_UNKNOWN_IMP1(i) MY_UNKNOWN_IMP_SPEC( \
205   MY_QUERYINTERFACE_ENTRY_UNKNOWN(i) \
206   MY_QUERYINTERFACE_ENTRY(i) \
207   )
208 
209 #define MY_UNKNOWN_IMP2(i1, i2) MY_UNKNOWN_IMP_SPEC( \
210   MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \
211   MY_QUERYINTERFACE_ENTRY(i1) \
212   MY_QUERYINTERFACE_ENTRY(i2) \
213   )
214 
215 #define MY_UNKNOWN_IMP3(i1, i2, i3) MY_UNKNOWN_IMP_SPEC( \
216   MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \
217   MY_QUERYINTERFACE_ENTRY(i1) \
218   MY_QUERYINTERFACE_ENTRY(i2) \
219   MY_QUERYINTERFACE_ENTRY(i3) \
220   )
221 
222 #define MY_UNKNOWN_IMP4(i1, i2, i3, i4) MY_UNKNOWN_IMP_SPEC( \
223   MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \
224   MY_QUERYINTERFACE_ENTRY(i1) \
225   MY_QUERYINTERFACE_ENTRY(i2) \
226   MY_QUERYINTERFACE_ENTRY(i3) \
227   MY_QUERYINTERFACE_ENTRY(i4) \
228   )
229 
230 #define MY_UNKNOWN_IMP5(i1, i2, i3, i4, i5) MY_UNKNOWN_IMP_SPEC( \
231   MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \
232   MY_QUERYINTERFACE_ENTRY(i1) \
233   MY_QUERYINTERFACE_ENTRY(i2) \
234   MY_QUERYINTERFACE_ENTRY(i3) \
235   MY_QUERYINTERFACE_ENTRY(i4) \
236   MY_QUERYINTERFACE_ENTRY(i5) \
237   )
238 
239 #define MY_UNKNOWN_IMP6(i1, i2, i3, i4, i5, i6) MY_UNKNOWN_IMP_SPEC( \
240   MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \
241   MY_QUERYINTERFACE_ENTRY(i1) \
242   MY_QUERYINTERFACE_ENTRY(i2) \
243   MY_QUERYINTERFACE_ENTRY(i3) \
244   MY_QUERYINTERFACE_ENTRY(i4) \
245   MY_QUERYINTERFACE_ENTRY(i5) \
246   MY_QUERYINTERFACE_ENTRY(i6) \
247   )
248 
249 #define MY_UNKNOWN_IMP7(i1, i2, i3, i4, i5, i6, i7) MY_UNKNOWN_IMP_SPEC( \
250   MY_QUERYINTERFACE_ENTRY_UNKNOWN(i1) \
251   MY_QUERYINTERFACE_ENTRY(i1) \
252   MY_QUERYINTERFACE_ENTRY(i2) \
253   MY_QUERYINTERFACE_ENTRY(i3) \
254   MY_QUERYINTERFACE_ENTRY(i4) \
255   MY_QUERYINTERFACE_ENTRY(i5) \
256   MY_QUERYINTERFACE_ENTRY(i6) \
257   MY_QUERYINTERFACE_ENTRY(i7) \
258   )
259 
260 const HRESULT k_My_HRESULT_WritingWasCut = 0x20000010;
261 
262 #endif
263