forked from gustavofbreunig/PrintQueueWatchCPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinterinformation.cpp
More file actions
175 lines (155 loc) · 4.07 KB
/
printerinformation.cpp
File metadata and controls
175 lines (155 loc) · 4.07 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
#include "printerinformation.h"
void PrinterInformation::SetMonitored(bool value)
{
if (value)
{
if (_NotificationThread == NULL)
{
_Cancelled = false;
//create the eventqueue
EventQueue = new PrinterEventQueue(JobEvent);
//create the eventqueue consumer thread
pthread_create(&_eventConsumer, NULL, PrinterEventQueue::staticStartThread, EventQueue);
_NotificationThread = new PrinterChangeNotificationThread(mhPrinter, this);
//create the notification thread consumer
pthread_create(&_thread, NULL, PrinterChangeNotificationThread::staticStartThread, _NotificationThread);
}
}
else
{
if (!_Cancelled)
{
_Cancelled = true;
//Stop monitoring the printer
if (!FindClosePrinterChangeNotification(_NotificationThread->mhWait))
{
std::string err("FindClosePrinterChangeNotification() failed: error ");
err += std::to_string(GetLastError());
err += " on printer ";
char printerName[wcslen(mPrinter_Info_2->pPrinterName) + 1];
wcstombs(printerName, mPrinter_Info_2->pPrinterName, wcslen(mPrinter_Info_2->pPrinterName));
err += printerName;
throw std::runtime_error(err.c_str());
}
//Wait for the monitoring thread to terminate
pthread_join(_thread, NULL);
EventQueue->CancelConsumerThread();
//wait for the eventqueue consumer to terminate
pthread_join(_eventConsumer, NULL);
delete EventQueue;
delete _NotificationThread;
std::wcout << L"All resources released in " << mPrinter_Info_2->pPrinterName << std::endl;
}
}
}
void PrinterInformation::InitPrinterInfo()
{
DWORD BytesWritten = 0;
BYTE* ptBuf = (BYTE*)malloc(1);
//pega o tamanho de buffer necessario na primeira chamada
if (!GetPrinter(mhPrinter, 2, ptBuf, 1, &BytesWritten))
{
free(ptBuf); //já sei quantos bytes preciso, aloco memória novamente
ptBuf = (BYTE*)malloc(BytesWritten);
if (GetPrinter(mhPrinter, 2, ptBuf, BytesWritten, &BytesWritten))
{
mPrinter_Info_2 = (PRINTER_INFO_2*)ptBuf;
}
else
{
std::string err("InitPrinterInfo error: ");
err += std::to_string(GetLastError());
throw std::runtime_error(err.c_str());
}
}
else
{
std::string err("InitPrinterInfo error: ");
err += std::to_string(GetLastError());
throw std::runtime_error(err.c_str());
}
}
PrinterInformation::PrinterInformation(std::wstring DeviceName, ACCESS_MASK DesiredAccess, std::function<void (PrintJobEventArgs *)> *JobEvent)
{
this->JobEvent = JobEvent;
WCHAR* dev = &DeviceName[0];
PRINTER_DEFAULTS def;
def.DesiredAccess = DesiredAccess;
def.pDatatype = 0;
def.pDevMode = 0;
if (OpenPrinter(dev, &mhPrinter, &def))
{
InitPrinterInfo();
}
else
{
def.DesiredAccess = PRINTER_ALL_ACCESS;
if (OpenPrinter(dev, &mhPrinter, &def))
{
InitPrinterInfo();
}
else if (OpenPrinter(dev, &mhPrinter, NULL))
{
InitPrinterInfo();
}
else
{
std::string err("OpenPrinter() failed for printer ");
char devName[DeviceName.length() + 1];
std::wcstombs(devName, DeviceName.c_str(), DeviceName.length() + 1);
err += devName;
err += ", error: ";
err += std::to_string(GetLastError());
throw std::runtime_error(err.c_str());
}
}
}
bool PrinterInformation::IsNetworkPrinter()
{
return mPrinter_Info_2->Attributes & PRINTER_ATTRIBUTE_NETWORK;
}
PrinterInformation::~PrinterInformation()
{
ClosePrinter(mhPrinter);
free(mPrinter_Info_2);
}
void PrinterInformation::CancelWatching()
{
_Cancelled = true;
}
bool PrinterInformation::isCancelled()
{
return _Cancelled;
}
void PrinterInformation::addPrintJob(PrintJob* job)
{
_PrintJobs.push_back(job);
}
PrintJob* PrinterInformation::getItemByJobId(uint32_t jobId)
{
int i = 0;
std::vector<PrintJob*>::iterator iter;
for (iter = _PrintJobs.begin(); iter < _PrintJobs.end(); iter++)
{
PrintJob* pJob = *iter;
if (pJob->getJobId() == jobId)
{
break;
}
i++;
}
return _PrintJobs.at(i);
}
bool PrinterInformation::PrintJobExists(uint32_t jobId)
{
std::vector<PrintJob*>::iterator iter;
for (iter = _PrintJobs.begin(); iter < _PrintJobs.end(); iter++)
{
PrintJob* pJob = *iter;
if (pJob->getJobId() == jobId)
{
return true;
}
}
return false;
}