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