Printer Driver Tips: Auto- Print module of the Resource Tool Kit

There are several pitfalls to Automatically Printing documents from MS Office by using the RTK BiAutoPrint.dll of the printer driver.

If you use the Black Ice BiAutoPrint dynamic link library or ActiveX control, you can print documents programmatically. The BiAutoPrint can handle several file formats, for example doc, xls, pps, pdf, html, txt, rtf, etc. You can add the BiAutoPrint module to your project easily and you can print documents without user interaction, so you can implement batch printing applications.

The BiAutoPrint uses third party applications for printing documents, for example if you want to print a file with the doc extension, the BiAutoPrint will print the document with Microsoft

Word. If you want to print PDF files, the BiAutoPrint starts Adobe Acrobat Reader for printing.

Because the BiAutoPrint starts another application, the application should be closed after printing. The BiAutoPrint contains a BIAPEndPrinting method for closing the printing application. You should call this method, when the printing is finished. When the Black Ice printer sends the
BLACKICE_MESSAGE_ENDDOC message, the BIAPEndPrinting method call won’t close the printing application, because the printing didn’t finish yet completely.
 

The BiAutoPrint sample contains a method for checking the spooler of the printer. If the specified job is removed from the spooler, or the spooler is getting empty, you can call the BIAPEndPrinting method to close the printing application. In this case the printing application (for example Microsoft Word) can be closed correctly.

The following code snippets show you how to get the count of the jobs in the spooler:
 

[C++]

int CAutoPrintDlg::JobsCount()
{
HANDLE hPrinter;
DWORD dwNeeded = 0;
BOOL bFlag;
int iRet;
PRINTER_INFO_2 *ppi2 = NULL;


bFlag = OpenPrinter(m_szActualPrnName, &hPrinter, NULL);
if (bFlag && hPrinter)
{
SetLastError(0);
bFlag = GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);

if (!bFlag)
{
if ((GetLastError() != ERROR_INSUFFICIENT_BUFFER) || (dwNeeded == 0))
{
ClosePrinter(hPrinter);
return TRUE;
}
}

// Allocate enough space for PRINTER_INFO_2.
ppi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
if (!ppi2)
{
ClosePrinter(hPrinter);
return TRUE;
}
// The second GetPrinter() will fill in all the current information
// so that all you have to do is modify what you are interested in.
bFlag = GetPrinter(hPrinter, 2, (LPBYTE)ppi2, dwNeeded, &dwNeeded);
if (!bFlag)
{
ClosePrinter(hPrinter);
GlobalFree(ppi2);
return TRUE;
}

iRet = ppi2->cJobs;

GlobalFree(ppi2);
ClosePrinter(hPrinter);
}

return iRet;
}
[VB]

Public Function JobsCount(PrinterName As String) As Long
Dim hPrinter As Long
Dim ret As Long
Dim lJobsCount As Long
Dim pinfo2 As PRINTER_INFO_2
Dim bufferSize As Long
Dim prn() As Long

lJobsCount = 0

ret = OpenPrinter(PrinterName, hPrinter, ByVal CLng(0))

If (ret > 0) And (hPrinter > 0) Then
'The first GetPrinter() tells you how big our buffer must
'be to hold ALL of PRINTER_INFO_2.
ret = GetPrinter(hPrinter, 2, ByVal 0&, 0, bufferSize)
'ret = 0 or
If bufferSize = 0 Then
ClosePrinter (hPrinter)
JobsCount = 0
Exit Function
End If

ReDim prn((bufferSize \ 4)) As Long

ret = GetPrinter(hPrinter, 2, prn(0), bufferSize, bufferSize)
If ret = 0 Then
ClosePrinter (hPrinter)
JobsCount = 0
Exit Function
End If

ClosePrinter (hPrinter)

lJobsCount = prn(19)
End If

JobsCount = lJobsCount
End Function
Page 1 Page 2 Page 3