summaryrefslogtreecommitdiff
path: root/wia/wiadriverex/errhandler/errhandler.cpp
blob: f205b15b8e3a21fa93786e05b6afff08167c51b3 (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
/*****************************************************************************
 *
 *  errhandler.cpp
 *
 *  Copyright (c) 2003 Microsoft Corporation.  All Rights Reserved.
 *
 *  DESCRIPTION:
 *
 *  CErrHandler is a simple error handler, which works together with
 *  the wiadriver.
 *
 *******************************************************************************/
#include <DriverSpecs.h>
_Analysis_mode_(_Analysis_code_type_user_driver_)

#include "stdafx.h"

#define MYDESCSTRING TEXT("Special driver device status error (only for testing purposes). Press 'Ok' to continue. Hitting 'Cancel' will abort the transfer.")

#define HANDLED_PRIVATE_STATUS_ERROR_1  MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, 1001)

// {CFC1A4D4-5F27-4881-81E4-1BE314EB22F7}
static const GUID CLSID_WiaErrorHandler =
{ 0xcfc1a4d4, 0x5f27, 0x4881, { 0x81, 0xe4, 0x1b, 0xe3, 0x14, 0xeb, 0x22, 0xf7 } };

static LONG g_cLocks = 0;

void LockModule(void)   { InterlockedIncrement(&g_cLocks); }
void UnlockModule(void) { InterlockedDecrement(&g_cLocks); }

class CErrHandler : public IWiaErrorHandler
{
public:

    STDMETHODIMP
    QueryInterface(const IID& iid_requested, _COM_Outptr_ void** ppInterfaceOut);

    STDMETHODIMP_(ULONG)
    AddRef(void);

    STDMETHODIMP_(ULONG)
    Release(void);

    STDMETHODIMP
    ReportStatus(
              LONG        lFlags,
        _In_  HWND        hwndParent,
        _In_  IWiaItem2   *pWiaItem2,
              HRESULT     hrStatus,
              LONG        lPercentComplete);

    STDMETHODIMP
    GetStatusDescription(
              LONG        lFlags,
        _In_  IWiaItem2   *pWiaItem2,
              HRESULT     hrStatus,
        _Out_ BSTR        *pbstrDescription);

    CErrHandler() : m_nRefCount(0) {}
private:

    LONG        m_nRefCount;
};

///
///  QueryInterface
///
STDMETHODIMP
CErrHandler::QueryInterface(const IID& iid_requested, _COM_Outptr_ void** ppInterfaceOut)
{
    HRESULT hr = S_OK;

    hr = ppInterfaceOut ? S_OK : E_POINTER;

    if (SUCCEEDED(hr))
    {
        *ppInterfaceOut = NULL;
    }

    //
    //  We support IID_IUnknown and IID_IWiaErrorHandler
    //
    if (SUCCEEDED(hr))
    {
        if (IID_IUnknown == iid_requested)
        {
            *ppInterfaceOut = static_cast<IUnknown*>(this);
        }
        else if (IID_IWiaErrorHandler == iid_requested)
        {
            *ppInterfaceOut = static_cast<IWiaErrorHandler*>(this);
        }
        else
        {
            hr = E_NOINTERFACE;
        }
    }

    if (SUCCEEDED(hr))
    {
        reinterpret_cast<IUnknown*>(*ppInterfaceOut)->AddRef();
    }

    return hr;
}

///
///  AddRef
///
STDMETHODIMP_(ULONG)
CErrHandler::AddRef(void)
{
    if (m_nRefCount == 0)
    {
        LockModule();
    }

    return InterlockedIncrement(&m_nRefCount);
}

///
///  Release
///
STDMETHODIMP_(ULONG)
CErrHandler::Release(void)
{
    ULONG nRetval = InterlockedDecrement(&m_nRefCount);

    if (0 == nRetval)
    {
        delete this;
        UnlockModule();
    }

    return nRetval;
}

/*****************************************************************************
 *
 *  @doc INTERNAL
 *
 *  @func STDMETHODIMP | CErrHandler::ReportStatus | ReportStatus implementation
 *
 *  @parm   LONG | lFlags |
 *          Flags - currently unused.
 *
 *  @parm   HWND | hwndParent |
 *          Window handle provided by the application
 *
 *  @parm   IWiaItem2 | pWiaItem2 |
 *          The item which is currently being transferred
 *
 *  @parm   HRESULT | hrStatus |
 *          Status code
 *
 *  @parm   LONG    | lPercentComplete
 *          Percent of operation completed (e.g. warming up device)
 *
 *
 *  @comm
 *  ReportStatus handles HANDLED_PRIVATE_STATUS_ERROR_1 for which it displays a modal
 *  dialog box which enables a user to cancel the transfer or to continue.
 *  For all other messages we return WIA_STATUS_NOT_HANDLED
 *
 *  @rvalue S_OK    |
 *              The function successfully handled the device status message.
 *
 *  @rvalue WIA_STATUS_NOT_HANDLED |
 *              The function does not handle this device status message
 *
 *  @rvalue E_XXX   |
 *              Error
 *
 *****************************************************************************/
STDMETHODIMP
CErrHandler::ReportStatus(
          LONG        lFlags,
    _In_  HWND        hwndParent,
    _In_  IWiaItem2   *pWiaItem2,
          HRESULT     hrStatus,
          LONG        lPercentComplete)
{
    UNREFERENCED_PARAMETER(lFlags);
    UNREFERENCED_PARAMETER(lPercentComplete);

    HRESULT hr = pWiaItem2 ? WIA_STATUS_NOT_HANDLED : E_INVALIDARG;

    if ((WIA_STATUS_NOT_HANDLED == hr) && (HANDLED_PRIVATE_STATUS_ERROR_1 == hrStatus))
    {
        HINSTANCE hModule         = NULL;
        TCHAR bufDialog[MAX_PATH] = {0};
        TCHAR bufTitle[MAX_PATH]  = {0};

        hModule = GetModuleHandle(L"errhandler.dll");

        if (NULL != hModule &&
            LoadString(hModule, IDS_MESSAGEBOX_ERRORHANDLE_DIALOG, bufDialog, ARRAYSIZE(bufDialog)) &&
            LoadString(hModule, IDS_MESSAGEBOX_ERRORHANDLE_TITLE, bufTitle, ARRAYSIZE(bufTitle)) &&
            IDOK == MessageBox(hwndParent, bufDialog, bufTitle, MB_OKCANCEL|MB_TASKMODAL|MB_ICONERROR)
            )
        {
            hr = S_OK;
        }
        else
        {
            hr = HANDLED_PRIVATE_STATUS_ERROR_1;
        }
    }

    return hr;
}

/*****************************************************************************
 *
 *  @doc INTERNAL
 *
 *  @func STDMETHODIMP | CErrHandler::GetStatusDescription | GetStatusDescription implementation
 *
 *  @parm   LONG | lFlags |
 *          Flags - currently unused.
 *
 *  @parm   IWiaItem2 | pWiaItem2 |
 *          The item which is currently being transferred
 *
 *  @parm   HRESULT | hrStatus |
 *          Status code
 *
 *  @parm   BSTR* | pbstrDescription |
 *          On S_OK this pbstrDescription will point to string with status description
 *
 *
 *  @comm
 *  GetStatusDescription handles HANDLED_PRIVATE_STATUS_ERROR_1 for which it returns
 *  a description string. It returns WIA_STATUS_NOT_HANDLED for all other messages
 *
 *  @rvalue S_OK    |
 *              The function successfully handled the device status message.
 *
 *  @rvalue WIA_STATUS_NOT_HANDLED |
 *              The function does not handle this device status message
 *
 *  @rvalue E_XXX   |
 *              Error
 *
 *****************************************************************************/
STDMETHODIMP
CErrHandler::GetStatusDescription(
          LONG        lFlags,
    _In_  IWiaItem2   *pWiaItem2,
          HRESULT     hrStatus,
    _Out_ BSTR        *pbstrDescription)
{
    UNREFERENCED_PARAMETER(lFlags);

    HRESULT hr = (pbstrDescription && pWiaItem2) ? WIA_STATUS_NOT_HANDLED : E_INVALIDARG;

    if (WIA_STATUS_NOT_HANDLED == hr)
    {
        BSTR    bstrDescription = NULL;

        bstrDescription = SysAllocString((HANDLED_PRIVATE_STATUS_ERROR_1 == hrStatus) ? MYDESCSTRING : L"");

        if (bstrDescription)
        {
            *pbstrDescription = bstrDescription;
            hr = (HANDLED_PRIVATE_STATUS_ERROR_1 == hrStatus) ? S_OK : WIA_STATUS_NOT_HANDLED;
        }
        else
        {
            hr = E_OUTOFMEMORY;
        }
    }

    return hr;
}


/*****************************************************************************
 *
 *  Class Object
 *
 *******************************************************************************/
class CErrClassObject : public IClassFactory
{
public:

    STDMETHODIMP
    QueryInterface(const IID& iid_requested, void** ppInterfaceOut)
    {
        HRESULT hr = S_OK;

        hr = ppInterfaceOut ? S_OK : E_POINTER;

        if (SUCCEEDED(hr))
        {
            *ppInterfaceOut = NULL;
        }

        //
        //  We only support IID_IUnknown and IID_IClassFactory
        //
        if (SUCCEEDED(hr))
        {
            if (IID_IUnknown == iid_requested)
            {
                *ppInterfaceOut = static_cast<IUnknown*>(this);
            }
            else if (IID_IClassFactory == iid_requested)
            {
                *ppInterfaceOut = static_cast<IClassFactory*>(this);
            }
            else
            {
                hr = E_NOINTERFACE;
            }
        }

        if (SUCCEEDED(hr))
        {
            reinterpret_cast<IUnknown*>(*ppInterfaceOut)->AddRef();
        }

        return hr;
    }

    STDMETHODIMP_(ULONG)
    AddRef(void)
    {
        LockModule();
        return 2;
    }

    STDMETHODIMP_(ULONG)
    Release(void)
    {
        UnlockModule();
        return 1;
    }

    STDMETHODIMP
    CreateInstance(_In_opt_     IUnknown *pUnkOuter,
                   _In_         REFIID   riid,
                   _COM_Outptr_ void     **ppv)
    {
        CErrHandler  *pErrHandler = NULL;
        HRESULT      hr;

        hr = ppv ? S_OK : E_POINTER;

        if (SUCCEEDED(hr))
        {
            *ppv = 0;
        }

        if (SUCCEEDED(hr))
        {
            if (pUnkOuter)
            {
                hr = CLASS_E_NOAGGREGATION;
            }
        }

        if (SUCCEEDED(hr))
        {
#pragma prefast(suppress:__WARNING_ALIASED_MEMORY_LEAK, "pErrHandler is freed on release.")
            pErrHandler = new CErrHandler();

            hr = pErrHandler ? S_OK : E_OUTOFMEMORY;
        }

        if (SUCCEEDED(hr))
        {
            pErrHandler->AddRef();
            hr = pErrHandler->QueryInterface(riid, ppv);
            pErrHandler->Release();
        }

        return hr;
    }

    STDMETHODIMP
    LockServer(BOOL bLock)
    {
        if (bLock)
        {
            LockModule();
        }
        else
        {
            UnlockModule();
        }

        return S_OK;
    }
};

STDAPI DllCanUnloadNow(void)
{
    return (g_cLocks == 0) ? S_OK : S_FALSE;
}

STDAPI DllGetClassObject(_In_           REFCLSID   rclsid,
                        _In_            REFIID     riid,
                        _Outptr_     void       **ppv)
{
    static  CErrClassObject s_FilterClass;

    HRESULT     hr;

    hr = ppv ? S_OK : E_INVALIDARG;

    if (SUCCEEDED(hr))
    {
        if (rclsid == CLSID_WiaErrorHandler)
        {
            hr = s_FilterClass.QueryInterface(riid, ppv);
        }
        else
        {
            *ppv = 0;
            hr = CLASS_E_CLASSNOTAVAILABLE;
        }
    }

    return hr;
}

STDAPI DllUnregisterServer()
{
    return S_OK;
}

STDAPI DllRegisterServer()
{
    return S_OK;
}