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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
#pragma once
#ifndef SAFE_RELEASE
#define SAFE_RELEASE(p) if( NULL != p ) { ( p )->Release(); p = NULL; }
#endif
// {CDD18979-A7B0-4D5E-9EB2-0A826805CBBD}
DEFINE_PROPERTYKEY(PRIVATE_SAMPLE_DRIVER_WUDF_DEVICE_OBJECT, 0xCDD18979, 0xA7B0, 0x4D5E, 0x9E, 0xB2, 0x0A, 0x82, 0x68, 0x05, 0xCB, 0xBD, 2);
// {9BD949E5-59CF-41AE-90A9-BE1D044F578F}
DEFINE_PROPERTYKEY(PRIVATE_SAMPLE_DRIVER_WPD_SERIALIZER_OBJECT, 0x9BD949E5, 0x59CF, 0x41AE, 0x90, 0xA9, 0xBE, 0x1D, 0x04, 0x4F, 0x57, 0x8F, 2);
// {4DF6C8C7-2CE5-457C-9F53-EFCECAA95C04}
DEFINE_PROPERTYKEY(PRIVATE_SAMPLE_DRIVER_CLIENT_CONTEXT_MAP, 0x4DF6C8C7, 0x2CE5, 0x457C, 0x9F, 0x53, 0xEF, 0xCE, 0xCA, 0xA9, 0x5C, 0x04, 2);
// {67BA8D9E-1DC4-431C-B89C-9D03F7D8C223}
DEFINE_PROPERTYKEY(PRIVATE_SAMPLE_DRIVER_REQUEST_FILENAME, 0x67BA8D9E, 0x1DC4, 0x431C, 0xB8, 0x9C, 0x9D, 0x03, 0xF7, 0xD8, 0xC2, 0x23, 2);
// Service event and parameters
// {D93102D5-8FED-4A39-AF84-228FE15888D0}
DEFINE_GUID(MyCustomEvent, 0xD93102D5, 0x8FED, 0x4A39, 0xAF, 0x84, 0x22, 0x8F, 0xE1, 0x58, 0x88, 0xD0);
// {D93102D5-8FED-4A39-AF84-228FE15888D0}.2
DEFINE_PROPERTYKEY(MyCustomEventParam0, 0xD93102D5, 0x8FED, 0x4A39, 0xAF, 0x84, 0x22, 0x8F, 0xE1, 0x58, 0x88, 0xD0, 2);
// {D93102D5-8FED-4A39-AF84-228FE15888D0}.3
DEFINE_PROPERTYKEY(MyCustomEventParam1, 0xD93102D5, 0x8FED, 0x4A39, 0xAF, 0x84, 0x22, 0x8F, 0xE1, 0x58, 0x88, 0xD0, 3);
// Service method and parameters
// {ECFC865F-7B43-4D76-9C6B-D67309A3F6F7}
DEFINE_GUID(MyCustomMethod, 0xECFC865F, 0x7B43, 0x4D76, 0x9C, 0x6B, 0xD6, 0x73, 0x09, 0xA3, 0xF6, 0xF7);
// {ECFC865F-7B43-4D76-9C6B-D67309A3F6F7}.2
DEFINE_PROPERTYKEY(MyCustomMethodResult, 0xECFC865F, 0x7B43, 0x4D76, 0x9C, 0x6B, 0xD6, 0x73, 0x09, 0xA3, 0xF6, 0xF7, 2);
// {ECFC865F-7B43-4D76-9C6B-D67309A3F6F7}.3
DEFINE_PROPERTYKEY(MyCustomMethodParam, 0xECFC865F, 0x7B43, 0x4D76, 0x9C, 0x6B, 0xD6, 0x73, 0x09, 0xA3, 0xF6, 0xF7, 3);
// {ECFC865F-7B43-4D76-9C6B-D67309A3F6F7}.4
DEFINE_PROPERTYKEY(MyCustomMethodParamInOut, 0xECFC865F, 0x7B43, 0x4D76, 0x9C, 0x6B, 0xD6, 0x73, 0x09, 0xA3, 0xF6, 0xF7, 4);
// Contact versioning property
// {2B0D5AA4-7EB3-4674-BD36-23FE4C39A2C2}.2
DEFINE_PROPERTYKEY(MyContactVersionIdentifier, 0x2B0D5AA4, 0x7EB3, 0x4674, 0xBD, 0x36, 0x23, 0xFE, 0x4C, 0x39, 0xA2, 0xC2, 2);
// Full Enumeration Sync Replica ID
// {81176f1e-2c42-4b4e-8f79-bc1a7f3da046}
DEFINE_GUID(MyFullEnumSyncReplicaId, 0x81176f1e, 0x2c42, 0x4b4e, 0x8f, 0x79, 0xbc, 0x1a, 0x7f, 0x3d, 0xa0, 0x46);
// Access Scope is a bit mask, where each bit enables access to a particular scope
// for example, contacts service is bit 1.
// The next scope, if any, will be in bit 2
// Full device access is a combination of all, requires all bits to be set
typedef enum tagACCESS_SCOPE
{
CONTACTS_SERVICE_ACCESS = 1,
FULL_DEVICE_ACCESS = 0xFFFFFFFF
}ACCESS_SCOPE;
typedef enum tagFakeDevicePropertyAttributesType
{
UnspecifiedForm_CanRead_CanWrite_CannotDelete_Fast,
UnspecifiedForm_CanRead_CannotWrite_CannotDelete_Fast,
} FakeDevicePropertyAttributesType;
typedef struct tagPropertyAttributeInfo
{
const PROPERTYKEY* pKey;
VARTYPE Vartype;
FakeDevicePropertyAttributesType AttributesType;
PCWSTR wszName;
} PropertyAttributeInfo;
typedef struct tagMethodParameterAttributeInfo
{
const PROPERTYKEY* pKey;
VARTYPE Vartype;
WPD_PARAMETER_USAGE_TYPES UsageType;
WpdParameterAttributeForm Form;
DWORD Order;
PCWSTR wszName;
} MethodParameterAttributeInfo;
typedef struct tagEventParameterAttributeInfo
{
const GUID* pEventGuid;
const PROPERTYKEY* pParameter;
VARTYPE Vartype;
} EventParameterAttributeInfo;
typedef struct tagFormatAttributeInfo
{
const GUID* pFormatGuid;
PCWSTR wszName;
} FormatAttributeInfo;
class ContextMap : public IUnknown
{
public:
ContextMap() :
m_cRef(1)
{
}
~ContextMap()
{
CComCritSecLock<CComAutoCriticalSection> Lock(m_CriticalSection);
IUnknown* pUnk = NULL;
POSITION elementPosition = NULL;
elementPosition = m_Map.GetStartPosition();
while(elementPosition != NULL)
{
pUnk = m_Map.GetNextValue(elementPosition);
if(pUnk != NULL)
{
pUnk->Release();
}
}
}
public: // IUnknown
ULONG __stdcall AddRef()
{
InterlockedIncrement((long*) &m_cRef);
return m_cRef;
}
_At_(this, __drv_freesMem(Mem))
ULONG __stdcall Release()
{
ULONG ulRefCount = m_cRef - 1;
if (InterlockedDecrement((long*) &m_cRef) == 0)
{
delete this;
return 0;
}
return ulRefCount;
}
HRESULT __stdcall QueryInterface(
REFIID riid,
void** ppv)
{
HRESULT hr = S_OK;
if(riid == IID_IUnknown)
{
*ppv = static_cast<IUnknown*>(this);
AddRef();
}
else
{
*ppv = NULL;
hr = E_NOINTERFACE;
}
return hr;
}
public: // Context accessor methods
// If successful, this method AddRef's the context and returns
// a context key
HRESULT Add(
_In_ IUnknown* pContext,
_Out_ CAtlStringW& key)
{
CComCritSecLock<CComAutoCriticalSection> Lock(m_CriticalSection);
HRESULT hr = S_OK;
GUID guidContext = GUID_NULL;
CComBSTR bstrContext;
key = L"";
// Create a unique context key
hr = CoCreateGuid(&guidContext);
if (hr == S_OK)
{
bstrContext = guidContext;
if(bstrContext.Length() > 0)
{
key = bstrContext;
}
else
{
hr = E_OUTOFMEMORY;
}
}
if (hr == S_OK)
{
// Insert this into the map
POSITION elementPosition = m_Map.SetAt(key, pContext);
if(elementPosition != NULL)
{
// AddRef since we are holding onto it
pContext->AddRef();
}
else
{
hr = E_OUTOFMEMORY;
}
}
return hr;
}
void Remove(
const CAtlStringW& key)
{
CComCritSecLock<CComAutoCriticalSection> Lock(m_CriticalSection);
// Get the element
IUnknown* pContext = NULL;
if (m_Map.Lookup(key, pContext) == true)
{
// Remove the entry for it
m_Map.RemoveKey(key);
// Release it
pContext->Release();
}
}
// Returns the context pointer. If not found, return value is NULL.
// If non-NULL, caller is responsible for Releasing when it is done,
// since this method will AddRef the context.
IUnknown* GetContext(
const CAtlStringW& key)
{
CComCritSecLock<CComAutoCriticalSection> Lock(m_CriticalSection);
// Get the element
IUnknown* pContext = NULL;
if (m_Map.Lookup(key, pContext) == true)
{
// AddRef
pContext->AddRef();
}
return pContext;
}
private:
CComAutoCriticalSection m_CriticalSection;
CAtlMap<CAtlStringW, IUnknown*> m_Map;
DWORD m_cRef;
};
// This class is used to store the connected client information.
class ClientContext : public IUnknown
{
public:
ClientContext() :
MajorVersion(0),
MinorVersion(0),
Revision(0),
m_cRef(1)
{
}
~ClientContext()
{
}
public: // IUnknown
ULONG __stdcall AddRef()
{
InterlockedIncrement((long*) &m_cRef);
return m_cRef;
}
_At_(this, __drv_freesMem(Mem))
ULONG __stdcall Release()
{
ULONG ulRefCount = m_cRef - 1;
if (InterlockedDecrement((long*) &m_cRef) == 0)
{
delete this;
return 0;
}
return ulRefCount;
}
HRESULT __stdcall QueryInterface(
REFIID riid,
void** ppv)
{
HRESULT hr = S_OK;
if(riid == IID_IUnknown)
{
*ppv = static_cast<IUnknown*>(this);
AddRef();
}
else
{
*ppv = NULL;
hr = E_NOINTERFACE;
}
return hr;
}
private:
DWORD m_cRef;
public:
CAtlStringW ClientName;
CAtlStringW EventCookie;
DWORD MajorVersion;
DWORD MinorVersion;
DWORD Revision;
};
class PropVariantWrapper : public tagPROPVARIANT
{
public:
PropVariantWrapper()
{
PropVariantInit(this);
}
PropVariantWrapper(LPCWSTR pszSrc)
{
PropVariantInit(this);
*this = pszSrc;
}
virtual ~PropVariantWrapper()
{
Clear();
}
void Clear()
{
PropVariantClear(this);
}
PropVariantWrapper& operator= (const ULONG ulValue)
{
Clear();
vt = VT_UI4;
ulVal = ulValue;
return *this;
}
PropVariantWrapper& operator= (_In_ LPCWSTR pszSrc)
{
Clear();
pwszVal = AtlAllocTaskWideString(pszSrc);
if(pwszVal != NULL)
{
vt = VT_LPWSTR;
}
return *this;
}
PropVariantWrapper& operator= (_In_ IUnknown* punkSrc)
{
Clear();
// Need to AddRef as PropVariantClear will Release
if (punkSrc != NULL)
{
vt = VT_UNKNOWN;
punkVal = punkSrc;
punkVal->AddRef();
}
return *this;
}
void SetErrorValue(const HRESULT hr)
{
Clear();
vt = VT_ERROR;
scode = hr;
}
void SetBoolValue(const bool bValue)
{
Clear();
vt = VT_BOOL;
if(bValue)
{
boolVal = VARIANT_TRUE;
}
else
{
boolVal = VARIANT_FALSE;
}
}
};
HRESULT UpdateDeviceFriendlyName(
_In_ IPortableDeviceClassExtension* pPortableDeviceClassExtension,
_In_ LPCWSTR wszDeviceFriendlyName);
HRESULT RegisterServices(
_In_ IPortableDeviceClassExtension* pPortableDeviceClassExtension,
const bool bUnregister);
HRESULT CheckRequestFilename(
_In_ LPCWSTR pszRequestFilename);
DWORD GetResourceSize(
const UINT uiResource);
PBYTE GetResourceData(
const UINT uiResource);
HRESULT AddStringValueToPropVariantCollection(
_In_ IPortableDevicePropVariantCollection* pCollection,
_In_ LPCWSTR wszValue);
HRESULT PostWpdEvent(
_In_ IPortableDeviceValues* pCommandParams,
_In_ IPortableDeviceValues* pEventParams);
HRESULT GetClientContextMap(
_In_ IPortableDeviceValues* pParams,
_Outptr_ ContextMap** ppContextMap);
HRESULT GetClientContext(
_In_ IPortableDeviceValues* pParams,
_In_ LPCWSTR pszContextKey,
_COM_Outptr_ IUnknown** ppContext);
HRESULT GetClientEventCookie(
_In_ IPortableDeviceValues* pParams,
_Outptr_result_maybenull_ LPWSTR* ppszEventCookie);
HRESULT AddPropertyAttributesByType(
const FakeDevicePropertyAttributesType type,
_In_ IPortableDeviceValues* pAttributes);
HRESULT SetPropertyAttributes(
_In_ REFPROPERTYKEY Key,
_In_reads_(cAttributeInfo) const PropertyAttributeInfo* AttributeInfo,
_In_ DWORD cAttributeInfo,
_In_ IPortableDeviceValues* pAttributes);
HRESULT SetMethodParameterAttributes(
_In_ REFPROPERTYKEY Parameter,
_In_reads_(cAttributeInfo) const MethodParameterAttributeInfo* AttributeInfo,
_In_ DWORD cAttributeInfo,
_In_ IPortableDeviceValues* pAttributes);
HRESULT SetEventParameterAttributes(
_In_ REFPROPERTYKEY Parameter,
_In_reads_(cAttributeInfo) const EventParameterAttributeInfo* AttributeInfo,
_In_ DWORD cAttributeInfo,
_In_ IPortableDeviceValues* pAttributes);
HRESULT SetEventParameters(
_In_ REFGUID Event,
_In_reads_(cAttributeInfo) const EventParameterAttributeInfo* AttributeInfo,
_In_ DWORD cAttributeInfo,
_In_ IPortableDeviceKeyCollection* pParameters);
|