Aborting a printing process

If you are a C/C++/C# developer and have any questions about using our products from your application here is the place to post them.
Post Reply
Riccardone
Posts: 7
Joined: Fri Dec 17 2004

Aborting a printing process

Post by Riccardone »

Hello,
due a selection file name dialog box that I put in the Converter activation fire event, I have to "trap" the user selection of "Cancel" button.
So, I'd like to abort the "print process" while is in progress but I don't understand how to handle the HDC argument fired with CCDIntfEx::StartDocPost() event.

I tried two different ways:
1) using the api ::AbortDoc() that throws a system message "The handle is invalid." and
2) using the ::SetJob() that gives me an "Access is denied." message

I am using the ActiveX version of PDF Converter 2.10i-3 and MS VC++ 6.0 on XP platform.

So I am sure the code that follows speaks better than me... I wrote something like this:


extern CCDIntfEx m_hPDF;
extern BOOL bAbortPrint;

// EVENT ID 7 /* EnabledPre */, VTS_NONE
void CMainFrame::OnPdfEnabledPre()
{
TRACE("CCDIntfEx has fired EnabledPre\n");

// Ask for the PDF FileName
CFileDialog dlg( FALSE, "pdf", m_hPDF.GetDefaultFileName(),
OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
"File PDF (*.pdf)|*.pdf||", this );

bAbortPrint = FALSE;

// Set PDF FileName
if(IDOK == dlg.DoModal())
m_hPDF.SetDefaultFileName( dlg.GetPathName() );
else
bAbortPrint = TRUE;

m_hPDF.EnablePrinter(<MyUser>, <MyLicence>);
}

// EVENT ID 2 /* StartDocPost */ VTS_I4 VTS_I4
void CMainFrame::OnPdfStartDocPost(long nJobId, long nHdc)
{
TRACE("CCDIntfEx has fired StartDocPost\n");

// Means user pressed CANCEL on the previous DoModal() for FileName selection
if(bAbortPrint)
{
// Abort document - ****** 1st attempt ******
if( AbortDoc((HDC) (long) nHdc) <= 0 )
{
LPVOID lpvMessageBuffer;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
FORMAT_MESSAGE_FROM_SYSTEM,
NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpvMessageBuffer, 0, NULL );
// This prints "The handle is invalid" (code 6 in WinXP)
TRACE(lpvMessageBuffer);
LocalFree(lpvMessageBuffer);

PRINTER_DEFAULTS pdef;
pdef.pDatatype = pdef.pDevMode = NULL;
pdef.DesiredAccess = PRINTER_ACCESS_ADMINISTER;
HANDLE hPrinter = 0;
if(OpenPrinter(prn, &hPrinter, &pdef))
{
// Abort document - ****** 2nd attempt ******
if( !SetJob(hPrinter, nJobId, 0, 0, JOB_CONTROL_DELETE) )
{
LPVOID lpvMessageBuffer;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
FORMAT_MESSAGE_FROM_SYSTEM,
NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL,
SUBLANG_DEFAULT),
(LPTSTR)&lpvMessageBuffer, 0, NULL);
// This prints "Access is denied." message (code 5 in WinXP)
TRACE(lpvMessageBuffer);
LocalFree(lpvMessageBuffer);
}
ClosePrinter(hPrinter);
}
}
}
}


Thank you in advance.
Joan
Amyuni Team
Posts: 2799
Joined: Wed Sep 11 2002
Contact:

Post by Joan »

Hello,

You are calling AbortDoc correctly but the problem might be that the printer has timed out before you get to call abortdoc.

The proper way to do what are doing is to prompt for filename in startdocpre. On EnablePre you should only call EnablePrinter.
Riccardone
Posts: 7
Joined: Fri Dec 17 2004

Post by Riccardone »

Joan wrote:Hello,

You are calling AbortDoc correctly but the problem might be that the printer has timed out before you get to call abortdoc.

The proper way to do what are doing is to prompt for filename in startdocpre. On EnablePre you should only call EnablePrinter.
Hi Joan,
I tryied your suggestion unsuccessfully (neither using AbortDoc in each fired event that follows StartDocPre nor avoiding to prompt for filename).

In other words I'm not able to stop the job process even if I use a static simple counter inside a StartPage, consider this other one:

Public Declare Function AbortDoc Lib "gdi32" (ByVal hdc As Long) As Long
Public nCurPage As Integer
Public nLastPage As Integer

Private Sub Command1_Click()
CDIntfEx1.PDFDriverInit (Text1.Text)
CDIntfEx1.FileNameOptionsEx = 35
CDIntfEx1.DefaultFileName = "c:\test.pdf"
CDIntfEx1.CaptureEvents (True)
nLastPage = 3 'For docs larger than 3 pages...
End Sub

Private Sub CDIntfEx1_EnabledPre()
r = CDIntfEx1.EnablePrinter <user>, <lic>
End Sub

Private Sub CDIntfEx1_StartDocPre()
nCurPage = 0
End Sub

Private Sub CDIntfEx1_StartPage(ByVal JobID As Long, ByVal hdc As Long)
nCurPage = nCurPage + 1
If nCurPage >= nLastPage Then
If AbortDoc(hdc) <= 0 Then
MsgBox ("Err pag: " & Str(nCurPage)) 'This Appears on page 3,4,5... until the end
End If
End If
End Sub

The only way I found, coming back on first example, is to by-pass the EnablePrinter (via OnEnablePre) in case of "cancel button" (the msg "device not ready" is less serious for end-user than "invalid handle").

Any other idea to prompt me ?
Thank you in advance.
Post Reply