summaryrefslogtreecommitdiff
path: root/print/SimplePipelineFilter/StreamFilter.cxx
blob: a03aafe0699cf19cc213c1cdc549f2843d12b421 (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
//+--------------------------------------------------------------------------
//
//  Copyright (c) Microsoft Corporation.  All rights reserved.
//
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
//
//  THIS CODE AND INFORMATION ARE 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.
//
//  Abstract:
//     WDK print filter sample.
//     This is the C file for the stream filter sample.
//
//----------------------------------------------------------------------------

#include "precomp.hxx"
#include "main.hxx"
#include "StreamFilter.tmh"
#include "StreamFilter.hxx"

#include "winddiui.h"
#include "compstui.h"
#include "printoem.h"

#include "initguid.h"
#include "prcomoem.h"

_Analysis_mode_(_Analysis_code_type_user_driver_)

//
// 5f5460d2-b313-44ca-82e4-37f83d793999 - generate a guid, do not use this one in your code
//
const GUID StreamFilterGuid = {0x5f5460d2, 0xb313, 0x44ca, {0x82, 0xe4, 0x37, 0xf8, 0x3d, 0x79, 0x39, 0x99}};

const GUID&
StreamFilter::
FilterClsid(
    void
    )
{
    return StreamFilterGuid;
}

StreamFilter::
StreamFilter() :
    m_bShutdown(false),
    m_cRef(1)
{
}

//
// IUnknown methods
//
__override
STDMETHODIMP
StreamFilter::
QueryInterface(
    _In_       REFIID           riid,
    _Out_      void             **ppv
    )
{
    HRESULT hRes = E_POINTER;

    if (ppv)
    {
        hRes = E_NOINTERFACE;

        *ppv = NULL;

        if (riid == IID_IPrintPipelineFilter)
        {
            *ppv = static_cast<IPrintPipelineFilter *>(this);
        }
        else if (riid == IID_IUnknown)
        {
            *ppv = static_cast<IUnknown *>(this);
        }

        if (*ppv)
        {
            AddRef();

            hRes = S_OK;
        }
    }

    return hRes;
}

__override
STDMETHODIMP_(ULONG)
StreamFilter::
AddRef(
    void
    )
{
    return InterlockedIncrement(&m_cRef);
}

__override
STDMETHODIMP_(ULONG)
StreamFilter::
Release(
    void
    )
{
    ULONG cRefCount = InterlockedDecrement(&m_cRef);

    if (cRefCount)
    {
        return cRefCount;
    }

    delete this;

    return 0;
}

//
// IPrintPipelineFilter
//
__override
STDMETHODIMP
StreamFilter::
ShutdownOperation(
    void
    )
{
    m_bShutdown = true;

    return S_OK;
}

__override
STDMETHODIMP
StreamFilter::
InitializeFilter(
    _In_    IInterFilterCommunicator         *pIFilterCommunicator,
    _In_    IPrintPipelinePropertyBag        *pIPropertyBag,
    _In_    IPrintPipelineManagerControl     *pIPipelineControl
    )
{
    HRESULT hr = S_OK;
    VARIANT varHelper;
    PCSTR *pFeatures = NULL;
    DWORD dwFeatures = 0;
    Tools::SmartPtr<IPrintCoreHelper> pHelper;

    VariantInit(&varHelper);

    //
    // StreamAccessSequential, IID_IPrintReadStream
    //
    hr = pIFilterCommunicator->RequestReader(reinterpret_cast<void **>(&m_pIRead));

    if (SUCCEEDED(hr))
    {
        //
        // StreamAccessSequential, StreamModify, IID_IPrintWriteStream
        //
        hr = pIFilterCommunicator->RequestWriter(reinterpret_cast<void **>(&m_pIWrite));
    }

    if (SUCCEEDED(hr))
    {
        m_pIPipelineControl = pIPipelineControl;
    }

    //
    // This shows how to use the helper interface to read information
    // from a UnidrvUI based configuration module.
    //

    if (SUCCEEDED(hr))
    {
        hr = pIPropertyBag->GetProperty(L"IPrintCoreHelper", &varHelper);
    }

    if (SUCCEEDED(hr) && V_UNKNOWN(&varHelper))
    {
        hr = (V_UNKNOWN(&varHelper))->QueryInterface(IID_IPrintCoreHelper, (VOID**)(&pHelper));
    }

    //
    // No need to release buffers provided by the helper.  Their
    // lifetime matches that of the helper.
    //
    if (SUCCEEDED(hr) && pHelper)
    {
        hr = pHelper->EnumFeatures(&pFeatures, &dwFeatures);
    }

    if (SUCCEEDED(hr) && dwFeatures > 0)
    {
        for (DWORD i = 0; i < dwFeatures; i++)
        {
            DoTraceMessage(WS_TRACE, L"StreamFilter::InitializeFilter: Feature found: %s", pFeatures[i]);
        }
    }

    VariantClear(&varHelper);

    return hr;
}

__override
STDMETHODIMP
StreamFilter::
StartOperation(
    void
    )
{
    HRESULT                         hr = S_OK;
    Tools::SmartPtr<IImgErrorInfo>  pIErrorInfo;
    DWORD                           cbRead;
    BYTE                            *pReadBuf;
    BOOL                            bEof = FALSE;

    pReadBuf = new BYTE[kBufferSize];

    if (!pReadBuf)
    {
        hr = E_OUTOFMEMORY;
    }

    while (SUCCEEDED(hr) && !bEof && !m_bShutdown)
    {
        hr = m_pIRead->ReadBytes(pReadBuf, kBufferSize, &cbRead, &bEof);

        if (SUCCEEDED(hr) && cbRead)
        {
            ULONG   cbWritten;

            hr = m_pIWrite->WriteBytes(pReadBuf, cbRead, &cbWritten);

            if (SUCCEEDED(hr))
            {
                DoTraceMessage(WS_TRACE, "StreamFilter::StartOperation read and wrote %u bytes", cbRead);
            }
        }
    }

    m_pIWrite->Close();

    if (FAILED(hr))
    {
        m_pIPipelineControl->RequestShutdown(hr, pIErrorInfo);
    }

    if (m_pIPipelineControl)
    {
        m_pIPipelineControl->FilterFinished();
    }

    delete [] pReadBuf;

    return hr;
}