summaryrefslogtreecommitdiff
path: root/print/OEM Printer Customization Plug-in Samples/C++/gdl/src/gdlsmpl.cpp
blob: 6d8fd195cadb84e7e5a9fe992611b048a715d64c (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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
//+--------------------------------------------------------------------------
//
//  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
//  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
//  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//
//  Copyright  1997 - 2006  Microsoft Corporation.  All Rights Reserved.
//
//  FILE:       gdlsmpl.cpp
//
//  PURPOSE:    Implementation of interface for Unidrv5 UI plug-ins.
//
//--------------------------------------------------------------------------

#include "precomp.h"

#include "debug.h"
#include "devmode.h"
#include "intrface.h"
#include "gdlsmpl.h"

// This indicates to Prefast that this is a usermode driver file.
_Analysis_mode_(_Analysis_code_type_user_driver_);

//--------------------------------------------------------------------------
//      Globals
//--------------------------------------------------------------------------

HINSTANCE   ghInstance = NULL;
    // Module's Instance handle from DLLEntry of process.


//--------------------------------------------------------------------------
//      Internal Globals
//--------------------------------------------------------------------------

static long g_cComponents = 0;
    // Count of active components

static long g_cServerLocks = 0;
    // Count of locks


//--------------------------------------------------------------------------
//      Local helper functions
//--------------------------------------------------------------------------
BOOL __stdcall
DebugPrint(
    LPCWSTR     pszMessage,
        // Format string for the error message
    ...
        // args specified in the format string.
    )
{
    va_list arglist;
        // varargs list for processing the '...' parameter.

    WCHAR szMsgBuf[MAX_PATH] = {0};
        // Use a stack error buffer so that we don't need to
        // allocate in the failure path.

    HRESULT     hResult;
        // Result from formatting the string.

    if (NULL == pszMessage)
    {
        return FALSE;
    }

    // Pass the variable parameters to wvsprintf to be formated.
    va_start(arglist, pszMessage);
    hResult = StringCbVPrintfW(szMsgBuf, MAX_PATH*sizeof(szMsgBuf[0]), pszMessage, arglist);
    va_end(arglist);

    // Dump string to debug output.
    OutputDebugStringW(szMsgBuf);

    return SUCCEEDED(hResult);
}


//--------------------------------------------------------------------------
//      Class factory for the Plug-In object
//--------------------------------------------------------------------------
class COemCF : public IClassFactory
{
public:

    // IUnknown methods
    STDMETHOD(QueryInterface) (THIS_
        REFIID riid,
        LPVOID FAR* ppvObj
        );

    STDMETHOD_(ULONG,AddRef) (THIS);

    _At_(this, __drv_freesMem(object))
        STDMETHOD_(ULONG,Release) (THIS);

    // IClassFactory methods
    STDMETHOD(CreateInstance) (THIS_
        _In_opt_    LPUNKNOWN pUnkOuter,
        _In_        REFIID riid,
        _Outptr_ LPVOID FAR* ppvObject
        );

    STDMETHOD(LockServer) (THIS_
        BOOL bLock
        );

    // Class-specific methods.
    COemCF(): m_cRef(1) { };

    ~COemCF() { };

protected:
    LONG m_cRef;

};


//+---------------------------------------------------------------------------
//
//  Member:
//      ::DllMain
//
//  Synopsis:
//      Dll's main routine, called when new
//      threads or processes reference the module
//
//  Returns:
//      TRUE.  Currently has no failure paths.  Return false if the module
//      cannot be properly initialized.
//
//  Notes:
//      the caller of this routine holds a process-wide lock preventing more
//      than one module from being initialized at a time.  In general,
//      plug-ins should do as little as possible in this routine to avoid
//      stalling other threads that may need to load modules.  Also, calling
//      any function that would, in-turn, cause another module to be loaded
//      could result in a deadlock.
//
//
//----------------------------------------------------------------------------
extern "C" BOOL
WINAPI DllMain(
    HINSTANCE hInst,
    WORD wReason,
    LPVOID lpReserved
    )
{
    UNREFERENCED_PARAMETER(hInst);
    UNREFERENCED_PARAMETER(lpReserved);

    switch(wReason)
    {
        case DLL_PROCESS_ATTACH:
            ghInstance = hInst;
            break;

        case DLL_THREAD_ATTACH:
            break;

        case DLL_PROCESS_DETACH:
            break;

        case DLL_THREAD_DETACH:
            break;
    }

    return TRUE;
}

//+---------------------------------------------------------------------------
//
//  Member:
//      ::DllCanUnloadNow
//
//  Synopsis:
//      Can the DLL be unloaded from memory?  Answers no if any objects are
//      still allocated, or if the server has been locked.
//
//      To avoid leaving OEM DLL still in memory when Unidrv or Pscript
//      drivers are unloaded, Unidrv and Pscript driver ignore the return
//      value of DllCanUnloadNow of the OEM DLL, and always call FreeLibrary
//      on the OEMDLL.
//
//      If the plug-in spins off a working thread that also uses this DLL,
//      the thread needs to call LoadLibrary and FreeLibraryAndExitThread,
//      otherwise it may crash after Unidrv or Pscript calls FreeLibrary.
//
//  Returns:
//      S_OK if the DLL can be unloaded safely, otherwise S_FALSE
//
//
//----------------------------------------------------------------------------
STDAPI DllCanUnloadNow()
{
    if ((g_cComponents == 0) && (g_cServerLocks == 0))
    {
        return S_OK;
    }
    else
    {
        return S_FALSE;
    }
}


//+---------------------------------------------------------------------------
//
//  Member:
//      ::DllGetClassObject
//
//  Synopsis:
//      Retrieve the class factory object for the indicated class.
//
//  Returns:
//      CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, or S_OK.
//
//  Notes:
//
//
//----------------------------------------------------------------------------
STDAPI
DllGetClassObject(
    _In_ CONST CLSID& clsid,
        // GUID of the class object that the caller wants a class factory for
    _In_ CONST IID& iid,
        // Interface ID to provide in ppv
    _Outptr_ VOID** ppv
        // out pointer to the interface requested.
    )
{
    if (ppv == NULL)
    {
        return E_POINTER;
    }
    *ppv = NULL;

    //
    // Can we create this component?
    //
    if (clsid != CLSID_OEMUI)
    {
        return CLASS_E_CLASSNOTAVAILABLE;
    }

    //
    // Create class factory.
    // Reference count set to 1 in Constructor
    //
    COemCF* pGDLSampleClassFactory = new COemCF;
    if (pGDLSampleClassFactory == NULL)
    {
        return E_OUTOFMEMORY;
    }

    //
    // Get requested interface.
    //
    HRESULT hr = pGDLSampleClassFactory->QueryInterface(iid, ppv);
    pGDLSampleClassFactory->Release();

    return hr;
}


//+---------------------------------------------------------------------------
//
//  Member:
//      COemCF::QueryInterface
//
//  Synopsis:
//      Standard COM IUnknown implementation.
//
//
//----------------------------------------------------------------------------
HRESULT __stdcall
COemCF::QueryInterface(
    CONST IID& iid,
    VOID** ppv
    )
{
    if ((iid == IID_IUnknown) || (iid == IID_IClassFactory))
    {
        *ppv = static_cast<COemCF*>(this);
    }
    else
    {
        *ppv = NULL;
        return E_NOINTERFACE;
    }
    reinterpret_cast<IUnknown*>(*ppv)->AddRef();
    return S_OK;
}

//+---------------------------------------------------------------------------
//
//  Member:
//      COemCF::AddRef
//
//  Synopsis:
//      Standard COM IUnknown implementation.
//
//
//----------------------------------------------------------------------------
ULONG __stdcall
COemCF::AddRef()
{
    return InterlockedIncrement(&m_cRef);
}

//+---------------------------------------------------------------------------
//
//  Member:
//      COemCF::Release
//
//  Synopsis:
//      Standard COM IUnknown implementation.
//
//
//----------------------------------------------------------------------------
_At_(this, __drv_freesMem(object))
ULONG __stdcall
COemCF::Release()
{
    assert(0 != m_cRef);

   ULONG cRef = InterlockedDecrement(&m_cRef);
   if (0 == cRef)
   {
      delete this;
   }
   return cRef;
}


//+---------------------------------------------------------------------------
//
//  Member:
//      COemCF::CreateInstance
//
//  Synopsis:
//      Attempt to create an object that implements the specified interface.
//
//
//----------------------------------------------------------------------------
HRESULT __stdcall
COemCF::CreateInstance(
    _In_opt_    IUnknown* pUnknownOuter,
    _In_        CONST IID& iid,
    _Outptr_ VOID** ppv
    )
{
    if (ppv == NULL)
    {
        return E_POINTER;
    }
    *ppv = NULL;

    //
    // Cannot aggregate.
    //
    if (pUnknownOuter != NULL)
    {
        return CLASS_E_NOAGGREGATION;
    }

    HRESULT hr = S_OK;

    if (iid == IID_IUnknown ||
        iid == IID_IPrintOemUI2)
    {
        //
        // Create component.
        //
        COemUI2* pOemCB = new COemUI2;
        if (pOemCB == NULL)
        {
            hr = E_OUTOFMEMORY;
        }
        else
        {
            //
            // Get the requested interface.
            //
            hr = pOemCB->QueryInterface(iid, ppv);

            //
            // Release the IUnknown pointer.
            // (If QueryInterface failed, component will delete itself.)
            //
            pOemCB->Release();
        }
    }
    else
    {
        hr = E_NOINTERFACE;
    }

    return hr;
}


//+---------------------------------------------------------------------------
//
//  Member:
//      COemCF::LockServer
//
//  Synopsis:
//      Locking the server holds the objects server in memory, so that new
//      instances of objects can be created more quickly.
//
//  Returns:
//      S_OK
//
//
//----------------------------------------------------------------------------
HRESULT __stdcall
COemCF::LockServer(
    BOOL bLock
        // If true, increment the lock count, otherwise decrement the lock
        // count.
    )
{
    if (bLock)
    {
        InterlockedIncrement(&g_cServerLocks);
    }
    else
    {
        InterlockedDecrement(&g_cServerLocks);
    }

    return S_OK;
}


//+---------------------------------------------------------------------------
//
//  Member:
//      COemUI2 (destructor)
//
//  Synopsis:
//      Release the helper interface, as well as any other resources that
//      the plug-in is holding onto.
//
//  Returns:
//
//  Notes:
//
//
//----------------------------------------------------------------------------
COemUI2::~COemUI2()
{
    //
    // Make sure that helper interface is released.
    //
    if(NULL != m_pCoreHelper)
    {
        m_pCoreHelper->Release();
        m_pCoreHelper = NULL;
    }

    //
    // If this instance of the object is being deleted, then the reference
    // count should be zero.
    //
    assert(0 == m_cRef);
}


//+---------------------------------------------------------------------------
//
//  Member:
//      COemUI2::PublishDriverInterface
//
//  Synopsis:
//      This routine is called by the core driver to supply objects to the
//      plug-in which the plug-in can use to implement various features.
//
//      This implementation requires an object that IPrintCoreHelper interface
//      from the core driver to implement full UI replacement.  If the object
//      passed in is the one desired, this routine will return S_OK, otherwise
//      it will return E_FAIL.
//
//
//----------------------------------------------------------------------------
HRESULT __stdcall
COemUI2::PublishDriverInterface(
    IUnknown *pIUnknown
    )
{
    HRESULT hr = S_OK;

    //
    // Save a pointer to the IPrintCoreHelperUni interface.  This will allow
    // the plug-in to access it's GDL file (which can be it's GPD file as well)
    //
    if (m_pCoreHelper == NULL)
    {
        hr = pIUnknown->QueryInterface(IID_IPrintCoreHelperUni, (VOID**) &(m_pCoreHelper));

        if (!SUCCEEDED(hr))
        {
            m_pCoreHelper = NULL;
            hr = E_FAIL;
        }
    }

    return hr;
}

//+---------------------------------------------------------------------------
//
//  Member:
//      COemUI2::GetInfo
//
//  Synopsis:
//      Most of this routine is a standard implementation common to most
//      plug-ins.  Of particular note in this sample is that to get a handle
//      the IPrintCoreHelper plug-in in the PublishDriverInterface callback,
//      this routine must return OEMPUBLISH_IPRINTCOREHELPER in the out
//      parameter when the mode is OEMGI_GETREQUESTEDHELPERINTERFACES.
//
//  Returns:
//      S_OK for supported modes, E_FAIL for everything else.
//
//
//----------------------------------------------------------------------------
HRESULT __stdcall
COemUI2::GetInfo(
    DWORD  dwMode,
        // The mode indicates what information is being requested by the
        // core driver.
    _Out_writes_bytes_(cbSize) PVOID  pBuffer,
        // The output buffer is the location into which the requested info
        // should be written
    DWORD  cbSize,
        // The size of the output buffer.
    PDWORD pcbNeeded
        // If the size of the buffer is insufficient, use this parameter
        // to indicate to the core driver how much space is required.
    )
{
    HRESULT hrResult = S_OK;

    //
    // Validate parameters.  OEMGI_GETPUBLISHERINFO is excluded from this
    // list because that GetInfo mode applies only to PScript rendering plug-ins.
    //
    if ((NULL == pcbNeeded) ||
        ((OEMGI_GETSIGNATURE != dwMode) &&
         (OEMGI_GETVERSION != dwMode) &&
         (OEMGI_GETREQUESTEDHELPERINTERFACES != dwMode)))
    {
        WARNING("COemUI2::GetInfo() exit pcbNeeded is NULL or mode is not supported\r\n");
        SetLastError(ERROR_INVALID_PARAMETER);
        return E_FAIL;
    }

    //
    // Set expected buffer size and number of bytes written.
    //
    *pcbNeeded = sizeof(DWORD);

    //
    // Check buffer size is sufficient.
    //
    if((cbSize < *pcbNeeded) || (NULL == pBuffer))
    {
        WARNING("COemUI2::GetInfo() exit insufficient buffer!\r\n");
        SetLastError(ERROR_INSUFFICIENT_BUFFER);
        hrResult = E_FAIL;
    }
    else
    {
        switch(dwMode)
        {
            //
            // OEM DLL Signature
            //
            case OEMGI_GETSIGNATURE:
                *(PDWORD)pBuffer = OEM_SIGNATURE;
                break;

            //
            // OEM DLL version
            //
            case OEMGI_GETVERSION:
                *(PDWORD)pBuffer = OEM_VERSION;
                break;

            //
            // The UI is asking whether the plug-in uses the IPrintCoreHelper based
            // helper object.  Indicate that the plug-in does need this object.
            //
            case OEMGI_GETREQUESTEDHELPERINTERFACES:
                *(PDWORD)pBuffer = 0;
                *(PDWORD)pBuffer |= OEMPUBLISH_IPRINTCOREHELPER;
                break;

            //
            // dwMode not supported.
            //
            default:
                //
                // Set written bytes to zero since nothing was written.
                //
                WARNING("COemUI2::GetInfo() exit mode not supported.\r\n");
                *pcbNeeded = 0;
                SetLastError(ERROR_NOT_SUPPORTED);
                hrResult = E_FAIL;
        }
    }

    return hrResult;
}

//+---------------------------------------------------------------------------
//
//  Member:
//      COemUI2::DevMode
//
//  Synopsis:
//      This routine operates in various modes to manage the private DEVMODE.
//      This sample does not provide much by way of illustration of how to
//      handle OEM settings in the private DEVMODE.  See the sample OEMUI for
//      a more in-depth look at this routine.  The function hrOEMDevMode in
//      devmode.cpp also provides more information on how to handle the various
//      modes.
//
//  Returns:
//      S_OK on success, else E_*
//
//
//----------------------------------------------------------------------------
HRESULT __stdcall
COemUI2::DevMode(
    DWORD  dwMode,
        // The operation that should be performed
    POEMDMPARAM pOemDMParam
        // The input & output DEVMODEs, including pointers to the public &
        // private DEVMODE data.
    )
{
    return hrOEMDevMode(dwMode, pOemDMParam);
}

//+---------------------------------------------------------------------------
//
//  Member:
//      COemUI2::DocumentPropertySheets
//
//  Synopsis:
//      Show the GPD version in the debugger whenever the
//      printing preferences UI is shown.
//
//
//----------------------------------------------------------------------------
HRESULT __stdcall
COemUI2::DocumentPropertySheets(
    PPROPSHEETUI_INFO   pPSUIInfo,
    LPARAM              lParam
    )
{
    UNREFERENCED_PARAMETER(pPSUIInfo);
    UNREFERENCED_PARAMETER(lParam);

    this->DbgShowGPDVersion();

    return S_OK;
}

//+---------------------------------------------------------------------------
//
//  Member:
//      COemUI2::DbgShowGPDVersion
//
//  Synopsis:
//      Display the GPD version in the debugger using a GDL snapshot & MSXML 6
//      To make this work on versions of Windows prior to Windows Vista, change
//      references to MSXML 6 with MSXML 3.
//
//
//----------------------------------------------------------------------------
HRESULT
COemUI2::DbgShowGPDVersion()
{
    HRESULT hr;
    IXMLDOMDocument2 *pDOMSnapshot = NULL;
    IXMLDOMNode *pVersionNode = NULL;

    //
    // Get a DOM representation of the snapshot using MSXML 3
    //
    hr = LoadGDLSnapshot(&pDOMSnapshot);

    if (hr == REGDB_E_CLASSNOTREG)
    {
        DebugPrint(L"COemUI2::PublishHelperInterface: MSXML 6.0 not registered, GDL version lookup disabled\r\n");
    }

    //
    // Find the GDL node that contains the GPDFileVersion.  This should return a single XML DOM Element.
    //
    if (SUCCEEDED(hr))
    {
        hr = pDOMSnapshot->selectSingleNode((BSTR)TEXT("//gdl:SnapshotRoot/gdl:GDL_ATTRIBUTE[@Name=\"*GPDFileVersion\"]"), &pVersionNode);
    }

    //
    // If the node was found, write the version information to the debugger.
    // It's content is represented as CDATA in the XML, but MSXML will
    // normalize that to a regular string when it parses the XML.
    //
    if (SUCCEEDED(hr) && pVersionNode)
    {
        VARIANT varVersionString;

        //
        // Get DefaultOption name
        //
        hr = pVersionNode->get_nodeTypedValue(&varVersionString);

        if (SUCCEEDED(hr))
        {
            DebugPrint(L"COemUI2::PublishHelperInterface: GDL Version: %s\r\n", varVersionString.bstrVal);
        }

        VariantClear(&varVersionString);
    }

    if (pDOMSnapshot)
    {
        pDOMSnapshot->Release();
        pDOMSnapshot = NULL;
    }

    return hr;
}


//+---------------------------------------------------------------------------
//
//  Member:
//      COemUI2::LoadGDLSnapshot
//
//  Synopsis:
//      Load the GDL snapshot from IPrintCoreHelperUni into a DOM object,
//      and set appropriate properties on the DOM to prepare it for use
//      with xpath queries.
//
//
//----------------------------------------------------------------------------
HRESULT
COemUI2::LoadGDLSnapshot(
    _Out_ IXMLDOMDocument2 **ppDOMSnapshot
    )
{
    HRESULT hr = S_OK;
    IStream *pSnapshotStream = NULL;
    VARIANT_BOOL loadResult = 0;

    if (!ppDOMSnapshot)
    {
        return E_INVALIDARG;
    }

    (*ppDOMSnapshot) = NULL;

    hr = m_pCoreHelper->CreateDefaultGDLSnapshot(0, &pSnapshotStream);

    if (SUCCEEDED(hr))
    {
        //
        // Create DOM document
        //
        hr = m_pCoreHelper->CreateInstanceOfMSXMLObject(CLSID_DOMDocument60,
                                                        NULL,
                                                        CLSCTX_INPROC_SERVER,
                                                        IID_IXMLDOMDocument2,
                                                        (LPVOID *)ppDOMSnapshot);
    }

    if (SUCCEEDED(hr))
    {
        VARIANT streamVar;

        VariantInit(&streamVar);
        V_VT(&streamVar) = VT_UNKNOWN;
        V_UNKNOWN(&streamVar) = pSnapshotStream;
        pSnapshotStream->AddRef();

        //
        // Load the snapshot into the DOM document
        //
        (*ppDOMSnapshot)->put_async(VARIANT_FALSE);
        (*ppDOMSnapshot)->put_validateOnParse(VARIANT_FALSE);
        (*ppDOMSnapshot)->put_resolveExternals(VARIANT_FALSE);

        hr = (*ppDOMSnapshot)->load(streamVar, &loadResult);

        VariantClear(&streamVar);
    }

    //
    // If the DOM fails to load, the HRESULT will be S_FALSE, so explicitly
    // look for S_OK.
    //
    if (hr == S_OK)
    {
        //
        // Set the namespaces that should be available for xpath queries later.
        //
        BSTR bstrDefNameSpace = SysAllocString(TEXT("xmlns:gdl=\"http://schemas.microsoft.com/2002/print/gdl/1.0\""));

        if (!bstrDefNameSpace)
        {
            hr = E_OUTOFMEMORY;
        }

        if (SUCCEEDED(hr))
        {
            VARIANT varDefNameSpace;

            VariantInit(&varDefNameSpace);
            varDefNameSpace.vt = VT_BSTR;
            varDefNameSpace.bstrVal = bstrDefNameSpace;

            hr = (*ppDOMSnapshot)->setProperty((BSTR)TEXT("SelectionNamespaces"), varDefNameSpace);
            SysFreeString(bstrDefNameSpace);
        }
    }
    else
    {
        hr = E_FAIL;
    }

    if (FAILED(hr) && *ppDOMSnapshot)
    {
        (*ppDOMSnapshot)->Release();
        (*ppDOMSnapshot) = NULL;
    }

    if (pSnapshotStream)
    {
        pSnapshotStream->Release();
        pSnapshotStream = NULL;
    }

    return hr;
}