forked from microsoft/Windows-driver-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomsup.cpp
More file actions
344 lines (242 loc) · 6.14 KB
/
comsup.cpp
File metadata and controls
344 lines (242 loc) · 6.14 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*++
Copyright (C) Microsoft Corporation, All Rights Reserved
Module Name:
ComSup.cpp
Abstract:
This module contains implementations for the functions and methods
used for providing COM support.
Environment:
Windows User-Mode Driver Framework (WUDF)
--*/
#include "internal.h"
#include "comsup.tmh"
//
// Implementation of CUnknown methods.
//
CUnknown::CUnknown(
VOID
) : m_ReferenceCount(1)
/*++
Routine Description:
Constructor for an instance of the CUnknown class. This simply initializes
the reference count of the object to 1. The caller is expected to
call Release() if it wants to delete the object once it has been allocated.
Arguments:
None
Return Value:
None
--*/
{
// do nothing.
}
HRESULT
STDMETHODCALLTYPE
CUnknown::QueryInterface(
_In_ REFIID InterfaceId,
_Out_ PVOID *Object
)
/*++
Routine Description:
This method provides the basic support for query interface on CUnknown.
If the interface requested is IUnknown it references the object and
returns an interface pointer. Otherwise it returns an error.
Arguments:
InterfaceId - the IID being requested
Object - a location to store the interface pointer to return.
Return Value:
S_OK or E_NOINTERFACE
--*/
{
if (IsEqualIID(InterfaceId, __uuidof(IUnknown)))
{
*Object = QueryIUnknown();
return S_OK;
}
else
{
*Object = NULL;
return E_NOINTERFACE;
}
}
IUnknown *
CUnknown::QueryIUnknown(
VOID
)
/*++
Routine Description:
This helper method references the object and returns a pointer to the
object's IUnknown interface.
This allows other methods to convert a CUnknown pointer into an IUnknown
pointer without a typecast and without calling QueryInterface and dealing
with the return value.
Arguments:
None
Return Value:
A pointer to the object's IUnknown interface.
--*/
{
AddRef();
return static_cast<IUnknown *>(this);
}
ULONG
STDMETHODCALLTYPE
CUnknown::AddRef(
VOID
)
/*++
Routine Description:
This method adds one to the object's reference count.
Arguments:
None
Return Value:
The new reference count. The caller should only use this for debugging
as the object's actual reference count can change while the caller
examines the return value.
--*/
{
return InterlockedIncrement(&m_ReferenceCount);
}
ULONG
STDMETHODCALLTYPE
CUnknown::Release(
VOID
)
/*++
Routine Description:
This method subtracts one to the object's reference count. If the count
goes to zero, this method deletes the object.
Arguments:
None
Return Value:
The new reference count. If the caller uses this value it should only be
to check for zero (i.e. this call caused or will cause deletion) or
non-zero (i.e. some other call may have caused deletion, but this one
didn't).
--*/
{
ULONG count = InterlockedDecrement(&m_ReferenceCount);
if (count == 0)
{
delete this;
}
return count;
}
//
// Implementation of CClassFactory methods.
//
//
// Define storage for the factory's static lock count variable.
//
LONG CClassFactory::s_LockCount = 0;
IClassFactory *
CClassFactory::QueryIClassFactory(
VOID
)
/*++
Routine Description:
This helper method references the object and returns a pointer to the
object's IClassFactory interface.
This allows other methods to convert a CClassFactory pointer into an
IClassFactory pointer without a typecast and without dealing with the
return value QueryInterface.
Arguments:
None
Return Value:
A referenced pointer to the object's IClassFactory interface.
--*/
{
AddRef();
return static_cast<IClassFactory *>(this);
}
HRESULT
CClassFactory::QueryInterface(
_In_ REFIID InterfaceId,
_Out_ PVOID *Object
)
/*++
Routine Description:
This method attempts to retrieve the requested interface from the object.
If the interface is found then the reference count on that interface (and
thus the object itself) is incremented.
Arguments:
InterfaceId - the interface the caller is requesting.
Object - a location to store the interface pointer.
Return Value:
S_OK or E_NOINTERFACE
--*/
{
//
// This class only supports IClassFactory so check for that.
//
if (IsEqualIID(InterfaceId, __uuidof(IClassFactory)))
{
*Object = QueryIClassFactory();
return S_OK;
}
else
{
//
// See if the base class supports the interface.
//
return CUnknown::QueryInterface(InterfaceId, Object);
}
}
HRESULT
STDMETHODCALLTYPE
CClassFactory::CreateInstance(
_In_opt_ IUnknown * /* OuterObject */,
_In_ REFIID InterfaceId,
_Out_ PVOID *Object
)
/*++
Routine Description:
This COM method is the factory routine - it creates instances of the driver
callback class and returns the specified interface on them.
Arguments:
OuterObject - only used for aggregation, which our driver callback class
does not support.
InterfaceId - the interface ID the caller would like to get from our
new object.
Object - a location to store the referenced interface pointer to the new
object.
Return Value:
Status.
--*/
{
HRESULT hr;
PCMyDriver driver;
*Object = NULL;
hr = CMyDriver::CreateInstance(&driver);
if (SUCCEEDED(hr))
{
hr = driver->QueryInterface(InterfaceId, Object);
driver->Release();
}
return hr;
}
HRESULT
STDMETHODCALLTYPE
CClassFactory::LockServer(
_In_ BOOL Lock
)
/*++
Routine Description:
This COM method can be used to keep the DLL in memory. However since the
driver's DllCanUnloadNow function always returns false, this has little
effect. Still it tracks the number of lock and unlock operations.
Arguments:
Lock - Whether the caller wants to lock or unlock the "server"
Return Value:
S_OK
--*/
{
if (Lock)
{
InterlockedIncrement(&s_LockCount);
}
else
{
InterlockedDecrement(&s_LockCount);
}
return S_OK;
}