forked from gustavofbreunig/PrintQueueWatchCPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintjob.cpp
More file actions
373 lines (301 loc) · 8.84 KB
/
printjob.cpp
File metadata and controls
373 lines (301 loc) · 8.84 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
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
#include "printjob.h"
PrintJob::PrintJob(HANDLE mhPrinter, uint32_t dwJobId)
{
this->mhPrinter = mhPrinter;
midJob = dwJobId;
}
PrintJob::~PrintJob()
{
delete ji1;
delete ji2;
}
void PrintJob::RefreshJobInfo()
{
DWORD pcbNeeded = 0;
if (ji2 == nullptr)
{
//Get the required buffer size into pcbNeeded
if (!GetJob(mhPrinter, midJob, 2, (BYTE*)ji2, 0, &pcbNeeded))
{
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) //it's expected this error
{
std::string err("GetJob for JOB_INFO_2 failed on first call :");
err += std::to_string(GetLastError());
throw std::runtime_error(err.c_str());
}
}
//Allocate a buffer the right size
ji2 = (JOB_INFO_2*)new BYTE[pcbNeeded];
//Populate the JOB_INFO_1 structure
if (!GetJob(mhPrinter, midJob, 2, (BYTE*)ji2, pcbNeeded, &pcbNeeded))
{
std::string err("GetJob for JOB_INFO_2 failed on second call :");
err += std::to_string(GetLastError());
throw std::runtime_error(err.c_str());
}
}
if (ji1 == nullptr)
{
//Get the required buffer size into pcbNeeded
if (!GetJob(mhPrinter, midJob, 1, (BYTE*)ji1, 0, &pcbNeeded))
{
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) //it's expected this error
{
std::string err("GetJob for JOB_INFO_1 failed on first call :");
err += std::to_string(GetLastError());
throw std::runtime_error(err.c_str());
}
}
//Allocate a buffer the right size
ji1 = (JOB_INFO_1*)new BYTE[pcbNeeded];
//Populate the JOB_INFO_1 structure
if (!GetJob(mhPrinter, midJob, 1, (BYTE*)ji1, pcbNeeded, &pcbNeeded))
{
std::string err("GetJob for JOB_INFO_1 failed on second call :");
err += std::to_string(GetLastError());
throw std::runtime_error(err.c_str());
}
}
}
uint32_t PrintJob::getJobId()
{
return midJob;
}
std::wstring PrintJob::getPrinterName()
{
//The name of the print device that this job is queued against
std::wstring name(ji1->pPrinterName);
return name;
}
std::wstring PrintJob::getUserName()
{
//The name of the user that sent the print job for printing
std::wstring name(ji1->pUserName);
return name;
}
std::wstring PrintJob::getMachineName()
{
//The name of the workstation that sent the print job to print
std::wstring machine(ji1->pMachineName);
return machine;
}
std::wstring PrintJob::getDocument()
{
//The document name being printed
// This value is set by the application which sends the job to be printed. Many
// applications put the application name at the start of the document name to aid
// identification
std::wstring document(ji1->pDocument);
return document;
}
std::wstring PrintJob::getStatusDescription()
{
//The description of the current status of the print job
if (ji1->pStatus)
{
std::wstring status(ji1->pStatus);
return status;
}
else
{
return std::wstring(L"");
}
}
void PrintJob::setStatusDescription(WCHAR *desc)
{
delete ji1->pStatus;
ji1->pStatus = new WCHAR[wcslen(desc) + 1];
wcscpy(ji1->pStatus, desc);
}
std::wstring PrintJob::getDataType()
{
std::wstring datatype(ji1->pDatatype);
return datatype;
}
std::wstring PrintJob::getNotifyUserName()
{
//The user to notify about the progress of this print job
std::wstring user(ji2->pNotifyName);
return user;
}
std::wstring PrintJob::getPrintProcessoName()
{
// The name of the print processor
//which is responsible for printing this job
std::wstring processor(ji2->pPrintProcessor);
return processor;
}
std::wstring PrintJob::getDriverName()
{
// The name of the printer driver
// that is responsible for producing this print job
std::wstring name(ji2->pDriverName);
return name;
}
std::wstring PrintJob::getParameters()
{
//Extra driver specific parameters for this print job
std::wstring param(ji2->pParameters);
return param;
}
int32_t PrintJob::getPagesPrinted()
{
//Gets the number of pages in each job as it is deleted
return ji1->PagesPrinted;
}
void PrintJob::setPagesPrinted(int32_t pages)
{
ji1->PagesPrinted = pages;
}
int32_t PrintJob::getPosition()
{
//The position of the job in the print device job queue
return ji1->Position;
}
int32_t PrintJob::getTotalPages()
{
//Number of pages in each new job as it is added to the monitored printers
return ji2->TotalPages;
}
void PrintJob::setTotalPages(int32_t pages)
{
ji2->TotalPages = pages;
}
int32_t PrintJob::getPriority()
{
// The priority of this print job. Higher priority jobs will be processed ahead
// of lower priority jobs
return ji2->Priority;
}
int32_t PrintJob::getQueuedTime()
{
//Specifies the total time, in milliseconds, that has elapsed since the job began printing
return ji2->Time;
}
int32_t PrintJob::getJobSize()
{
//Specifies the size, in bytes, of the job
//While the job is being spooled this will contain the current size of the spool file
return ji2->Size;
}
void PrintJob::setJobSize(int32_t size)
{
ji2->Size = size;
}
int16_t PrintJob::getPaperKind()
{
//The paper type that the job is intended to be printed on
//This could be a standard paper size (A4, A5 etc) or custom paper size if the printer
return ji2->pDevMode->dmPaperSize;
}
int16_t PrintJob::getPaperWidth()
{
//The width of the selected paper (if PaperKind is PaperKind.Custom)
//This value is measured in millimeters
return ji2->pDevMode->dmPaperWidth;
}
int16_t PrintJob::getPaperLength()
{
//The height of the selected paper (if PaperKind is PaperKind.Custom)
//This value is measured in millimeters
return ji2->pDevMode->dmPaperLength;
}
int16_t PrintJob::getPaperSource()
{
return ji2->pDevMode->dmDefaultSource;
}
int16_t PrintJob::getPrinterResolution()
{
return ji2->pDevMode->dmPrintQuality;
}
int16_t PrintJob::getCopies()
{
//The number of copies of each page to print
// Some applications (Microsoft Word) misreport the number of copies to the spooler which will
// result in incorrect values being returned
if (ji2->pDevMode->dmCopies > 0)
{
return ji2->pDevMode->dmCopies;
}
else
{
if (ji2->PagesPrinted > ji2->TotalPages)
{
return ji2->PagesPrinted / ji2->TotalPages;
}
else
{
return 1;
}
}
}
bool PrintJob::isLandscape()
{
return ji2->pDevMode->dmOrientation == DMORIENT_LANDSCAPE;
}
bool PrintJob::isColor()
{
return ji2->pDevMode->dmColor == DMCOLOR_COLOR;
}
bool PrintJob::isDuplex()
{
return ji2->pDevMode->dmDuplex == DM_DUPLEX;
}
bool PrintJob::isDeleted()
{
//True if the print job has been deleted
return (ji1->Status & JOB_STATUS_DELETED) == JOB_STATUS_DELETED;
}
bool PrintJob::isDeleting()
{
//True if the print job is being deleted
return (ji1->Status & JOB_STATUS_DELETING) == JOB_STATUS_DELETING;
}
bool PrintJob::isPrinted()
{
//True if the job has been printed
// This will be true once the job has been completely sent to the printer. This
// does not mean that the physical print out has necessarily appeared.
return (ji1->Status & JOB_STATUS_PRINTED) == JOB_STATUS_PRINTED;
}
bool PrintJob::isPrinting()
{
//True if the print job is currently printing
return (ji1->Status & JOB_STATUS_PRINTING) == JOB_STATUS_PRINTING;
}
bool PrintJob::isInError()
{
//True if there is an error with this print job that prevents it from
return (ji1->Status & JOB_STATUS_ERROR) == JOB_STATUS_ERROR;
}
bool PrintJob::isOffline()
{
//True if the job is in error because the printer is off line
return (ji1->Status & JOB_STATUS_OFFLINE) == JOB_STATUS_OFFLINE;
}
bool PrintJob::isPaperOut()
{
//True if the job is in error because the printer has run out of paper
return (ji1->Status & JOB_STATUS_PAPEROUT) == JOB_STATUS_PAPEROUT;
}
bool PrintJob::isUserInterventionRequired()
{
//True if the print job is in error because the printer requires user intervention
//This can be caused by a print job that requires manual paper feed
return (ji1->Status & JOB_STATUS_USER_INTERVENTION) == JOB_STATUS_USER_INTERVENTION;
}
bool PrintJob::isSpooling()
{
//True if the print job is spooling to a spool file
return (ji1->Status & JOB_STATUS_SPOOLING) == JOB_STATUS_SPOOLING;
}
void PrintJob::setStatus(int32_t status)
{
ji1->Status = status;
}
SYSTEMTIME PrintJob::getSubmitted()
{
//Specifies the date and time at which the job was submitted for printing
//for info about SYSTEMTIME struct, see https://msdn.microsoft.com/pt-br/library/windows/desktop/ms724950(v=vs.85).aspx
return ji2->Submitted;
}