summaryrefslogtreecommitdiff
path: root/wia/ProdScan/WiaUtil.cpp
blob: 7378ae0a3d7439f65d0b57fd31e86c4d3f7a1b64 (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
/**************************************************************************
*
*  Copyright � Microsoft Corporation
*
*  File Title:  WiaUtil.cpp
*
*  Project:     Production Scanner Driver Sample
*
*  Description: This file contains implementation of helper functions
*               declared in wiautil.h
*
***************************************************************************/

#include "stdafx.h"

/**************************************************************************\
*
* This function creates a full WIA item name from a given WIA item name.
* The new full item name is created by concatenating the WIA item name
* with the parent's full item name.
*
* (e.g. 0000\Root + Flatbed = 0000\Root\Flatbed)
*
*
* Parameters:
*
*  pParent           - IWiaDrvItem interface of the parent WIA driver item
*  bstrItemName      - Name of the WIA item
*  pbstrFullItemName - Returned full item name.
*
* Return Value:
*
*    S_OK or a standard COM error code
*
\**************************************************************************/

HRESULT MakeFullItemName(
    _In_ IWiaDrvItem* pParent,
    _In_ BSTR bstrItemName,
    _Out_ BSTR* pbstrFullItemName)
{
    HRESULT hr = S_OK;

    if (pParent && bstrItemName && pbstrFullItemName)
    {
        BSTR bstrParentFullItemName = NULL;
        hr = pParent->GetFullItemName(&bstrParentFullItemName);
        if (SUCCEEDED(hr))
        {
            WCHAR wFullItemName[MAX_PATH * 2] = {};

            hr = StringCbPrintf(wFullItemName, sizeof(wFullItemName), TEXT("%ws\\%ws"), bstrParentFullItemName, bstrItemName);
            if (SUCCEEDED(hr))
            {
                *pbstrFullItemName = SysAllocString(wFullItemName);
                if (*pbstrFullItemName)
                {
                    hr = S_OK;
                }
                else
                {
                    hr = E_OUTOFMEMORY;
                    WIAEX_ERROR((g_hInst, "Failed to allocate memory for BSTR full item name, hr = 0x%08X", hr));
                }
            }
            else
            {
                WIAEX_ERROR((g_hInst, "Failed to allocate memory for BSTR full item name, hr = 0x%08X", hr));
            }

            SysFreeString(bstrParentFullItemName);
            bstrParentFullItemName = NULL;
        }
        else
        {
            WIAEX_ERROR((g_hInst, "Failed to produce the full item name, hr = 0x%08X", hr));
        }
    }
    else
    {
        hr = E_INVALIDARG;
        WIAEX_ERROR((g_hInst, "Invalid parameter, hr = 0x%08X", hr));
    }
    return hr;
}

/**************************************************************************\
*
* This function creates a WIA child item
*
* Parameters:
*
*    wszItemName      - Item name
*    pIWiaMiniDrv     - WIA minidriver interface
*    pParent          - Parent's WIA driver item interface
*    lItemFlags       - Item flags
*    guidItemCategory - Item category
*    ppChild          - Pointer to the newly created child item
*
* Return Value:
*
*    S_OK or a standard COM error code
*
\**************************************************************************/

HRESULT CreateWIAChildItem(
    _In_ LPOLESTR pszItemName,
    _In_ IWiaMiniDrv *pIWiaMiniDrv,
    _In_ IWiaDrvItem *pParent,
    LONG lItemFlags,
    GUID guidItemCategory,
    _Inout_opt_ IWiaDrvItem **ppChild)
{
    UNREFERENCED_PARAMETER(guidItemCategory);

    HRESULT hr = E_INVALIDARG;

    if (pszItemName && pIWiaMiniDrv && pParent)
    {
        BSTR bstrItemName = SysAllocString(pszItemName);
        BSTR bstrFullItemName = NULL;
        IWiaDrvItem *pIWiaDrvItem = NULL;

        if (bstrItemName)
        {
            hr = MakeFullItemName(pParent, bstrItemName, &bstrFullItemName);
            if (SUCCEEDED(hr))
            {
                WIA_DRIVER_ITEM_CONTEXT *pWiaDriverItemContext = NULL;
                hr = wiasCreateDrvItem(lItemFlags,
                    bstrItemName,
                    bstrFullItemName,
                    pIWiaMiniDrv,
                    sizeof(WIA_DRIVER_ITEM_CONTEXT),
                    (BYTE **)&pWiaDriverItemContext,
                    &pIWiaDrvItem);

                if (SUCCEEDED(hr))
                {
                    //
                    // Initialize the item context data:
                    //
                    memset(pWiaDriverItemContext, 0, sizeof(WIA_DRIVER_ITEM_CONTEXT));
                    pWiaDriverItemContext->m_pUploadedImage = NULL;

                    hr = pIWiaDrvItem->AddItemToFolder(pParent);
                    if (FAILED(hr))
                    {
                        WIAEX_ERROR((g_hInst, "Failed to add the new WIA item (%ws) to the specified parent item, hr = 0x%08X",
                            bstrFullItemName, hr));
                        pIWiaDrvItem->Release();
                        pIWiaDrvItem = NULL;
                    }

                    if (SUCCEEDED(hr))
                    {
                        //
                        // If a child iterface pointer parameter was specified, then the caller
                        // expects to have the newly created child interface pointer returned to
                        // them (do not release the newly created item in this case).
                        //

                        if (ppChild)
                        {
                            *ppChild = pIWiaDrvItem;
                            pIWiaDrvItem = NULL;
                        }
                        else if (pIWiaDrvItem)
                        {
                            //
                            // The newly created child has been added to the tree, and is no longer
                            // needed.  Release it.
                            //

                            pIWiaDrvItem->Release();
                            pIWiaDrvItem = NULL;
                        }
                    }
                }
                else
                {
                    WIAEX_ERROR((g_hInst, "Failed to create the new WIA driver item, hr = 0x%08X", hr));
                }

                SysFreeString(bstrItemName);
                bstrItemName = NULL;
                SysFreeString(bstrFullItemName);
                bstrFullItemName = NULL;
            }
            else
            {
                WIAEX_ERROR((g_hInst, "Failed to create the new WIA item's full item name, hr = 0x%08X", hr));
            }
        }
        else
        {
            //
            // Failed to allocate memory for bstrItemName.
            //
            hr = E_OUTOFMEMORY;
            WIAEX_ERROR((g_hInst, "Failed to allocate memory for BSTR storage item name"));
        }

    }
    else
    {
        WIAEX_ERROR((g_hInst, "Invalid parameter"));
    }
    return hr;
}

/**************************************************************************\
*
* This function returns the WIA driver item context data stored with the
* driver item. The context is initialized and stored at WIA item creation.
* See CreateWIAChildItem function.
*
* Parameters:
*
*     pWiasContext           - Pointer to the WIA item context
*     ppWiaDriverItemContext - Pointer to the WIA driver item context data
*
* Return Value:
*
*    S_OK or a standard COM error code
*
\**************************************************************************/

HRESULT wiasGetDriverItemPrivateContext(
    _In_ BYTE* pWiasContext,
    _Out_ BYTE** ppWiaDriverItemContext)
{
    HRESULT hr = E_INVALIDARG;

    if (pWiasContext && ppWiaDriverItemContext)
    {
        IWiaDrvItem *pIWiaDrvItem = NULL;

        hr = wiasGetDrvItem(pWiasContext, &pIWiaDrvItem);

        if (SUCCEEDED(hr))
        {
            hr = pIWiaDrvItem->GetDeviceSpecContext(ppWiaDriverItemContext);

            //
            // The caller will handle the failure case.  A failure, may mean that
            // the the WIA item does not have a private device specific context
            // stored.  This is OK, because is is not required.
            //
        }
        else
        {
            WIAEX_ERROR((g_hInst, "Failed to get the WIA driver item from the application item, hr = 0x%08X", hr));
        }
    }
    else
    {
        WIAEX_ERROR((g_hInst, "Invalid parameter"));
    }
    return hr;
}


/**************************************************************************\
*
* This function allocates a buffer to be used during data transfers.
* FreeTransferBuffer should be called to free the memory allocated by
* this function.
*
* Parameters:
*
*     ppBuffer      - Pointer to the allocated buffer; the caller must
*                     call FreeTransferBuffer() when finished with this buffer
*     pulBufferSize - Size of the buffer allocated
*
* Return Value:
*
*    S_OK or a standard COM error code
*
\**************************************************************************/

HRESULT AllocateTransferBuffer(
    _Outptr_result_bytebuffer_(*pulBufferSize) BYTE** ppBuffer,
    _Out_ ULONG* pulBufferSize)
{
    HRESULT hr = S_OK;

    if (ppBuffer && pulBufferSize)
    {
        //
        // Set the buffer size to DEFAULT_BUFFER_SIZE
        //
        *pulBufferSize  = DEFAULT_BUFFER_SIZE;

        //
        // Allocate the memory
        //
        *ppBuffer = (BYTE*) CoTaskMemAlloc(*pulBufferSize);
        if (*ppBuffer)
        {
            hr = S_OK;
        }
        else
        {
            hr = E_OUTOFMEMORY;
            WIAEX_ERROR((g_hInst, "Failed to allocate memory for transfer buffer, hr = 0x%08X", hr));
        }
    }
    else
    {
        WIAEX_ERROR((g_hInst, "Invalid parameter"));
        hr = E_INVALIDARG;
    }

    return hr;
}

/**************************************************************************\
*
* This function frees any memory allocated using AllocateTransferBuffer()
*
* Parameters:
*
*     pBuffer - Pointer to a buffer allocated with an AllocateTransferBuffer
*               call. If NULL, this call does nothing.
*
* Return Value:
*
*    None
*
\**************************************************************************/

void FreeTransferBuffer(
    _In_opt_ BYTE* pBuffer)
{
    if (pBuffer)
    {
        CoTaskMemFree(pBuffer);
    }
}

/**************************************************************************\
*
* This function queues a WIA event using the passed in WIA item context.
*
* Parameters:
*
*     pWiasContext - Pointer to the WIA item context
*     guidWIAEvent - WIA event to queue
*
* Return Value:
*
*    None
*
\**************************************************************************/

void QueueWIAEvent(
    _In_ BYTE* pWiasContext,
    const GUID& guidWIAEvent)
{
    HRESULT hr = S_OK;
    BSTR bstrDeviceID = NULL;
    BSTR bstrFullItemName = NULL;
    BYTE* pRootItemContext = NULL;

    hr = wiasReadPropStr(pWiasContext, WIA_IPA_FULL_ITEM_NAME, &bstrFullItemName, NULL, TRUE);

    if (SUCCEEDED(hr))
    {
        hr = wiasGetRootItem(pWiasContext, &pRootItemContext);
        if (SUCCEEDED(hr))
        {
            hr = wiasReadPropStr(pRootItemContext, WIA_DIP_DEV_ID, &bstrDeviceID, NULL, TRUE);
            if (SUCCEEDED(hr))
            {
                hr = wiasQueueEvent(bstrDeviceID, &guidWIAEvent, bstrFullItemName);
                if (FAILED(hr))
                {
                    WIAEX_ERROR((g_hInst, "Failed to queue WIA event, hr = 0x%08X", hr));
                }
            }
            else
            {
                WIAEX_ERROR((g_hInst, "Failed to read the WIA_DIP_DEV_ID property, hr = 0x%08X", hr));
            }
        }
        else
        {
            WIAEX_ERROR((g_hInst, "Failed to get the Root item from child item, using wiasGetRootItem, hr = 0x%08X", hr));
        }
    }
    else
    {
        WIAEX_ERROR((g_hInst, "Failed to read WIA_IPA_FULL_ITEM_NAME property, hr = 0x%08X", hr));
    }

    if (bstrFullItemName)
    {
        SysFreeString(bstrFullItemName);
    }

    if (bstrDeviceID)
    {
        SysFreeString(bstrDeviceID);
    }
}