Printing To Windows Spooler

Started by
3 comments, last by Hacksaw2201 16 years, 2 months ago
Copied this straight from Microsoft's MSDN site. http://msdn2.microsoft.com/en-us/library/ms535786(VS.85).aspx Would someone please give me some kind of explanation that will help me understand why it doesn't work. I keep getting a message from the spooler that states "Local Downlevel Document"

int CPrinter::WriteBuffer(char* str)
{
	// clear out a print buffer
	memset(errBuffer, 0x20, MAX_LONG_STRING_LENGTH);

	// format the time for the log entry
	time.FormatTime(timeBuffer);

	// build the line
	int x = 0;
	while(timeBuffer[x] > 0x19)
	{
		errBuffer[x] = timeBuffer[x];
		x++;
	}

	x = 10;
	int y = 0;
	while(str[y] != 0x00)
	{
		errBuffer[x] = str[y];
		x++;
		y++;
	}

	errBuffer[x] = str[y];

    int	 bStatus = FALSE;
    HANDLE     hPrinter = NULL;
    DOC_INFO_1 DocInfo;
    DWORD      dwJob = 0L;
    DWORD      dwBytesWritten = 0L;

	char	szPrinterName[] = "HP Deskjet 3900 Series";

    // Open a handle to the printer.
    bStatus = OpenPrinter( (LPTSTR)szPrinterName, &hPrinter, NULL );
    if (bStatus) {
        // Fill in the structure with info about this "document."
		DocInfo.pDocName = (LPTSTR)_T("Flipidi Floop");
        DocInfo.pOutputFile = NULL;
        DocInfo.pDatatype = (LPTSTR)_T("RAW");

        // Inform the spooler the document is beginning.
        dwJob = StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo );
        if (dwJob > 0) {
            // Start a page.
            bStatus = StartPagePrinter( hPrinter );
            if (bStatus) {
                // Send the data to the printer.
//                bStatus = WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten);
                bStatus = WritePrinter( hPrinter, str, 1, &dwBytesWritten);
                bStatus = EndPagePrinter (hPrinter);
            }
            // Inform the spooler that the document is ending.
            bStatus = EndDocPrinter( hPrinter );
        }
        // Close the printer handle.
        ClosePrinter( hPrinter );
    }
    // Check to see if correct number of bytes were written.
//    if (!bStatus || (dwBytesWritten != dwCount)) {
    if (!bStatus || (dwBytesWritten != 1)) {
        bStatus = FALSE;
    } else {
        bStatus = TRUE;
    }

	return bStatus;

	return TRUE;
}

Advertisement
Quote:Original post by Hacksaw2201
Copied this straight from Microsoft's MSDN site.

http://msdn2.microsoft.com/en-us/library/ms535786(VS.85).aspx


Would someone please give me some kind of explanation that will help me understand why it doesn't work.

I keep getting a message from the spooler that states "Local Downlevel Document"
Where do you get that message? From the print spooler in the system tray? Is it an error? Do any of the function in the code you posted fail?
None of the commands failed. It comes from the system tray icon.

Give it a try. Copy in the code as it's what I got from the MSDN website so you know that's correct code.

Change this line to be YOUR printer:


char szPrinterName[] = "HP Deskjet 3900 Series";


call the routine:

Printer.WriteBuffer("A");




and see if it works for you.
The following works fine for me:
#include <windows.h>//// RawDataToPrinter - sends binary data directly to a printer// // szPrinterName: NULL-terminated string specifying printer name// lpData:        Pointer to raw data bytes// dwCount        Length of lpData in bytes// // Returns: TRUE for success, FALSE for failure.// BOOL RawDataToPrinter(LPTSTR szPrinterName, LPBYTE lpData, DWORD dwCount){    BOOL	 bStatus = FALSE;    HANDLE     hPrinter = NULL;    DOC_INFO_1 DocInfo;    DWORD      dwJob = 0L;    DWORD      dwBytesWritten = 0L;    // Open a handle to the printer.    bStatus = OpenPrinter( szPrinterName, &hPrinter, NULL );    if (bStatus) {        // Fill in the structure with info about this "document."        DocInfo.pDocName = "My Document";        DocInfo.pOutputFile = NULL;        DocInfo.pDatatype = "RAW";        // Inform the spooler the document is beginning.        dwJob = StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo );        if (dwJob > 0) {            // Start a page.            bStatus = StartPagePrinter( hPrinter );            if (bStatus) {                // Send the data to the printer.                bStatus = WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten);                EndPagePrinter (hPrinter);            }            // Inform the spooler that the document is ending.            EndDocPrinter( hPrinter );        }        // Close the printer handle.        ClosePrinter( hPrinter );    }    // Check to see if correct number of bytes were written.    if (!bStatus || (dwBytesWritten != dwCount)) {        bStatus = FALSE;    } else {        bStatus = TRUE;    }    return bStatus;}int main(){	RawDataToPrinter("HP Photosmart 2570 series", (BYTE*)"hello", 5);}

Although it only prints out "h" (Presumably because it's expecting raw data, not text).

EDIT: The following works fine for me too (Slight modification from your code to make it compile):
#include <windows.h>int WriteBuffer(char* str){    int	 bStatus = FALSE;    HANDLE     hPrinter = NULL;    DOC_INFO_1 DocInfo;    DWORD      dwJob = 0L;    DWORD      dwBytesWritten = 0L;	char	szPrinterName[] = "HP Photosmart 2570 series";    // Open a handle to the printer.    bStatus = OpenPrinter( (LPTSTR)szPrinterName, &hPrinter, NULL );    if (bStatus) {        // Fill in the structure with info about this "document."		DocInfo.pDocName = "Flipidi Floop";        DocInfo.pOutputFile = NULL;        DocInfo.pDatatype = "RAW";        // Inform the spooler the document is beginning.        dwJob = StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo );        if (dwJob > 0) {            // Start a page.            bStatus = StartPagePrinter( hPrinter );            if (bStatus) {                // Send the data to the printer.//                bStatus = WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten);                bStatus = WritePrinter( hPrinter, str, 1, &dwBytesWritten);                bStatus = EndPagePrinter (hPrinter);            }            // Inform the spooler that the document is ending.            bStatus = EndDocPrinter( hPrinter );        }        // Close the printer handle.        ClosePrinter( hPrinter );    }    // Check to see if correct number of bytes were written.//    if (!bStatus || (dwBytesWritten != dwCount)) {    if (!bStatus || (dwBytesWritten != 1)) {        bStatus = FALSE;    } else {        bStatus = TRUE;    }	return bStatus;}int main(){	WriteBuffer("hello");}
The number 1 after str is the amount of bytes to write hence only the h printing.


bStatus = WritePrinter( hPrinter, str, 1, &dwBytesWritten);



Is your printer a USB printer or are you using the parallel port?

[Edited by - Hacksaw2201 on February 16, 2008 2:45:03 PM]

This topic is closed to new replies.

Advertisement