summaryrefslogtreecommitdiff
path: root/print/XpsRasFilter/src/dllentry.cpp
blob: 8e19ae165a873203318b53a9237926ac041850ab (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
// 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 (c) Microsoft Corporation. All rights reserved
//
// File Name:
//
//    dllentry.cpp
//
// Abstract:
//
//    Xps Rasterization Service filter DLL entry points.
//

#include "precomp.h"
#include "WppTrace.h"
#include "Exception.h"
#include "filtertypes.h"
#include "UnknownBase.h"
#include "xpsrasfilter.h"

#include "dllentry.tmh"

namespace xpsrasfilter
{

//
// Class Factory returned by DllGetClassObject()
//
class __declspec( uuid("CFF7BE69-E62D-403b-BE4F-48EC73F5DA1A") ) XPSRasFilterFactory : public UnknownBase<IClassFactory>
{
public:
    XPSRasFilterFactory() :
        m_serverLocks(0)
    {
        ::InterlockedIncrement(&XPSRasFilter::ms_numObjects);
    }

    ~XPSRasFilterFactory()
    {
        ::InterlockedDecrement(&XPSRasFilter::ms_numObjects);
    }

    //
    //Routine Name:
    //
    //    XPSRasFilterFactory::CreateInstance
    //
    //Routine Description:
    //
    //    Returns an instance of XPSRasFilter.
    //
    //Arguments:
    //
    //    pUnkOuter - Outer class (must be NULL)
    //    riid      - Requested interface (IPrintPipelineFilter)
    //    ppvObject - Pointer to the requested interface
    //
    //Return Value:
    //
    //    HRESULT
    //    S_OK      - On success
    //    Otherwise - Failure
    //
    HRESULT
    STDMETHODCALLTYPE
    CreateInstance(
        IUnknown    *pUnkOuter,
        REFIID      riid,
        void        **ppvObject
        )
    {
        HRESULT hr = S_OK;

        if (pUnkOuter != NULL)
        {
            WPP_LOG_ON_FAILED_HRESULT(CLASS_E_NOAGGREGATION);

            return CLASS_E_NOAGGREGATION;
        }

        if (ppvObject == NULL)
        {
            WPP_LOG_ON_FAILED_HRESULT(E_POINTER);

            return E_POINTER;
        }

        *ppvObject = NULL;

        xpsrasfilter::XPSRasFilter *pFilter = NULL;

        //
        // XpsRasFilter::XpsRasFilter() can throw, as can new
        //
        try
        {
            DoTraceMessage(XPSRASFILTER_TRACE_INFO, L"Instantiating filter");
            pFilter = new xpsrasfilter::XPSRasFilter();
        }
        CATCH_VARIOUS(hr)

        if (SUCCEEDED(hr))
        {
            WPP_LOG_ON_FAILED_HRESULT(
                hr = pFilter->QueryInterface(riid, ppvObject)
                );

            pFilter->Release();
        }

        return hr;
    }

    //
    //Routine Name:
    //
    //    XPSRasFilterFactory::LockServer
    //
    //Routine Description:
    //
    //    Allows clients to lock the filter factory in
    //    memory.
    //
    //Arguments:
    //
    //    fLock - TRUE - lock; FALSE - unlock
    //
    //Return Value:
    //
    //    HRESULT
    //    S_OK      - On success
    //
    HRESULT
    STDMETHODCALLTYPE
    LockServer(
        BOOL    fLock
        )
    {
        LONG result;

        if (fLock)  // lock
        {
            result = ::InterlockedIncrement(&m_serverLocks);

            if (result == 1)
            {
                //
                // This was the first 'lock' call; increment the
                // global numObjects
                //
                ::InterlockedIncrement(&XPSRasFilter::ms_numObjects);
            }
        }
        else // unlock
        {
            result = ::InterlockedDecrement(&m_serverLocks);

            if (result == 0)
            {
                //
                // All locks have been unlocked; decrement the
                // global numObjects
                //
                ::InterlockedDecrement(&XPSRasFilter::ms_numObjects);
            }
        }

        return S_OK;
    }

private:
    volatile LONG m_serverLocks;
};

} // namespace xpsrasfilter 

//
//Routine Name:
//
//    DllGetClassObject
//
//Routine Description:
//
//    Returns an instance of XPSRasFilterFactory.
//
//Arguments:
//
//    rclsid  - Requested class (XPSRasFilter)
//    riid    - Requested interface (IClassFactory)
//    ppv     - Pointer to the requested interface
//
//Return Value:
//
//    HRESULT
//    S_OK      - On success
//    Otherwise - Failure
//
STDAPI
DllGetClassObject(
    _In_ REFCLSID        rclsid,
    _In_ REFIID          riid,
    _Outptr_ LPVOID   *ppv
    )
{
    HRESULT hr = S_OK;

    if (ppv == NULL)
    {
        WPP_LOG_ON_FAILED_HRESULT(E_POINTER);

        return E_POINTER;
    }

    *ppv = NULL;

    if (rclsid != __uuidof(xpsrasfilter::XPSRasFilterFactory))
    {
        WPP_LOG_ON_FAILED_HRESULT(CLASS_E_CLASSNOTAVAILABLE);

        return CLASS_E_CLASSNOTAVAILABLE;
    }

    DoTraceMessage(XPSRASFILTER_TRACE_INFO, L"Instantiating class factory");

    xpsrasfilter::XPSRasFilterFactory *pFactory = NULL;
    pFactory = new(std::nothrow) xpsrasfilter::XPSRasFilterFactory();

    if (pFactory == NULL)
    {
        WPP_LOG_ON_FAILED_HRESULT(E_OUTOFMEMORY);

        hr = E_OUTOFMEMORY;
    }

    if (SUCCEEDED(hr))
    {
        WPP_LOG_ON_FAILED_HRESULT(
            hr = pFactory->QueryInterface(riid, ppv)
            );

        pFactory->Release();
    }

    return hr;
}

//
//Routine Name:
//
//    DllCanUnloadNow
//
//Routine Description:
//
//    Checks whether the DLL can be unloaded. That is,
//    whether there are any instances of XPSRasFilter.
//
//Arguments:
//
//    None
//
//Return Value:
//
//    HRESULT
//    S_OK      - Can unload.
//    Otherwise - Cannot unload.
//
STDAPI
DllCanUnloadNow()
{
    return (0 == xpsrasfilter::XPSRasFilter::ms_numObjects) ? S_OK : S_FALSE;
}

//
//Routine Name:
//
//    DllMain
//
//Routine Description:
//
//    Initializes WPP tracing when the DLL is loaded
//    and cleans up WPP tracing when the DLL is unloaded.
//
//Arguments:
//
//    None
//
//Return Value:
//
//    HRESULT
//    S_OK      - On success.
//    Otherwise - Otherwise.
//
extern "C"
BOOL
WINAPI
DllMain(
    _In_     HINSTANCE   hinstDLL,
    _In_     DWORD       fdwReason,
    _In_opt_ LPVOID      /*lpvReserved*/
    )
{
    switch(fdwReason)
    {
    case DLL_PROCESS_ATTACH:

        ::DisableThreadLibraryCalls(hinstDLL);

        WPP_INIT_TRACING(L"XpsRasFilter");
        DoTraceMessage(XPSRASFILTER_TRACE_INFO, L"DLL_PROCESS_ATTACH");

        break;

    case DLL_PROCESS_DETACH:

        DoTraceMessage(XPSRASFILTER_TRACE_INFO, L"DLL_PROCESS_DETACH");
        WPP_CLEANUP();

        break;
    }

    return TRUE;
}