summaryrefslogtreecommitdiff
path: root/wpd/WpdServiceSampleDriver/FakeContactsService.cpp
blob: 405279a9dc4ef6f6e5b29463b98727e1a7ebc827 (plain)
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
#include "stdafx.h"

#include "FakeContactsService.tmh"

const FormatAttributeInfo g_SupportedContactFormats[] =
{
    {&FORMAT_AbstractContact,   L"AbstractContact"},
    {&FORMAT_VCard2Contact,     L"VCard2"}
};

const GUID* g_SupportedMethods[] =
{
    &METHOD_FullEnumSyncSvc_BeginSync,
    &METHOD_FullEnumSyncSvc_EndSync,
    &MyCustomMethod
};

// Method parameters
const MethodParameterAttributeInfo g_MethodParameters[] =
{
    {&MyCustomMethodResult,      VT_BOOL,    WPD_PARAMETER_USAGE_RETURN, WPD_PARAMETER_ATTRIBUTE_FORM_UNSPECIFIED,       0, L"Result"},
    {&MyCustomMethodParam,       VT_UI4,     WPD_PARAMETER_USAGE_IN,     WPD_PARAMETER_ATTRIBUTE_FORM_UNSPECIFIED,       1, L"Integer_Param"},
    {&MyCustomMethodParamInOut,  VT_LPWSTR,  WPD_PARAMETER_USAGE_INOUT,  WPD_PARAMETER_ATTRIBUTE_FORM_OBJECT_IDENTIFIER, 2, L"ObjectId_Param"},
};

const GUID* g_SupportedServiceEvents[] =
{
    &WPD_EVENT_OBJECT_ADDED,
    &WPD_EVENT_OBJECT_REMOVED,
    &WPD_EVENT_OBJECT_UPDATED,
    &MyCustomEvent,
};

// Event parameters
const EventParameterAttributeInfo g_ServiceEventParameters[] =
{
    {&WPD_EVENT_OBJECT_ADDED,   &WPD_EVENT_PARAMETER_EVENT_ID,                              VT_CLSID},
    {&WPD_EVENT_OBJECT_ADDED,   &WPD_OBJECT_PERSISTENT_UNIQUE_ID,                           VT_LPWSTR},
    {&WPD_EVENT_OBJECT_ADDED,   &WPD_EVENT_PARAMETER_OBJECT_PARENT_PERSISTENT_UNIQUE_ID,    VT_LPWSTR},
    {&WPD_EVENT_OBJECT_ADDED,   &WPD_OBJECT_CONTAINER_FUNCTIONAL_OBJECT_ID,                 VT_LPWSTR},

    {&WPD_EVENT_OBJECT_REMOVED, &WPD_EVENT_PARAMETER_EVENT_ID,                              VT_CLSID},
    {&WPD_EVENT_OBJECT_REMOVED, &WPD_OBJECT_PERSISTENT_UNIQUE_ID,                           VT_LPWSTR},
    {&WPD_EVENT_OBJECT_REMOVED, &WPD_EVENT_PARAMETER_OBJECT_PARENT_PERSISTENT_UNIQUE_ID,    VT_LPWSTR},
    {&WPD_EVENT_OBJECT_REMOVED, &WPD_OBJECT_CONTAINER_FUNCTIONAL_OBJECT_ID,                 VT_LPWSTR},

    {&WPD_EVENT_OBJECT_UPDATED, &WPD_EVENT_PARAMETER_EVENT_ID,                              VT_CLSID},
    {&WPD_EVENT_OBJECT_UPDATED, &WPD_OBJECT_PERSISTENT_UNIQUE_ID,                           VT_LPWSTR},
    {&WPD_EVENT_OBJECT_UPDATED, &WPD_EVENT_PARAMETER_OBJECT_PARENT_PERSISTENT_UNIQUE_ID,    VT_LPWSTR},
    {&WPD_EVENT_OBJECT_UPDATED, &WPD_OBJECT_CONTAINER_FUNCTIONAL_OBJECT_ID,                 VT_LPWSTR},

    {&MyCustomEvent,            &WPD_EVENT_PARAMETER_EVENT_ID,                              VT_CLSID},
    {&MyCustomEvent,            &WPD_OBJECT_CONTAINER_FUNCTIONAL_OBJECT_ID,                 VT_LPWSTR},
    {&MyCustomEvent,            &MyCustomEventParam0,                                       VT_BOOL},
    {&MyCustomEvent,            &MyCustomEventParam1,                                       VT_UI4},
};

// Supported commands for this service
const PROPERTYKEY* g_ServiceSupportedCommands[] =
{
    // WPD_CATEGORY_OBJECT_ENUMERATION
    &WPD_COMMAND_OBJECT_ENUMERATION_START_FIND,
    &WPD_COMMAND_OBJECT_ENUMERATION_FIND_NEXT,
    &WPD_COMMAND_OBJECT_ENUMERATION_END_FIND,

    // WPD_CATEGORY_OBJECT_PROPERTIES
    &WPD_COMMAND_OBJECT_PROPERTIES_GET_SUPPORTED,
    &WPD_COMMAND_OBJECT_PROPERTIES_GET,
    &WPD_COMMAND_OBJECT_PROPERTIES_GET_ALL,
    &WPD_COMMAND_OBJECT_PROPERTIES_SET,
    &WPD_COMMAND_OBJECT_PROPERTIES_GET_ATTRIBUTES,
    &WPD_COMMAND_OBJECT_PROPERTIES_DELETE,

    // WPD_CATEGORY_OBJECT_RESOURCES
    &WPD_COMMAND_OBJECT_RESOURCES_GET_SUPPORTED,
    &WPD_COMMAND_OBJECT_RESOURCES_OPEN,
    &WPD_COMMAND_OBJECT_RESOURCES_READ,
    &WPD_COMMAND_OBJECT_RESOURCES_CLOSE,
    &WPD_COMMAND_OBJECT_RESOURCES_GET_ATTRIBUTES,

    // WPD_CATEGORY_OBJECT_MANAGEMENT
    &WPD_COMMAND_OBJECT_MANAGEMENT_CREATE_OBJECT_WITH_PROPERTIES_ONLY,
    &WPD_COMMAND_OBJECT_MANAGEMENT_DELETE_OBJECTS,

    // WPD_CATEGORY_OBJECT_PROPERTIES_BULK
    &WPD_COMMAND_OBJECT_PROPERTIES_BULK_GET_VALUES_BY_OBJECT_LIST_START,
    &WPD_COMMAND_OBJECT_PROPERTIES_BULK_GET_VALUES_BY_OBJECT_LIST_NEXT,
    &WPD_COMMAND_OBJECT_PROPERTIES_BULK_GET_VALUES_BY_OBJECT_LIST_END,
    &WPD_COMMAND_OBJECT_PROPERTIES_BULK_GET_VALUES_BY_OBJECT_FORMAT_START,
    &WPD_COMMAND_OBJECT_PROPERTIES_BULK_GET_VALUES_BY_OBJECT_FORMAT_NEXT,
    &WPD_COMMAND_OBJECT_PROPERTIES_BULK_GET_VALUES_BY_OBJECT_FORMAT_END,
    &WPD_COMMAND_OBJECT_PROPERTIES_BULK_SET_VALUES_BY_OBJECT_LIST_START,
    &WPD_COMMAND_OBJECT_PROPERTIES_BULK_SET_VALUES_BY_OBJECT_LIST_NEXT,
    &WPD_COMMAND_OBJECT_PROPERTIES_BULK_SET_VALUES_BY_OBJECT_LIST_END,

    // WPD_CATEGORY_COMMON
    &WPD_COMMAND_COMMON_GET_OBJECT_IDS_FROM_PERSISTENT_UNIQUE_IDS,

    // WPD_CATEGORY_SERVICE_COMMON
    &WPD_COMMAND_SERVICE_COMMON_GET_SERVICE_OBJECT_ID,

    // WPD_CATEGORY_SERVICE_CAPABILITIES
    &WPD_COMMAND_SERVICE_CAPABILITIES_GET_SUPPORTED_COMMANDS,
    &WPD_COMMAND_SERVICE_CAPABILITIES_GET_COMMAND_OPTIONS,
    &WPD_COMMAND_SERVICE_CAPABILITIES_GET_SUPPORTED_METHODS,
    &WPD_COMMAND_SERVICE_CAPABILITIES_GET_SUPPORTED_METHODS_BY_FORMAT,
    &WPD_COMMAND_SERVICE_CAPABILITIES_GET_METHOD_ATTRIBUTES,
    &WPD_COMMAND_SERVICE_CAPABILITIES_GET_METHOD_PARAMETER_ATTRIBUTES,
    &WPD_COMMAND_SERVICE_CAPABILITIES_GET_SUPPORTED_FORMATS,
    &WPD_COMMAND_SERVICE_CAPABILITIES_GET_FORMAT_ATTRIBUTES,
    &WPD_COMMAND_SERVICE_CAPABILITIES_GET_SUPPORTED_FORMAT_PROPERTIES,
    &WPD_COMMAND_SERVICE_CAPABILITIES_GET_FORMAT_PROPERTY_ATTRIBUTES,
    &WPD_COMMAND_SERVICE_CAPABILITIES_GET_SUPPORTED_EVENTS,
    &WPD_COMMAND_SERVICE_CAPABILITIES_GET_EVENT_ATTRIBUTES,
    &WPD_COMMAND_SERVICE_CAPABILITIES_GET_EVENT_PARAMETER_ATTRIBUTES,
    &WPD_COMMAND_SERVICE_CAPABILITIES_GET_INHERITED_SERVICES
};


HRESULT FakeContactsService::GetSupportedCommands(
    _In_    IPortableDeviceKeyCollection* pCommands)
{
    HRESULT hr = S_OK;

    if(pCommands == NULL)
    {
        hr = E_POINTER;
        CHECK_HR(hr, "Cannot have NULL parameter");
        return hr;
    }

    for (DWORD dwIndex = 0; dwIndex < ARRAYSIZE(g_ServiceSupportedCommands); dwIndex++)
    {
        PROPERTYKEY key = *(g_ServiceSupportedCommands[dwIndex]);
        hr = pCommands->Add(key);
        CHECK_HR(hr, "Failed to add supported command at index %d", dwIndex);
        if (FAILED(hr))
        {
            break;
        }
    }
    return hr;
}

HRESULT FakeContactsService::GetCommandOptions(
    _In_    REFPROPERTYKEY         Command,
    _In_    IPortableDeviceValues* pOptions)
{
    HRESULT hr = S_OK;

    if(pOptions == NULL)
    {
        hr = E_POINTER;
        CHECK_HR(hr, "Cannot have NULL parameter");
        return hr;
    }

    //  Check for command options
    if (IsEqualPropertyKey(WPD_COMMAND_OBJECT_MANAGEMENT_DELETE_OBJECTS, Command))
    {
        // This driver does not support recursive deletion
        hr = pOptions->SetBoolValue(WPD_OPTION_OBJECT_MANAGEMENT_RECURSIVE_DELETE_SUPPORTED, TRUE);
        CHECK_HR(hr, "Failed to set WPD_OPTION_OBJECT_MANAGEMENT_RECURSIVE_DELETE_SUPPORTED");
    }

    return hr;
}


HRESULT FakeContactsService::GetSupportedMethods(
    _In_    IPortableDevicePropVariantCollection* pMethods)
{
    HRESULT hr = S_OK;

    if (pMethods == NULL)
    {
        hr = E_POINTER;
        CHECK_HR(hr, "Cannot have NULL parameter");
        return hr;
    }

    // Add the supported methods to the collection.
    for (DWORD dwIndex = 0; dwIndex < ARRAYSIZE(g_SupportedMethods); dwIndex++)
    {
        PROPVARIANT pv;
        pv.vt = VT_CLSID;
        pv.puuid = (GUID*)g_SupportedMethods[dwIndex]; // Assignment only, don't PropVariantClear this

        hr = pMethods->Add(&pv);
        CHECK_HR(hr, "Failed to add supported method at index %d", dwIndex);
        if (FAILED(hr))
        {
            break;
        }
    }

    return hr;
}

BOOL FakeContactsService::IsMethodSupported(
    _In_    REFGUID Method)
{
    // Add the supported methods to the collection.
    for (DWORD dwIndex = 0; dwIndex < ARRAYSIZE(g_SupportedMethods); dwIndex++)
    {
        if (Method == *g_SupportedMethods[dwIndex])
        {
            return TRUE;
        }
    }

    return FALSE;
}

HRESULT FakeContactsService::GetSupportedMethodsByFormat(
    _In_    REFGUID                               Format,
    _In_    IPortableDevicePropVariantCollection* pMethods)
{
    HRESULT hr = S_OK;

    if (pMethods == NULL)
    {
        hr = E_POINTER;
        CHECK_HR(hr, "Cannot have NULL parameter");
        return hr;
    }

    hr = HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
    for (DWORD i=0; i<ARRAYSIZE(g_SupportedContactFormats); i++)
    {
        if (Format == *g_SupportedContactFormats[i].pFormatGuid)
        {
            // Add the supported methods for the format to the collection, right now there are none, so we
            // return an emtpy collection
            hr = S_OK;
            break;
        }
    }
    CHECK_HR(hr, "Format %ws is not supported", CComBSTR(Format));

    return hr;
}


HRESULT FakeContactsService::GetMethodAttributes(
    _In_    REFGUID                Method,
    _In_    IPortableDeviceValues* pAttributes)
{
    HRESULT hr = S_OK;
    CComPtr<IPortableDeviceKeyCollection> pParameters;

    if (pAttributes == NULL)
    {
        hr = E_POINTER;
        CHECK_HR(hr, "Cannot have NULL parameter");
        return hr;
    }

    // CoCreate a collection for specifying the method parameters.
    hr = CoCreateInstance(CLSID_PortableDeviceKeyCollection,
                          NULL,
                          CLSCTX_INPROC_SERVER,
                          IID_IPortableDeviceKeyCollection,
                          (VOID**) &pParameters);
    CHECK_HR(hr, "Failed to CoCreate CLSID_PortableDeviceKeyCollection");

    // Add the method attributes to this collection
    if (Method == METHOD_FullEnumSyncSvc_BeginSync)
    {
        if (hr == S_OK)
        {
            hr = pAttributes->SetStringValue(WPD_METHOD_ATTRIBUTE_NAME, L"BeginSync");
            CHECK_HR(hr, "Failed to set WPD_METHOD_ATTRIBUTE_NAME");
        }

        if (hr == S_OK)
        {
            hr = pAttributes->SetUnsignedIntegerValue(WPD_METHOD_ATTRIBUTE_ACCESS, WPD_COMMAND_ACCESS_READWRITE);
            CHECK_HR(hr, "Failed to set WPD_METHOD_ATTRIBUTE_ACCESS");
        }

        if (hr == S_OK)
        {
            // no parameters, set empty collection
            hr = pAttributes->SetIPortableDeviceKeyCollectionValue(WPD_METHOD_ATTRIBUTE_PARAMETERS, pParameters);
            CHECK_HR(hr, "Failed to set WPD_METHOD_ATTRIBUTE_PARAMETERS");
        }

        if (hr == S_OK)
        {
            // no associated format, set GUID_NULL
            hr = pAttributes->SetGuidValue(WPD_METHOD_ATTRIBUTE_ASSOCIATED_FORMAT, GUID_NULL);
            CHECK_HR(hr, "Failed to set WPD_METHOD_ATTRIBUTE_ASSOCIATED_FORMAT");
        }

    }
    else if (Method == METHOD_FullEnumSyncSvc_EndSync)
    {
        if (hr == S_OK)
        {
            hr = pAttributes->SetStringValue(WPD_METHOD_ATTRIBUTE_NAME, L"EndSync");
            CHECK_HR(hr, "Failed to set WPD_METHOD_ATTRIBUTE_NAME");
        }

        if (hr == S_OK)
        {
            hr = pAttributes->SetUnsignedIntegerValue(WPD_METHOD_ATTRIBUTE_ACCESS, WPD_COMMAND_ACCESS_READWRITE);
            CHECK_HR(hr, "Failed to set WPD_METHOD_ATTRIBUTE_ACCESS");
        }

        if (hr == S_OK)
        {
            // no parameters, set empty collection
            hr = pAttributes->SetIPortableDeviceKeyCollectionValue(WPD_METHOD_ATTRIBUTE_PARAMETERS, pParameters);
            CHECK_HR(hr, "Failed to set WPD_METHOD_ATTRIBUTE_PARAMETERS");
        }

        if (hr == S_OK)
        {
            // no associated format, set GUID_NULL
            hr = pAttributes->SetGuidValue(WPD_METHOD_ATTRIBUTE_ASSOCIATED_FORMAT, GUID_NULL);
            CHECK_HR(hr, "Failed to set WPD_METHOD_ATTRIBUTE_ASSOCIATED_FORMAT");
        }
    }
    else if (Method == MyCustomMethod)
    {
        if (hr == S_OK)
        {
            hr = pAttributes->SetStringValue(WPD_METHOD_ATTRIBUTE_NAME, L"CustomMethod");
            CHECK_HR(hr, "Failed to set WPD_METHOD_ATTRIBUTE_NAME");
        }

        if (hr == S_OK)
        {
            hr = pAttributes->SetUnsignedIntegerValue(WPD_METHOD_ATTRIBUTE_ACCESS, WPD_COMMAND_ACCESS_READWRITE);
            CHECK_HR(hr, "Failed to set WPD_METHOD_ATTRIBUTE_ACCESS");
        }

        if (hr == S_OK)
        {
            // Set the supported parameters
            for (size_t i=0; i<ARRAYSIZE(g_MethodParameters); i++)
            {
                pParameters->Add(*(g_MethodParameters[i].pKey));
            }

            hr = pAttributes->SetIPortableDeviceKeyCollectionValue(WPD_METHOD_ATTRIBUTE_PARAMETERS, pParameters);
            CHECK_HR(hr, "Failed to set WPD_METHOD_ATTRIBUTE_PARAMETERS");
        }

        if (hr == S_OK)
        {
            // no associated format, set GUID_NULL
            hr = pAttributes->SetGuidValue(WPD_METHOD_ATTRIBUTE_ASSOCIATED_FORMAT, GUID_NULL);
            CHECK_HR(hr, "Failed to set WPD_METHOD_ATTRIBUTE_ASSOCIATED_FORMAT");
        }
    }
    else
    {
        hr = HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
        CHECK_HR(hr, "Unknown method %ws received",CComBSTR(Method));
    }

    return hr;
}

HRESULT FakeContactsService::GetMethodParameterAttributes(
    _In_    REFPROPERTYKEY         Parameter,
    _In_    IPortableDeviceValues* pAttributes)
{
    HRESULT hr = S_OK;

    if (pAttributes == NULL)
    {
        hr = E_POINTER;
        CHECK_HR(hr, "Cannot have NULL parameter");
        return hr;
    }

    hr = SetMethodParameterAttributes(Parameter, &g_MethodParameters[0], ARRAYSIZE(g_MethodParameters), pAttributes);
    CHECK_HR(hr, "Failed to set method parameter attributes");

    return hr;
}

HRESULT FakeContactsService::GetSupportedFormats(
    _In_    IPortableDevicePropVariantCollection* pFormats)
{
    HRESULT hr = S_OK;

    if (pFormats == NULL)
    {
        hr = E_POINTER;
        CHECK_HR(hr, "Cannot have NULL parameter");
        return hr;
    }

    if (hr == S_OK)
    {
        // Add the supported formats to this collection
        for (DWORD i=0; i<ARRAYSIZE(g_SupportedContactFormats); i++)
        {
            PROPVARIANT pv = {0};
            pv.vt = VT_CLSID;
            pv.puuid = (CLSID*)g_SupportedContactFormats[i].pFormatGuid; // assignment, do not call PropVariantClear

            hr = pFormats->Add(&pv);
            CHECK_HR(hr, "Failed to add format to IPortableDevicePropVariantCollection");
        }
    }

    return hr;
}

HRESULT FakeContactsService::GetFormatAttributes(
    _In_    REFGUID                Format,
    _In_    IPortableDeviceValues* pAttributes)
{
    HRESULT hr = S_OK;

    if (pAttributes == NULL)
    {
        hr = E_POINTER;
        CHECK_HR(hr, "Cannot have NULL parameter");
        return hr;
    }

    hr = HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);

    // Add the supported formats to this collection
    for (DWORD i=0; i<ARRAYSIZE(g_SupportedContactFormats); i++)
    {
        if (Format == *g_SupportedContactFormats[i].pFormatGuid)
        {
            hr = pAttributes->SetStringValue(WPD_FORMAT_ATTRIBUTE_NAME, g_SupportedContactFormats[i].wszName);
            CHECK_HR(hr, "Failed to set WPD_FORMAT_ATTRIBUTE_NAME");
            break;
        }
    }

    if (hr == HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED))
    {
        CHECK_HR(hr, "Unknown format %ws received",CComBSTR(Format));
    }

    return hr;
}

HRESULT FakeContactsService::GetSupportedFormatProperties(
    _In_    REFGUID                       Format,
    _In_    IPortableDeviceKeyCollection* pKeys)
{
    HRESULT hr = S_OK;

    if (pKeys == NULL)
    {
        hr = E_POINTER;
        CHECK_HR(hr, "Cannot have NULL parameter");
        return hr;
    }

    // Add the supported format properties to this collection
    // The formats of this service happen to support the same set of properties
    hr = HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
    for (DWORD i=0; i<ARRAYSIZE(g_SupportedContactFormats); i++)
    {
        if (Format == (*g_SupportedContactFormats[i].pFormatGuid))
        {
            hr = GetSupportedContactProperties(pKeys);
            CHECK_HR(hr, "Failed to add supported contact format properties");
            break;
        }
    }
    CHECK_HR(hr, "Format %ws is not supported", CComBSTR(Format));

    return hr;
}

HRESULT FakeContactsService::GetPropertyAttributes(
    _In_    REFGUID                Format,
    _In_    REFPROPERTYKEY         Property,
    _In_    IPortableDeviceValues* pAttributes)
{
    HRESULT hr = HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);

    for (DWORD i=0; i<ARRAYSIZE(g_SupportedContactFormats); i++)
    {
        if (Format == (*g_SupportedContactFormats[i].pFormatGuid))
        {
            hr = GetContactPropertyAttributes(Property, pAttributes);
            CHECK_HR(hr, "Failed to get property attributes");
            break;
        }
    }

    CHECK_HR(hr, "Failed to find supported format to retrieve property attributes");

    return hr;
}

HRESULT FakeContactsService::GetSupportedEvents(
    _In_    IPortableDevicePropVariantCollection* pEvents)
{
    HRESULT hr = S_OK;

    if (pEvents == NULL)
    {
        hr = E_POINTER;
        CHECK_HR(hr, "Cannot have NULL parameter");
        return hr;
    }

    PROPVARIANT pv;
    pv.vt = VT_CLSID;

    for (DWORD i=0; i<ARRAYSIZE(g_SupportedServiceEvents); i++)
    {
        pv.puuid = (CLSID*)g_SupportedServiceEvents[i];  // Assignment, don't PropVariantClear this

        hr = pEvents->Add(&pv);
        CHECK_HR(hr, "Failed to add event to the collection");
    }

    return hr;
}

HRESULT FakeContactsService::GetEventAttributes(
    _In_    REFGUID                Event,
    _In_    IPortableDeviceValues* pAttributes)
{
    HRESULT hr = S_OK;
    CComPtr<IPortableDeviceValues> pEventOptions;
    CComPtr<IPortableDeviceKeyCollection> pEventParameters;

    if (pAttributes == NULL)
    {
        hr = E_POINTER;
        CHECK_HR(hr, "Cannot have NULL parameter");
        return hr;
    }

    // CoCreate a collection to store the event options.
    hr = CoCreateInstance(CLSID_PortableDeviceValues,
                          NULL,
                          CLSCTX_INPROC_SERVER,
                          IID_IPortableDeviceValues,
                          (VOID**) &pEventOptions);
    CHECK_HR(hr, "Failed to CoCreate CLSID_PortableDeviceValues");
    if (hr == S_OK)
    {
        hr = pEventOptions->SetBoolValue(WPD_EVENT_OPTION_IS_BROADCAST_EVENT, TRUE);
        CHECK_HR(hr, "Failed to set WPD_EVENT_OPTION_IS_BROADCAST_EVENT");
    }

    // Loop through the supported events for this service to find a match
    hr = HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
    for (DWORD i=0; i<ARRAYSIZE(g_SupportedServiceEvents); i++)
    {
        if (Event == *g_SupportedServiceEvents[i])
        {
            // Set the event options.
            hr = pAttributes->SetIPortableDeviceValuesValue(WPD_EVENT_ATTRIBUTE_OPTIONS, pEventOptions);
            CHECK_HR(hr, "Failed to set WPD_EVENT_ATTRIBUTE_OPTIONS");

            // Set the event parameters.
            if (hr == S_OK)
            {
                hr = CoCreateInstance(CLSID_PortableDeviceKeyCollection,
                                      NULL,
                                      CLSCTX_INPROC_SERVER,
                                      IID_IPortableDeviceKeyCollection,
                                      (VOID**) &pEventParameters);
                CHECK_HR(hr, "Failed to CoCreate CLSID_PortableDeviceValues");
            }

            if (hr == S_OK)
            {
                hr = SetEventParameters(Event, &g_ServiceEventParameters[0], ARRAYSIZE(g_ServiceEventParameters), pEventParameters);
                CHECK_HR(hr, "Failed to set event parameters");

                if (hr == S_OK)
                {
                    hr = pAttributes->SetIPortableDeviceKeyCollectionValue(WPD_EVENT_ATTRIBUTE_PARAMETERS, pEventParameters);
                    CHECK_HR(hr, "Failed to set WPD_METHOD_ATTRIBUTE_PARAMETERS");
                }
            }

            // Set a name for the custom event
            if (hr == S_OK)
            {
                if (Event == MyCustomEvent)
                {
                    hr = pAttributes->SetStringValue(WPD_EVENT_ATTRIBUTE_NAME , L"MyCustomEvent");
                    CHECK_HR(hr, "Failed to set WPD_EVENT_ATTRIBUTE_NAME");
                }
            }
            break;
        }
    }

    return hr;
}

HRESULT FakeContactsService::GetEventParameterAttributes(
    _In_    REFPROPERTYKEY         Parameter,
    _In_    IPortableDeviceValues* pAttributes)
{
    HRESULT hr = S_OK;

    if (pAttributes == NULL)
    {
        hr = E_POINTER;
        CHECK_HR(hr, "Cannot have NULL parameter");
        return hr;
    }

    hr = SetEventParameterAttributes(Parameter, &g_ServiceEventParameters[0], ARRAYSIZE(g_ServiceEventParameters), pAttributes);
    CHECK_HR(hr, "Failed to set event parameter attributes");

    return hr;
}

HRESULT FakeContactsService::GetInheritedServices(
            const DWORD                           dwInheritanceType,
    _In_    IPortableDevicePropVariantCollection* pServices)
{
    HRESULT hr = S_OK;

    if (pServices == NULL)
    {
        hr = E_POINTER;
        CHECK_HR(hr, "Cannot have NULL parameter");
        return hr;
    }

    if (dwInheritanceType == WPD_SERVICE_INHERITANCE_IMPLEMENTATION)
    {
        PROPVARIANT pv;
        pv.vt = VT_CLSID;
        pv.puuid = (CLSID*)&SERVICE_FullEnumSync;  // Assignment, don't PropVariantClear this

        hr = pServices->Add(&pv);
        CHECK_HR(hr, "Failed to add service GUID to the collection");
    }

    return hr;
}

HRESULT FakeContactsService::OnBeginSync(
    _In_    IPortableDeviceValues* pParams,
    _In_    IPortableDeviceValues* pResults)
{
    UNREFERENCED_PARAMETER(pParams);
    UNREFERENCED_PARAMETER(pResults);

    // This is where the sync service receives a notification from the application that
    // sync is about to begin so that it can lock the session
    // This method does not do anything right now

    return S_OK;
}

HRESULT FakeContactsService::OnEndSync(
    _In_    IPortableDeviceValues* pParams,
    _In_    IPortableDeviceValues* pResults)
{
    UNREFERENCED_PARAMETER(pParams);
    UNREFERENCED_PARAMETER(pResults);

    // This is where the sync service receives a notification from the application that
    // sync is about to end so that it can unlock the session
    // This method does not do anything right now

    return S_OK;
}

// This demonstrates how a custom service method can be implemented
HRESULT FakeContactsService::OnMyCustomMethod(
    _In_    IPortableDeviceValues*  pParams,
    _In_    IPortableDeviceValues*  pResults,
    _In_    IPortableDeviceValues*  pEventParams)
{
    HRESULT hr            = S_OK;
    DWORD   dwParamValue  = 0;
    BOOL    bResultValue  = FALSE;
    LPWSTR  pszParamValue = NULL;

    if (pParams == NULL || pResults == NULL || pEventParams == NULL)
    {
        hr = E_POINTER;
        CHECK_HR(hr, "Cannot have NULL parameter");
        return hr;
    }

    hr = pParams->GetUnsignedIntegerValue(MyCustomMethodParam, &dwParamValue);
    CHECK_HR(hr, "Failed to get MyCustomMethodParam");

    if (hr == S_OK)
    {
        hr = pParams->GetStringValue(MyCustomMethodParamInOut, &pszParamValue);
        CHECK_HR(hr, "Failed to get MyCustomMethodParamInOut");
    }

    if (hr == S_OK)
    {
        // For demonstration purposes only, we simply return the inout parameter as is
        hr = pResults->SetStringValue(MyCustomMethodParamInOut, pszParamValue);
        CHECK_HR(hr, "Failed to set MyCustomMethodParamInOut");
    }

    if (hr == S_OK)
    {
        // This is where the device will process the method invocation
        // For demonstration purposes only, we return TRUE if the input is an even number
        bResultValue = (dwParamValue % 1 == 0)?TRUE:FALSE;
    }

    if (hr == S_OK)
    {
        hr = pResults->SetBoolValue(MyCustomMethodResult, bResultValue);
        CHECK_HR(hr, "Failed to set MyCustomMethodResult");
    }

    if (hr == S_OK)
    {
        hr = pEventParams->SetGuidValue(WPD_EVENT_PARAMETER_EVENT_ID, MyCustomEvent);
        CHECK_HR(hr, "Failed to add WPD_EVENT_PARAMETER_EVENT_ID");

        if (hr == S_OK)
        {
            // Adding this event parameter will allow WPD to scope this event to the container functional object
            hr = pEventParams->SetStringValue(WPD_OBJECT_CONTAINER_FUNCTIONAL_OBJECT_ID, RequestFilename);
            CHECK_HR(hr, "Failed to add WPD_OBJECT_CONTAINER_FUNCTIONAL_OBJECT_ID");
        }

        if (hr == S_OK)
        {
            // set the first custom event parameter
            hr = pEventParams->SetBoolValue(MyCustomEventParam0, bResultValue);
            CHECK_HR(hr, "Failed to add MyCustomEvent parameter 0");
        }

        if (hr == S_OK)
        {
            // set the second custom event parameter
            hr = pEventParams->SetUnsignedIntegerValue(MyCustomEventParam1, dwParamValue);
            CHECK_HR(hr, "Failed to add MyCustomEvent parameter 1");
        }
    }

    CoTaskMemFree(pszParamValue);
    return hr;
}