-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD2DPrintJobChecker.cpp
More file actions
338 lines (297 loc) · 9.11 KB
/
Copy pathD2DPrintJobChecker.cpp
File metadata and controls
338 lines (297 loc) · 9.11 KB
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
//// 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
#include "D2DPrintJobChecker.h"
#include <Strsafe.h>
D2DPrintJobChecker::D2DPrintJobChecker() :
m_eventCookie(0),
m_refcount(1),
m_completionEvent(nullptr),
m_connectionPoint(nullptr),
m_isInitialized(false)
{
PrintDocumentPackageStatus status = { 0 };
m_documentPackageStatus = status;
// Initialize the critical section, one time only.
InitializeCriticalSection(&m_criticalSection);
}
D2DPrintJobChecker::~D2DPrintJobChecker()
{
ReleaseResources();
// Release resources used by the critical section object.
DeleteCriticalSection(&m_criticalSection);
}
// Initialize this print job checker by registering a print document package target.
// This is required before the application can use this print job checker to monitor a print job.
HRESULT D2DPrintJobChecker::Initialize(
_In_ IPrintDocumentPackageTarget* documentPackageTarget
)
{
// Application can only initialize this job checker once.
if (m_isInitialized)
{
return E_FAIL;
}
HRESULT hr = (documentPackageTarget != nullptr) ? S_OK : E_INVALIDARG;
IConnectionPointContainer* connectionPointContainer = nullptr;
if (SUCCEEDED(hr))
{
hr = documentPackageTarget->QueryInterface(IID_PPV_ARGS(&connectionPointContainer));
}
if (SUCCEEDED(hr))
{
hr = connectionPointContainer->FindConnectionPoint(
__uuidof(IPrintDocumentPackageStatusEvent),
&m_connectionPoint
);
}
IUnknown* unknownEvent = nullptr;
if (SUCCEEDED(hr))
{
hr = this->QueryInterface(IID_PPV_ARGS(&unknownEvent));
}
if (SUCCEEDED(hr))
{
hr = m_connectionPoint->Advise(unknownEvent, &m_eventCookie);
}
// Create an event handle for print job completion.
if (SUCCEEDED(hr))
{
m_completionEvent = ::CreateEvent(
nullptr, // The handle cannot be inherited by child processes.
TRUE, // The event object requires the use of the ResetEvent function to set the event state to nonsignaled.
FALSE, // The initial state of the event object is nonsignaled.
nullptr // Name of the event object.
);
hr = (!m_completionEvent) ? HRESULT_FROM_WIN32(::GetLastError()) : hr;
}
if (unknownEvent)
{
unknownEvent->Release();
unknownEvent = nullptr;
}
if (connectionPointContainer)
{
connectionPointContainer->Release();
connectionPointContainer = nullptr;
}
if (SUCCEEDED(hr))
{
m_isInitialized = true;
}
else
{
ReleaseResources();
}
return hr;
}
HRESULT STDMETHODCALLTYPE
D2DPrintJobChecker::QueryInterface(
REFIID iid,
_Out_ void** ppvObject
)
{
if (iid == __uuidof(IUnknown) ||
iid == __uuidof(IPrintDocumentPackageStatusEvent))
{
*ppvObject = static_cast<IPrintDocumentPackageStatusEvent*>(this);
AddRef();
return S_OK;
}
else
{
*ppvObject = nullptr;
return E_NOINTERFACE;
}
}
ULONG STDMETHODCALLTYPE
D2DPrintJobChecker::AddRef()
{
return (ULONG)InterlockedIncrement(&m_refcount);
}
ULONG STDMETHODCALLTYPE
D2DPrintJobChecker::Release()
{
ULONG res = (ULONG)InterlockedDecrement(&m_refcount);
if (0 == res)
{
delete this;
}
return res;
}
HRESULT D2DPrintJobChecker::GetTypeInfoCount(
_Out_ UINT* /* pctinfo */
)
{
return E_NOTIMPL;
}
HRESULT D2DPrintJobChecker::GetTypeInfo(
UINT /* iTInfo */,
LCID /* lcid */,
_Outptr_result_maybenull_ ITypeInfo** /* ppTInfo */
)
{
return E_NOTIMPL;
}
HRESULT D2DPrintJobChecker::GetIDsOfNames(
_In_ REFIID /* riid */,
_In_reads_(cNames) LPOLESTR* /* rgszNames */,
_In_range_(0, 16384) UINT cNames,
LCID /* lcid */,
__out_ecount_full(cNames) DISPID* /* rgDispId */
)
{
UNREFERENCED_PARAMETER(cNames);
return E_NOTIMPL;
}
HRESULT D2DPrintJobChecker::Invoke(
DISPID /* dispIdMember */,
REFIID /* riid */,
LCID /* lcid */,
WORD /* wFlags */,
DISPPARAMS* /* pDispParams */,
VARIANT* /* pVarResult */,
EXCEPINFO* /* pExcepInfo */,
UINT* /* puArgErr */
)
{
return E_NOTIMPL;
}
// Callback to indicate that the status of the package has changed.
STDMETHODIMP D2DPrintJobChecker::PackageStatusUpdated(
_In_ PrintDocumentPackageStatus* packageStatus
)
{
HRESULT hr = (packageStatus == nullptr) ? E_INVALIDARG : S_OK;
if (SUCCEEDED(hr))
{
// The package status update operation is guarded with a critical section,
// since PackageStatusUpdated may be called in a very short time slot.
EnterCriticalSection(&m_criticalSection);
m_documentPackageStatus = *packageStatus;
LeaveCriticalSection(&m_criticalSection);
}
if (SUCCEEDED(hr))
{
// Signal the print job complete event.
if (PrintDocumentPackageCompletion_InProgress != m_documentPackageStatus.Completion)
{
if (!SetEvent(m_completionEvent))
{
hr = HRESULT_FROM_WIN32(::GetLastError());
}
}
}
#if defined(_DEBUG)
OutputPackageStatus(m_documentPackageStatus);
#endif
return S_OK;
}
// Return print document package status.
PrintDocumentPackageStatus D2DPrintJobChecker::GetStatus()
{
return(m_documentPackageStatus);
}
// Wait for job completion event to be signaled; in the meanwhile keep pumping messages to avoid deadlocks.
//
// This is an example for deadlock: when user print to a " Print To File" printer, the winspool.dll will try to pop
// up a modal dialog asking for file name. In order to show modal dialog, winspool will call EnableWindow(FALSE) to
// disable this window. Therefore, the winspool thread is waiting for us to process the message of disabling the
// window, while our thread is waiting for the winspool thread to complete.
HRESULT D2DPrintJobChecker::WaitForCompletion()
{
// Return successfully if this job checker is not initialized.
if (!m_isInitialized)
{
return S_OK;
}
HRESULT hr = S_OK;
bool isWaiting = true;
while (isWaiting)
{
DWORD waitResult = ::MsgWaitForMultipleObjects(
1, // Number of object handles in the array.
&m_completionEvent, // Array of object handles.
FALSE, // Function returns when the state of any one of the objects is set to signaled.
INFINITE, // Function will return only when the specified objects are signaled.
QS_ALLINPUT // Any message is in the queue.
);
if (waitResult == WAIT_OBJECT_0 + 1)
{
// This is not the desired completion event.
MSG msg;
while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
::DispatchMessage(&msg);
}
if (msg.message == WM_QUIT)
{
isWaiting = false;
break;
}
}
else
{
isWaiting = false;
}
}
return hr;
}
// Output debug message for the print document package status.
void D2DPrintJobChecker::OutputPackageStatus(
_In_ PrintDocumentPackageStatus packageStatus
)
{
switch (packageStatus.Completion)
{
case PrintDocumentPackageCompletion_Completed:
OutputDebugString(L"PrintDocumentPackageCompletion_Completed; ");
break;
case PrintDocumentPackageCompletion_Canceled:
OutputDebugString(L"PrintDocumentPackageCompletion_Canceled; ");
break;
case PrintDocumentPackageCompletion_Failed:
OutputDebugString(L"PrintDocumentPackageCompletion_Failed; ");
break;
case PrintDocumentPackageCompletion_InProgress:
OutputDebugString(L"PrintDocumentPackageCompletion_InProgress; ");
break;
default:
OutputDebugString(L"PrintDocumentPackageStatus unknown! ");
break;
}
WCHAR messageBuffer[256] = { 0 };
StringCchPrintf(
messageBuffer,
ARRAYSIZE(messageBuffer),
L"\tjobID:%3d, currentDoc:%3d, currentPage:%3d, currentPageTotal:%3d, packageStatus:0x%08X\n",
packageStatus.JobId,
packageStatus.CurrentDocument,
packageStatus.CurrentPage,
packageStatus.CurrentPageTotal,
packageStatus.PackageStatus
);
OutputDebugString(messageBuffer);
}
// Release resources.
void D2DPrintJobChecker::ReleaseResources()
{
if (m_connectionPoint != nullptr)
{
if (m_eventCookie != 0)
{
m_connectionPoint->Unadvise(m_eventCookie);
}
m_connectionPoint->Release();
m_connectionPoint = nullptr;
}
if (m_completionEvent != nullptr)
{
::CloseHandle(m_completionEvent);
m_completionEvent = nullptr;
}
return;
}