SDI app: Wait for printer
-
Hello, In my application, I send a print command to my view from a (modal) dialog:
pView->OnCmdMsg(ID_FILE_PRINT, 0, 0, 0);
How can I wait until the printing process has been finished? Background: In my dialog I want to be able to print other data than loaded into the current view. For this purpose, I save the state of the current view, load another document, print it and afterthe document has finished printing I reload the old document again and close the dialog. Thank you and regards, Niki
-
Hello, In my application, I send a print command to my view from a (modal) dialog:
pView->OnCmdMsg(ID_FILE_PRINT, 0, 0, 0);
How can I wait until the printing process has been finished? Background: In my dialog I want to be able to print other data than loaded into the current view. For this purpose, I save the state of the current view, load another document, print it and afterthe document has finished printing I reload the old document again and close the dialog. Thank you and regards, Niki
-
Hello, In my application, I send a print command to my view from a (modal) dialog:
pView->OnCmdMsg(ID_FILE_PRINT, 0, 0, 0);
How can I wait until the printing process has been finished? Background: In my dialog I want to be able to print other data than loaded into the current view. For this purpose, I save the state of the current view, load another document, print it and afterthe document has finished printing I reload the old document again and close the dialog. Thank you and regards, Niki
Printing is heavily abstracted in windows - this is one of the main selling points they had waaaaaaay back when. But that means you shove data at the print spooler - and you know when that is finished. But that may then shove data at some remote machine which may or may not complete then. That may then go to a fancy print system which could batch your print for late at night, for its own convenience. None of this gets fed back to you. You could do this:
while (1)
{
if (MessageBox (_T("Has the paper come out yet?"), MB_YESNO | MB_ICONQUESTION) == IDYES)
break;
}Being slightly serious, the OnCmdMsg (ID_FILE_PRINT, 0, 0, 0) shouldn't return until it's finished its bit of the printing process. Iain.
-
Printing is heavily abstracted in windows - this is one of the main selling points they had waaaaaaay back when. But that means you shove data at the print spooler - and you know when that is finished. But that may then shove data at some remote machine which may or may not complete then. That may then go to a fancy print system which could batch your print for late at night, for its own convenience. None of this gets fed back to you. You could do this:
while (1)
{
if (MessageBox (_T("Has the paper come out yet?"), MB_YESNO | MB_ICONQUESTION) == IDYES)
break;
}Being slightly serious, the OnCmdMsg (ID_FILE_PRINT, 0, 0, 0) shouldn't return until it's finished its bit of the printing process. Iain.
Hello and thank you for your answer! I do not want to check if the document has printed successfully but it should just be successfully appended to the queue (before the new document is loaded). I think I managed this (the following code is done in a modal dialog):
// pView is a pointer to my current view (CHtmlView)
// get currently loaded document
CString currentUrl = pView->GetLocationURL();
// load document to be printed
pView->Pall();
// wait until the document has loaded successfully
while(pView->GetReadyState() != READYSTATE_COMPLETE)
{
((CMyApp*)AfxGetApp())->Yeild();
}
// FIXXXME: IS THIS OPERATION REALLY BLOCKING?
pView->OnCmdMsg(ID_FILE_PRINT, 0, 0, 0);
// restore old document
pView->Navigate(currentUrl);
// return to my app
EndDialog(FALSE);The OnCmdMsg(ID_FILE_PRINT) does nothing more than
ExecWB(OLECMDID_PRINT,...)
in the IWebBrowser2. And now my "backup question" is. You said: "Being slightly serious, the OnCmdMsg (ID_FILE_PRINT, 0, 0, 0) shouldn't return until it's finished its bit of the printing process." Are you sure that ExecWB(OLECMDID_PRINT,...) will block until the currently loaded document has been printed so that I can issue the next Navigate(...) call just afterwards? In my tests it works but I am not sure about this. Regards, Niki -
Hello, In my application, I send a print command to my view from a (modal) dialog:
pView->OnCmdMsg(ID_FILE_PRINT, 0, 0, 0);
How can I wait until the printing process has been finished? Background: In my dialog I want to be able to print other data than loaded into the current view. For this purpose, I save the state of the current view, load another document, print it and afterthe document has finished printing I reload the old document again and close the dialog. Thank you and regards, Niki
You could obtain a PRINTER_INFO_2[^] structure which contains the number of jobs in the printer que. The Status member should return PRINTER_STATUS_PRINTING while printing and PRINTER_STATUS_WAITING when finished. If you wanted to track individual print jobs you could use the JOB_INFO_1[^] or other associated print job structures to track the job. The Status member will return JOB_STATUS_PRINTED when the job has completed. It should be noted that older printers and/or port monitor drivers may always return JOB_STATUS_PRINTED information for job status. Your mileage may vary. Some documentation: http://support.microsoft.com/kb/158828[^] Some sample code:
#include <winspool.h> /************************************************************************************ HANDLE OpenDefaultPrinter(ACCESS_MASK dwMask) Returns a HANDLE to the Default system printer. The caller must Close the HANDLE. -David Delaune 12-06-2006 *************************************************************************************/ HANDLE YourClass::OpenDefaultPrinter(ACCESS_MASK dwMask) { HANDLE hPrinter = INVALID_HANDLE_VALUE; PRINTER_DEFAULTS pDef; DWORD dwSize; ZeroMemory(&pDef, sizeof(pDef)); GetDefaultPrinter(NULL, &dwSize); TCHAR* szBuffer = new TCHAR[dwSize]; if(NULL != szBuffer) { if(GetDefaultPrinter(szBuffer, &dwSize)) { pDef.DesiredAccess = dwMask; OpenPrinter(szBuffer, &hPrinter, &pDef); } delete szBuffer; } return hPrinter; } /************************************************************************************ DWORD GetPrintQueCount() Returns the number of waiting print jobs in the Default printer que. -David Delaune 12-06-2006 *************************************************************************************/ DWORD YourClass::GetPrintQueCount() { DWORD dwNeeded = NULL; PRINTER_INFO_2 *pInfo = NULL; DWORD dwRet = 0; HANDLE hPrinter = OpenDefaultPrinter(); if(INVALID_HANDLE_VALUE !=
-
Hello and thank you for your answer! I do not want to check if the document has printed successfully but it should just be successfully appended to the queue (before the new document is loaded). I think I managed this (the following code is done in a modal dialog):
// pView is a pointer to my current view (CHtmlView)
// get currently loaded document
CString currentUrl = pView->GetLocationURL();
// load document to be printed
pView->Pall();
// wait until the document has loaded successfully
while(pView->GetReadyState() != READYSTATE_COMPLETE)
{
((CMyApp*)AfxGetApp())->Yeild();
}
// FIXXXME: IS THIS OPERATION REALLY BLOCKING?
pView->OnCmdMsg(ID_FILE_PRINT, 0, 0, 0);
// restore old document
pView->Navigate(currentUrl);
// return to my app
EndDialog(FALSE);The OnCmdMsg(ID_FILE_PRINT) does nothing more than
ExecWB(OLECMDID_PRINT,...)
in the IWebBrowser2. And now my "backup question" is. You said: "Being slightly serious, the OnCmdMsg (ID_FILE_PRINT, 0, 0, 0) shouldn't return until it's finished its bit of the printing process." Are you sure that ExecWB(OLECMDID_PRINT,...) will block until the currently loaded document has been printed so that I can issue the next Navigate(...) call just afterwards? In my tests it works but I am not sure about this. Regards, NikiI confess I have not printed from an HtmlView, so take my logic with some skepticism. From the docs: Executes a command on an OLE object and returns the status of the command execution using the IOleCommandTarget interface. It does not say "queue up some thready thing". So S_OK, means the job is *done*. Iain.