monitoring printer
i've combined dll monitors the netowork printer. printer connected server around usb cable. i imitation something directly server, displays information pages sent/printed properly, nonetheless i imitation something fan computers wholly displays the ip, sent/printed always 0.
i suspect accountability winspooler api, doubt is, there any proceed around it?
here's code, the worth:
typedef struct printermonitor_api tagprintjobinfo
{
// name accessory printed
bake *machinename;
// name document
bake *document;
// sum pages indeed *printed*
dword totalpages;
// array pages sent printer
dword printed;
// private: state printjob
bool done;
} printjobinfo_t;
//-------------------------------------------------------
// duty called imitation office eventuality finishes
//-------------------------------------------------------
typedef vacant (*callback)(printjobinfo_t *);
//-------------------------------------------------------
// printer guard class
//-------------------------------------------------------
class printermonitor
{
private:
//-------------------------------------------------------
// hoop printer.
//-------------------------------------------------------
hoop m_hprinter;
//-------------------------------------------------------
// hoop report vigilant being set imitation event
// occurs.
//-------------------------------------------------------
hoop m_hnotify;
//-------------------------------------------------------
// hoop the workman thread.
//-------------------------------------------------------
hoop m_hthread;
//-------------------------------------------------------
// binds printer_change_xxx_xxx information current
// event.
//-------------------------------------------------------
dword m_dwchanged;
//-------------------------------------------------------
// id workman thread.
//-------------------------------------------------------
dword m_dwthreadid;
//-------------------------------------------------------
// name printer.
//-------------------------------------------------------
bake *m_pszprintername;
//-------------------------------------------------------
// printer eventuality snooping options.
//-------------------------------------------------------
printer_notify_options *m_poptions;
//-------------------------------------------------------
// cost eventuality function.
//-------------------------------------------------------
printer_notify_info *m_poutput;
//-------------------------------------------------------
// shall away?
//-------------------------------------------------------
bool m_fexit;
public:
//-------------------------------------------------------
// callback duty called eventuality fires.
//-------------------------------------------------------
callback m_fpcallback;
//-------------------------------------------------------
// constructor
// - name printer
// - callback function
//-------------------------------------------------------
printermonitor(char *printername, callback callback)
{
memset(this, 0, sizeof(printermonitor)); // 0 me
m_fpcallback = callback;
m_pszprintername = new char[strlen(printername)];
strcpy(m_pszprintername, printername);
m_poptions = new printer_notify_options;
m_hthread = createthread(null, 0, (lpthread_start_routine)printermonitor::startmonitoring, this, create_suspended, &m_dwthreadid);
}
//-------------------------------------------------------
// stop workman thread mislay shit
//-------------------------------------------------------
~printermonitor()
{
stopmonitor();
delete(m_poptions);
delete[](m_pszprintername);
}
//-------------------------------------------------------
// run guard seperate thread
//-------------------------------------------------------
vacant runmonitor()
{
resumethread(m_hthread);
}
//-------------------------------------------------------
// stop monitor, noes.
//-------------------------------------------------------
vacant stopmonitor()
{
m_fexit = true;
}
//-------------------------------------------------------
// checks vigilant current monitor
//-------------------------------------------------------
bool checkmonitor()
{
if(openprinter(this->m_pszprintername, &this->m_hprinter, null))
{
if(this->m_hprinter != invalid_handle_value)
{
closeprinter(this->m_hprinter);
relapse true;
}
}
relapse false;
}
private:
//-------------------------------------------------------
// workman thread proc
//-------------------------------------------------------
stationary vacant winapi startmonitoring(void *thisptr)
{
printermonitor* pthis = reinterpret_cast<printermonitor*>(thisptr);
if(openprinter(pthis->m_pszprintername, &pthis->m_hprinter, null))
{
wjobfields[5];
wjobfields[0] = job_notify_field_status;
wjobfields[1] = job_notify_field_machine_name;
wjobfields[2] = job_notify_field_total_pages;
wjobfields[3] = job_notify_field_pages_printed;
wjobfields[4] = job_notify_field_document;
printer_notify_options_type roptions[1];
roptions[0].type = job_notify_type;
roptions[0].pfields = &wjobfields[0];
roptions[0].count = 5;
pthis->m_poptions->count = 1;
pthis->m_poptions->version = 2;
pthis->m_poptions->ptypes = roptions;
pthis->m_poptions->flags = printer_notify_options_refresh;
if((pthis->m_hnotify =
findfirstprinterchangenotification(pthis->m_hprinter, 0, 0, pthis->m_poptions)) != invalid_handle_value)
{
while(pthis->m_hnotify != invalid_handle_value && !pthis->m_fexit)
{
if(waitforsingleobject(pthis->m_hnotify, 10) == wait_object_0)
{
findnextprinterchangenotification(pthis->m_hnotify, &pthis->m_dwchanged, (lpvoid)pthis->m_poptions, (lpvoid*)&pthis->m_poutput);
printjobinfo_t info = {0};
(unsigned int i=0; i < pthis->m_poutput->count; i++)
{
printer_notify_info_data information = pthis->m_poutput->adata[i];
if(data.type == job_notify_type)
{
switch(data.field
{
box job_notify_field_pages_printed:
{
info.printed = data.notifydata.adwdata[0];
}
break;
box job_notify_field_machine_name:
{
lpstr name = (lpstr) data.notifydata.data.pbuf;
unsigned int length = strlen(name);
info.machinename = new char[length];
strcpy(info.machinename, name);
}
break;
box job_notify_field_total_pages:
{
info.totalpages = data.notifydata.adwdata[0];
}
break;
box job_notify_field_document:
{
lpstr request = (lpstr) data.notifydata.data.pbuf;
unsigned int length = strlen(document);
info.document = new char[length];
strcpy(info.document, document);
}
break;
box job_notify_field_status:
{
if(data.notifydata.adwdata[0] & job_status_printed)
{
info.done = true;
}
}
break;
}
}
}
if(info.done)
{
pthis->m_fpcallback(&info);
delete[](info.document);
delete[](info.machinename);
}
}
}
if(pthis->m_hprinter != invalid_handle_value)
closeprinter(pthis->m_hprinter);
if(pthis->m_hnotify != invalid_handle_value)
findcloseprinterchangenotification(pthis->m_hnotify);
}
}
}
};
Comments
Post a Comment