summaryrefslogtreecommitdiff
path: root/wia/ProdScan/CapMan.cpp
blob: 9758ad67b5e195c69c7609960a8ac401f2582cc6 (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
/**************************************************************************
*
*  Copyright � Microsoft Corporation
*
*  File Title:  CapMan.cpp
*
*  Project:     Production Scanner Driver Sample
*
*  Description: Contains the implementation for the CWIACapabilityManager
*               class, a helper class for managing WIA capabilities
*
***************************************************************************/

#include "stdafx.h"

/**************************************************************************\
*
* CWIACapabilityManager constructor
*
\**************************************************************************/

CWIACapabilityManager::CWIACapabilityManager()
{
    m_ulEvents = 0;
    m_ulCommands = 0;
}

/**************************************************************************\
*
* CWIACapabilityManager destructor
*
\**************************************************************************/

CWIACapabilityManager::~CWIACapabilityManager()
{
    Destroy();
}

/**************************************************************************\
*
* Parameters:
*
*    hInstance - module instance handle used for driver resources
*
* Return Value:
*
*    S_OK or a standard COM error code
*
\**************************************************************************/

HRESULT
CWIACapabilityManager::Initialize(
    _In_ HINSTANCE hInstance)
{
    HRESULT hr = S_OK;

    if (!hInstance)
    {
        hr = E_INVALIDARG;
        WIAEX_ERROR((g_hInst, "Invalid parameter, hr = 0x%08X", hr));
    }

    if (SUCCEEDED(hr))
    {
        m_hInstance = hInstance;
    }

    return hr;
}

/**************************************************************************\
*
* Parameters:
*
*    None
*
* Return Value:
*
*    None
*
\**************************************************************************/

void
CWIACapabilityManager::Destroy()
{
    INT nCapabilities = m_CapabilityArray.Size();

    for (INT i = 0; i < nCapabilities; i++)
    {
        FreeCapability(&m_CapabilityArray[i], TRUE);
    }
    m_CapabilityArray.Destroy();

    m_ulEvents = 0;
    m_ulCommands = 0;
}

/**************************************************************************\
*
* Parameters:
*
*   guidCapability          - capability identifier
*   uiNameResourceID        - capability name
*   uiDescriptionResourceID - capability description
*   ulFlags                 - capability flags
*   wszIcon                 - capability icon
*
* Return Value:
*
*    S_OK or a standard COM error code
*
\**************************************************************************/

HRESULT
CWIACapabilityManager::AddCapability(
         const GUID guidCapability,
         UINT       uiNameResourceID,
         UINT       uiDescriptionResourceID,
         ULONG      ulFlags,
    _In_ LPCWSTR    wszIcon)
{
    HRESULT hr = S_OK;
    WIA_DEV_CAP_DRV *pWIADeviceCapability = NULL;
    WCHAR wCapabilityString[MAX_PATH] = {};

    if (!wszIcon)
    {
        hr = E_INVALIDARG;
        WIAEX_ERROR((g_hInst, "Invalid parameter, hr = 0x%08X", hr));
    }

    //
    // Allocate memory for the new capability structure:
    //
    if (SUCCEEDED(hr))
    {
        hr = AllocateCapability(&pWIADeviceCapability);
        if (SUCCEEDED(hr) && (!pWIADeviceCapability))
        {
            hr = E_POINTER;
            WIAEX_ERROR((g_hInst, "AllocateCapability failed to return a valid pointer, hr = 0x%08X", hr));
        }
        if (FAILED(hr))
        {
            WIAEX_ERROR((g_hInst, "Cannot allocate memory for capability, hr = 0x%08X", hr));
        }
    }

    //
    // Copy the capability flags and GUID identifier:
    //
    if (SUCCEEDED(hr))
    {
        pWIADeviceCapability->ulFlags = ulFlags;
        *pWIADeviceCapability->guid = guidCapability;
    }

    //
    // Load and copy the capability name from resources:
    //
    if (SUCCEEDED(hr))
    {
        if (LoadString(m_hInstance, uiNameResourceID, wCapabilityString, ARRAYSIZE(wCapabilityString)))
        {
            hr = StringCbCopyW(pWIADeviceCapability->wszName, MAX_CAPABILITY_STRING_SIZE_BYTES, wCapabilityString);
            if(FAILED(hr))
            {
                WIAEX_ERROR((g_hInst, "Failed to copy source string (%ws) to destination string, hr = 0x%08X", wCapabilityString, hr));
            }
        }
        else
        {
            hr = E_FAIL;
            WIAEX_ERROR((g_hInst, "Failed to load the device capability name string %u from resources, hr = 0x%08X", uiNameResourceID, hr));
        }
    }

    //
    // Load and copy the capability description from resources:
    //
    if (SUCCEEDED(hr))
    {
        memset(wCapabilityString, 0, sizeof(wCapabilityString));
        if (LoadString(m_hInstance, uiDescriptionResourceID, wCapabilityString, ARRAYSIZE(wCapabilityString)))
        {
            hr = StringCbCopyW(pWIADeviceCapability->wszDescription, MAX_CAPABILITY_STRING_SIZE_BYTES, wCapabilityString);
            if(FAILED(hr))
            {
                WIAEX_ERROR((g_hInst, "Failed to copy source string (%ws) to destination string, hr = 0x%08X", wCapabilityString, hr));
            }
        }
        else
        {
            hr = E_FAIL;
            WIAEX_ERROR((g_hInst, "Failed to load the device capability description string %u from DLL resource, hr = 0x%08X",
                uiDescriptionResourceID, hr));
        }
    }

    //
    // Copy the icon location:
    //
    if (SUCCEEDED(hr))
    {
        memset(pWIADeviceCapability->wszIcon, 0, MAX_CAPABILITY_STRING_SIZE_BYTES);

        hr = StringCbCopyW(pWIADeviceCapability->wszIcon, MAX_CAPABILITY_STRING_SIZE_BYTES, wszIcon);
        if(FAILED(hr))
        {
            WIAEX_ERROR((g_hInst, "Failed to copy source string (%ws) to destination string, hr = 0x%08X", wszIcon, hr));
        }
    }

    //
    // Add the new capability item to the array:
    //
    if(SUCCEEDED(hr))
    {
        if ((WIA_NOTIFICATION_EVENT & pWIADeviceCapability->ulFlags) || (WIA_ACTION_EVENT & pWIADeviceCapability->ulFlags))
        {
            //
            // Event capabilities are inserted at the beginning of the array:
            //
            m_CapabilityArray.Insert(*pWIADeviceCapability, 0);

            m_ulEvents += 1;
        }
        else
        {
            //
            // Command capabilities are appended at the end of the array:
            //
            m_CapabilityArray.Append(*pWIADeviceCapability);

            m_ulCommands += 1;
        }

        if ((m_ulEvents + m_ulCommands) != (ULONG)m_CapabilityArray.Size())
        {
            WIAEX_ERROR((g_hInst, "Counted %u capabilities (%u events, %u commands), recorded %u..",
                m_ulEvents + m_ulCommands, m_ulEvents, m_ulCommands, m_CapabilityArray.Size()));
        }
    }

    //
    // Clean up:
    //

    if (pWIADeviceCapability)
    {
        CoTaskMemFree(pWIADeviceCapability);
        pWIADeviceCapability = NULL;
    }

    return hr;
}

/**************************************************************************\
*
* Parameters:
*
*   ppWIADeviceCapability - pointer to capability object to allocate
*
* Return Value:
*
*    S_OK or a standard COM error code
*
\**************************************************************************/

HRESULT
CWIACapabilityManager::AllocateCapability(
    _Out_ WIA_DEV_CAP_DRV **ppWIADeviceCapability)
{
    HRESULT hr = S_OK;
    WIA_DEV_CAP_DRV *pWIADeviceCapability = NULL;

    if (!ppWIADeviceCapability)
    {
        hr = E_INVALIDARG;
        WIAEX_ERROR((g_hInst, "Invalid parameter, hr = 0x%08X", hr));
    }

    if (SUCCEEDED(hr))
    {
        *ppWIADeviceCapability = NULL;

#pragma prefast(suppress:__WARNING_MEMORY_LEAK, "pWIADeviceCapability is freed with FreeCapability when the call fails")
        pWIADeviceCapability = (WIA_DEV_CAP_DRV*)CoTaskMemAlloc(sizeof(WIA_DEV_CAP_DRV));
        if (!pWIADeviceCapability)
        {
            hr = E_OUTOFMEMORY;
            WIAEX_ERROR((g_hInst, "Failed to allocate memory for WIA_DEV_CAP_DRV structure, hr = 0x%08X", hr));
        }
        else
        {
            //
            // Successfully created WIA_DEV_CAP_DRV, now initialize to 0.
            //
            memset(pWIADeviceCapability, 0, sizeof(WIA_DEV_CAP_DRV));
        }
    }

    if (SUCCEEDED(hr))
    {
#pragma prefast(suppress:__WARNING_MEMORY_LEAK, "Freed with FreeCapability when the call fails")
        pWIADeviceCapability->guid = (GUID*)CoTaskMemAlloc(sizeof(GUID));
        if (!pWIADeviceCapability->guid)
        {
            hr = E_OUTOFMEMORY;
            WIAEX_ERROR((g_hInst, "Failed to allocate memory for GUID member of WIA_DEV_CAP_DRV structure, hr = 0x%08X", hr));
        }
    }

    if (SUCCEEDED(hr))
    {
#pragma prefast(suppress:__WARNING_MEMORY_LEAK, "Freed with FreeCapability when the call fails")
        pWIADeviceCapability->wszName = (LPOLESTR)CoTaskMemAlloc(MAX_CAPABILITY_STRING_SIZE_BYTES);
        if (!pWIADeviceCapability->wszName)
        {
            hr = E_OUTOFMEMORY;
            WIAEX_ERROR((g_hInst, "Failed to allocate memory for LPOLESTR (wszName) member of WIA_DEV_CAP_DRV structure, hr = 0x%08X", hr));
        }
    }

    if (SUCCEEDED(hr))
    {
#pragma prefast(suppress:__WARNING_MEMORY_LEAK, "Freed with FreeCapability when the call fails")
        pWIADeviceCapability->wszDescription = (LPOLESTR)CoTaskMemAlloc(MAX_CAPABILITY_STRING_SIZE_BYTES);
        if (!pWIADeviceCapability->wszDescription)
        {
            hr = E_OUTOFMEMORY;
            WIAEX_ERROR((g_hInst, "Failed to allocate memory for LPOLESTR (wszDescription) member of WIA_DEV_CAP_DRV structure, hr = 0x%08X", hr));
        }
    }

    if (SUCCEEDED(hr))
    {
#pragma prefast(suppress:__WARNING_MEMORY_LEAK, "Freed with FreeCapability when the call fails")
        pWIADeviceCapability->wszIcon = (LPOLESTR)CoTaskMemAlloc(MAX_CAPABILITY_STRING_SIZE_BYTES);
        if (!pWIADeviceCapability->wszIcon)
        {
            hr = E_OUTOFMEMORY;
            WIAEX_ERROR((g_hInst, "Failed to allocate memory for LPOLESTR (wszIcon) member of WIA_DEV_CAP_DRV structure, hr = 0x%08X", hr));
        }
    }

    if (SUCCEEDED(hr))
    {
        *pWIADeviceCapability->guid = GUID_NULL;
        memset(pWIADeviceCapability->wszName, 0, MAX_CAPABILITY_STRING_SIZE_BYTES);
        memset(pWIADeviceCapability->wszDescription, 0, MAX_CAPABILITY_STRING_SIZE_BYTES);
        memset(pWIADeviceCapability->wszIcon, 0, MAX_CAPABILITY_STRING_SIZE_BYTES);

        *ppWIADeviceCapability = pWIADeviceCapability;
    }
    else if (pWIADeviceCapability)
    {
        FreeCapability(pWIADeviceCapability);
        pWIADeviceCapability = NULL;
    }

    return hr;
}

/**************************************************************************\
*
* Parameters:
*
*   pWIADeviceCapability       - pointer to capability object to free
*   bFreeCapabilityContentOnly - TRUE to free only the capability content
*                                or FALSE to free the entire object instance
*
* Return Value:
*
*    S_OK or a standard COM error code
*
\**************************************************************************/

void
CWIACapabilityManager::FreeCapability(
    _In_ WIA_DEV_CAP_DRV *pWIADeviceCapability,
         BOOL             bFreeCapabilityContentOnly)
{
    if (pWIADeviceCapability)
    {
        if (pWIADeviceCapability->guid)
        {
            CoTaskMemFree(pWIADeviceCapability->guid);
            pWIADeviceCapability->guid = NULL;
        }

        if (pWIADeviceCapability->wszName)
        {
            CoTaskMemFree(pWIADeviceCapability->wszName);
            pWIADeviceCapability->wszName = NULL;
        }

        if (pWIADeviceCapability->wszDescription)
        {
            CoTaskMemFree(pWIADeviceCapability->wszDescription);
            pWIADeviceCapability->wszDescription = NULL;
        }

        if (pWIADeviceCapability->wszIcon)
        {
            CoTaskMemFree(pWIADeviceCapability->wszIcon);
            pWIADeviceCapability->wszIcon = NULL;
        }

        if (!bFreeCapabilityContentOnly)
        {
            CoTaskMemFree(pWIADeviceCapability);
        }
    }
    else
    {
        WIAEX_ERROR((g_hInst, "Invalid parameter, caller attempted to free a NULL WIA_DEV_CAP_DRV structure"));
    }
}

/**************************************************************************\
*
* Parameters:
*
*   None
*
* Return Value:
*
*   Pointer to the capability manager's list of capabilities
*
\**************************************************************************/

WIA_DEV_CAP_DRV*
CWIACapabilityManager::GetCapabilities()
{
    WIA_DEV_CAP_DRV* pCapabilities = NULL;

    if ((!m_ulEvents) && (!m_ulCommands))
    {
        WIAEX_ERROR((g_hInst, "No capabilities to return"));
        pCapabilities = NULL;
    }
    else
    {
        pCapabilities = &m_CapabilityArray[0];
    }

    return pCapabilities;
}

/**************************************************************************\
*
* Parameters:
*
*   None
*
* Return Value:
*
*   Pointer to the capability manager's list of command capabilities
*
\**************************************************************************/

WIA_DEV_CAP_DRV*
CWIACapabilityManager::GetCommands()
{
    WIA_DEV_CAP_DRV* pCommands = NULL;

    if (!m_ulCommands)
    {
        WIAEX_ERROR((g_hInst, "No commands to return"));
        pCommands = NULL;
    }
    else
    {
        //
        // Command capabilities are stored at the end of the capability list, after the event capabilities:
        //
        pCommands =  &m_CapabilityArray[m_ulEvents];
    }

    return pCommands;
}

/**************************************************************************\
*
* Parameters:
*
*   None
*
* Return Value:
*
*   Pointer to the capability manager's list of event capabilities
*
\**************************************************************************/

WIA_DEV_CAP_DRV*
CWIACapabilityManager::GetEvents()
{
    WIA_DEV_CAP_DRV* pEvents = NULL;

    if (!m_ulEvents)
    {
        WIAEX_ERROR((g_hInst, "No events to return"));
        pEvents = NULL;
    }
    else
    {
        //
        // Event capabilities are stored at the beginning of the capability list:
        //
        pEvents = &m_CapabilityArray[0];
    }

    return pEvents;
}