nmsltd
asked on
Printer WaitForSingleObject problems
The code below is SUPPOSED to wait until the print spooler has a job submitted to it. Unfortunately, it always returns IMMEDIATELY from the WaitForSingleObject without waiting for a job! All above this point doesn't error (have stripped out error checking!
////////////////////////// ////////// ////////// ///
DWORD needed, returned;
HANDLE printer = 0;
BOOL didit;
PRINTER_INFO_1 printers[255];
PRINTER_DEFAULTS pdefaults;
DWORD event;
HANDLE changenotify;
EnumPrinters((DWORD)PRINTE R_ENUM_NAM E,(LPTSTR) "Windows 95 Local Print Provider",
(DWORD)1,(LPBYTE)printers, sizeof(PRI NTER_INFO_ 1)*255,
&needed,&returned);
pdefaults.pDatatype = NULL;
pdefaults.pDevMode = NULL;
pdefaults.DesiredAccess = PRINTER_ACCESS_ADMINISTER;
OpenPrinter(printers[0].pN ame,(LPHAN DLE)&print er,(LPPRIN TER_DEFAUL TS)&pdefau lts);
// Now let's see if we can wait for a job!
changenotify = FindFirstPrinterChangeNoti fication(p rinter,PRI NTER_CHANG E_ALL,0,NU LL);
event = WaitForSingleObject(change notify,INF INITE);
MessageBox("Wait Completed","Wait");
if(event==WAIT_FAILED)
{
char buff[50];
////////////////////////// ////////// ////////// ////////// ////////// ////////// /////////
wsprintf(buff,"failed...%d ",GetLastE rror()); // **HERE IS WHERE IT ALWAYS FAILS!! ** With the informative error code 6!!!
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////////
MessageBox(buff);
}
else if(event == PRINTER_CHANGE_JOB)
{
MessageBox("Yippee Printer change job signalled\n","Mwuhahaha");
}
else
{
sprintf(buff,"Hmmm....wait returned %d",event);
EditBox->SetText(buff);
}
//////////////////////////
DWORD needed, returned;
HANDLE printer = 0;
BOOL didit;
PRINTER_INFO_1 printers[255];
PRINTER_DEFAULTS pdefaults;
DWORD event;
HANDLE changenotify;
EnumPrinters((DWORD)PRINTE
(DWORD)1,(LPBYTE)printers,
&needed,&returned);
pdefaults.pDatatype = NULL;
pdefaults.pDevMode = NULL;
pdefaults.DesiredAccess = PRINTER_ACCESS_ADMINISTER;
OpenPrinter(printers[0].pN
// Now let's see if we can wait for a job!
changenotify = FindFirstPrinterChangeNoti
event = WaitForSingleObject(change
MessageBox("Wait Completed","Wait");
if(event==WAIT_FAILED)
{
char buff[50];
//////////////////////////
wsprintf(buff,"failed...%d
//////////////////////////
MessageBox(buff);
}
else if(event == PRINTER_CHANGE_JOB)
{
MessageBox("Yippee Printer change job signalled\n","Mwuhahaha");
}
else
{
sprintf(buff,"Hmmm....wait
EditBox->SetText(buff);
}
ASKER
Edited text of question
Error code 6 mean 'Invalid handle'
Did you check changenotyfy variable after calling FindFirstPrinterChangeNoti fication() to make sure it is not equal INVALID_HANDLE_VALUE? If so, invoke GetLastError() immediately after this call and check error code.
Also, is OpenPrinter() return TRUE?
Did you check changenotyfy variable after calling FindFirstPrinterChangeNoti
Also, is OpenPrinter() return TRUE?
I'm not sure you can set these values to NULL:
pdefaults.pDatatype = NULL;
pdefaults.pDevMode = NULL;
May be, better try this:
OpenPrinter(printers[0].pN ame,(LPHAN DLE)&print er,NULL);
pdefaults.pDatatype = NULL;
pdefaults.pDevMode = NULL;
May be, better try this:
OpenPrinter(printers[0].pN
If it works, please let me know to turn comments into answer.
also make sure your linking to the multi-threaded libraries, because by default (msvc 4.x and 5.x) they are single-threaded..
ASKER
In response to above comments:
Yes I do check for INVALID_HANDLE_VALUE (it's ok).
OpenPrinter does indeed return TRUE.
According to the Borland C++ V5 helpdoc on open printer:
".....or obtain a handle to a print server to call WaitForPrinterChange. To do so, it should call OpenPrinter with pPrinterName set to the name of the server and should specify a server access mask value such as SERVER_ALL_ACCESS. The pDatatype and pDevMode members of the PRINTER_DEFAULTS data structure should be set to NULL. The handle returned by such a call may be passed to the ClosePrinter or WaitForPrinterChange function.
So (allegedly) you can set these values to NULL.
I am linking multithreading libraries.
I have tried OpenPrinter(printers[0].pN ame,(LPHAN DLE)&print er,NULL);
Still doesn't work! :(
My only thoughts on the matter reading the above snippet from the helpdoc is that it says "print server". Is there a difference between a printer returned from EnumPrinters and a print server, or a print provider? I have found no real explanation of the terminology in any documentation (oh for a decent book on windows printer programming - are there any???)
Hope this clarifies the problem!
Yes I do check for INVALID_HANDLE_VALUE (it's ok).
OpenPrinter does indeed return TRUE.
According to the Borland C++ V5 helpdoc on open printer:
".....or obtain a handle to a print server to call WaitForPrinterChange. To do so, it should call OpenPrinter with pPrinterName set to the name of the server and should specify a server access mask value such as SERVER_ALL_ACCESS. The pDatatype and pDevMode members of the PRINTER_DEFAULTS data structure should be set to NULL. The handle returned by such a call may be passed to the ClosePrinter or WaitForPrinterChange function.
So (allegedly) you can set these values to NULL.
I am linking multithreading libraries.
I have tried OpenPrinter(printers[0].pN
Still doesn't work! :(
My only thoughts on the matter reading the above snippet from the helpdoc is that it says "print server". Is there a difference between a printer returned from EnumPrinters and a print server, or a print provider? I have found no real explanation of the terminology in any documentation (oh for a decent book on windows printer programming - are there any???)
Hope this clarifies the problem!
http://www.microsoft.com/win32dev/guidelns/apidiffs.htm DECLARES:
Printer change notification
Printer change notification print spooler functions are not supported on Windows 95 nor on Windows 95 OSR 2. The following functions are not supported:
FindClosePrinterChangeNoti fication
FindFirstPrinterChangeNoti fication
FindNextPrinterChangeNotif ication
WaitForPrinterChange
Printer change notification
Printer change notification print spooler functions are not supported on Windows 95 nor on Windows 95 OSR 2. The following functions are not supported:
FindClosePrinterChangeNoti
FindFirstPrinterChangeNoti
FindNextPrinterChangeNotif
WaitForPrinterChange
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
FindFirstPrinterChangeNoti fication() works fine for NT, but not for Windows95!
ASKER
100% Correct! I discovered this myself at 1:30 this afternoon!
Great minds think alike, eh Nick?! Now all I have to figure out is how I'm going to implement FindFirstPrinterChangeNoti fication on win95 & the project's home and dry :)
Great minds think alike, eh Nick?! Now all I have to figure out is how I'm going to implement FindFirstPrinterChangeNoti
if(event==WAIT_FAILED) { wsprintf(buff,"failed...%d