forked from salsita/passthruapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProtocolCF.inl
241 lines (209 loc) · 5.38 KB
/
ProtocolCF.inl
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#ifndef PASSTHROUGHAPP_PROTOCOLCF_INL
#define PASSTHROUGHAPP_PROTOCOLCF_INL
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#ifndef PASSTHROUGHAPP_PROTOCOLCF_H
#error ProtocolCF.inl requires ProtocolCF.h to be included first
#endif
#include "PassthroughObject.h"
namespace PassthroughAPP
{
// ===== CComClassFactoryProtocol =====
inline STDMETHODIMP CComClassFactoryProtocol::CreateInstance(
IUnknown* punkOuter, REFIID riid, void** ppvObj)
{
ATLASSERT(ppvObj != 0);
if (!ppvObj)
{
return E_POINTER;
}
*ppvObj = 0;
CComPtr<IUnknown> spUnkTarget;
HRESULT hr = CreateInstanceTarget(&spUnkTarget);
ATLASSERT(SUCCEEDED(hr) && spUnkTarget != 0);
CComPtr<IUnknown> spUnkObject;
if (SUCCEEDED(hr))
{
hr = BaseClass::CreateInstance(punkOuter, riid,
reinterpret_cast<void**>(&spUnkObject));
ATLASSERT(SUCCEEDED(hr) && spUnkObject != 0);
}
if (SUCCEEDED(hr))
{
CComPtr<IPassthroughObject> spPassthroughObj;
hr = spUnkObject->QueryInterface(&spPassthroughObj);
ATLASSERT(SUCCEEDED(hr) && spPassthroughObj != 0);
if (SUCCEEDED(hr))
{
hr = spPassthroughObj->SetTargetUnknown(spUnkTarget);
ATLASSERT(SUCCEEDED(hr));
}
}
if (SUCCEEDED(hr))
{
*ppvObj = spUnkObject.Detach();
}
return hr;
}
inline HRESULT CComClassFactoryProtocol::CreateInstanceTarget(
IUnknown** ppTargetProtocol)
{
ATLASSERT(ppTargetProtocol != 0);
if (!ppTargetProtocol)
{
return E_POINTER;
}
*ppTargetProtocol = 0;
CComPtr<IClassFactory> spTargetCF;
HRESULT hr = GetTargetClassFactory(&spTargetCF);
ATLASSERT(SUCCEEDED(hr) && spTargetCF != 0);
if (SUCCEEDED(hr))
{
hr = spTargetCF->CreateInstance(0, IID_IInternetProtocolRoot,
reinterpret_cast<void**>(ppTargetProtocol));
ATLASSERT(SUCCEEDED(hr) && *ppTargetProtocol != 0);
}
return hr;
}
inline HRESULT CComClassFactoryProtocol::GetTargetClassFactory(
IClassFactory** ppCF)
{
ObjectLock lock(this);
return m_spTargetCF.CopyTo(ppCF);
}
inline HRESULT CComClassFactoryProtocol::SetTargetClassFactory(
IClassFactory* pCF)
{
HRESULT hr = (pCF ? pCF->LockServer(TRUE) : S_OK);
if (SUCCEEDED(hr))
{
ObjectLock lock(this);
if (m_spTargetCF)
{
// LockServer(FALSE) is assumed to always succeed. Otherwise,
// it is impossible to implement correct semantics
HRESULT hr1 = m_spTargetCF->LockServer(FALSE);
hr1;
ATLASSERT(SUCCEEDED(hr1));
}
m_spTargetCF = pCF;
}
return hr;
}
inline HRESULT CComClassFactoryProtocol::SetTargetCLSID(REFCLSID clsid,
DWORD clsContext)
{
CComPtr<IClassFactory> spTargetCF;
HRESULT hr = CoGetClassObject(clsid, clsContext, 0, IID_IClassFactory,
reinterpret_cast<void**>(&spTargetCF));
ATLASSERT(SUCCEEDED(hr) && spTargetCF != 0);
if (SUCCEEDED(hr))
{
hr = SetTargetClassFactory(spTargetCF);
ATLASSERT(SUCCEEDED(hr));
}
return hr;
}
inline void CComClassFactoryProtocol::FinalRelease()
{
// No need to be thread safe here
if (m_spTargetCF)
{
// LockServer(FALSE) is assumed to always succeed.
HRESULT hr = m_spTargetCF->LockServer(FALSE);
hr;
ATLASSERT(SUCCEEDED(hr));
m_spTargetCF.Release();
}
}
// ===== CMetaFactory =====
template <class Factory, class Protocol, class FactoryComObject>
inline HRESULT CMetaFactory<Factory, Protocol, FactoryComObject>::
CreateInstance(Factory** ppObj)
{
ATLASSERT(ppObj != 0);
if (!ppObj)
{
return E_POINTER;
}
HRESULT hr = E_OUTOFMEMORY;
void* pv = static_cast<void*>(CreatorClass::CreateInstance);
FactoryComObject* p = 0;
ATLTRY(p = new FactoryComObject(pv))
if (p != NULL)
{
p->SetVoid(pv);
p->InternalFinalConstructAddRef();
// MG: Updated to conform to more recent ATL version.
hr = p->_AtlInitialConstruct();
if (SUCCEEDED(hr))
hr = p->FinalConstruct();
if (SUCCEEDED(hr))
hr = p->_AtlFinalConstruct();
p->InternalFinalConstructRelease();
if (FAILED(hr))
{
delete p;
p = 0;
}
}
*ppObj = p;
return hr;
}
template <class Factory, class Protocol, class FactoryComObject>
inline HRESULT CMetaFactory<Factory, Protocol, FactoryComObject>::
CreateInstance(IClassFactory* pTargetCF, IClassFactory** ppCF)
{
if (!ppCF)
{
return E_POINTER;
}
*ppCF = 0;
Factory* pObj = 0;
HRESULT hr = CreateInstance(&pObj);
ATLASSERT(SUCCEEDED(hr) && pObj != 0);
if (SUCCEEDED(hr))
{
pObj->AddRef();
hr = pObj->SetTargetClassFactory(pTargetCF);
ATLASSERT(SUCCEEDED(hr));
if (SUCCEEDED(hr))
{
hr = pObj->QueryInterface(IID_IClassFactory,
reinterpret_cast<void**>(ppCF));
ATLASSERT(SUCCEEDED(hr) && *ppCF != 0);
}
pObj->Release();
}
return hr;
}
template <class Factory, class Protocol, class FactoryComObject>
inline HRESULT CMetaFactory<Factory, Protocol, FactoryComObject>::
CreateInstance(REFCLSID clsidTarget, IClassFactory** ppCF)
{
if (!ppCF)
{
return E_POINTER;
}
*ppCF = 0;
Factory* pObj = 0;
HRESULT hr = CreateInstance(&pObj);
ATLASSERT(SUCCEEDED(hr) && pObj != 0);
if (SUCCEEDED(hr))
{
pObj->AddRef();
hr = pObj->SetTargetCLSID(clsidTarget);
ATLASSERT(SUCCEEDED(hr));
if (SUCCEEDED(hr))
{
hr = pObj->QueryInterface(IID_IClassFactory,
reinterpret_cast<void**>(ppCF));
ATLASSERT(SUCCEEDED(hr) && *ppCF != 0);
}
pObj->Release();
}
return hr;
}
} // end namespace PassthroughAPP
#endif // PASSTHROUGHAPP_PROTOCOLCF_INL