- Joined
- Mar 29, 2011
- Messages
- 1
- Reaction score
- 0
Hi All,
I am not sure if this is the right forum to post this question. If it isn't please guide me to the correct one.
I am using WritePrinter (Win32 API) to write RAW data to printer. For testing I am just writing a constant string (actually I want to print a PDF). Everything goes fine, but the printing doesn't happen at all. The print queue shows the job with status "Printing" and it doesn't change. The spoolsv.exe will then take up 98% of the CPU usage.
Below is my code. Can please somebody help me out..
All the Win32 API calls are returning success values.
I am not sure if this is the right forum to post this question. If it isn't please guide me to the correct one.
I am using WritePrinter (Win32 API) to write RAW data to printer. For testing I am just writing a constant string (actually I want to print a PDF). Everything goes fine, but the printing doesn't happen at all. The print queue shows the job with status "Printing" and it doesn't change. The spoolsv.exe will then take up 98% of the CPU usage.
Below is my code. Can please somebody help me out..
Code:
#include "stdafx.h"
#pragma hdrstop
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <fstream>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <winspool.h>
int _tmain(int argc, _TCHAR* argv[])
{
int i;
char *szPrinterName ="doPDFv7";
int Sts = ERROR_SUCCESS
;
DWORD dwJobHandle, dwJobInfo2Len, dwJobNumber, dwBytesWritten;
char cDocInfo1[ 10*1024 ] = {0}, cJobInfo2[ 10*1024 ] = {0};
char szHelloWorld[] = "Hello World!\f";
HANDLE hPrinter;
PRINTER_DEFAULTS sPrinterDefaults = {0};
DOC_INFO_1 psDocInfo1 = {0};
JOB_INFO_2 *psJobInfo2;
sPrinterDefaults.DesiredAccess = PRINTER_ACCESS_ADMINISTER;
// Open the Printer
if ( ! OpenPrinter( szPrinterName, &hPrinter, &sPrinterDefaults ) )
{
printf("Error in OpenPrinter");
return 0;
}
printf("Got handle to printer\n");
char *fileName = "c:\\shy.txt";
// Set up the information needed to start the job
psDocInfo1.pDocName = (LPTSTR)"c:\\test.log";
psDocInfo1.pDatatype = _T("RAW");
psDocInfo1.pOutputFile = NULL;
if ( ! ( dwJobHandle = StartDocPrinter( hPrinter, 1, (PBYTE)&psDocInfo1) ) )
{
DWORD lastError = GetLastError();
char buf[10];
sprintf(buf, "%d", lastError);
printf(buf);
printf("\n");
char buf2[10];
sprintf(buf2, "%d", dwJobHandle);
printf(buf2);
printf("\n");
ClosePrinter( hPrinter );
printf("Error while StartDocPrinter");
return 0;//( Sts );
}
// Prepare to get the information for the job
psJobInfo2 = (JOB_INFO_2 *)cJobInfo2;
printf("Out of StartDocPrinter\n");
// Get the information for the Job
if ( ! GetJob( hPrinter, dwJobHandle, 2, (unsigned char *)psJobInfo2, sizeof( cJobInfo2 ), &dwJobInfo2Len ) )
{
ClosePrinter( hPrinter );
printf("Error while GetJob");
return 0;
}
// Return some information about the job to the caller
dwJobNumber = psJobInfo2->JobId;
// Prepare to set specific information about the job
psJobInfo2->Position = JOB_POSITION_UNSPECIFIED;
psJobInfo2->pUserName = "test_user";
// Set the Owner of the print job
if ( ! SetJob( hPrinter, dwJobHandle, 2, (unsigned char *)psJobInfo2, 0 ) )
{
ClosePrinter( hPrinter );
printf("Error while SETJOB");
return 0;
}
StartPagePrinter(hPrinter);
printf("\nPrinting started...");
// Write something to the print job
if ( ! WritePrinter( hPrinter, szHelloWorld, strlen( szHelloWorld ), &dwBytesWritten ) )
{
ClosePrinter( hPrinter );
printf("Error while WritePrinter");
return 0;
}
EndPagePrinter(hPrinter);
// Call EndDocPrinter to start the job
if ( ! EndDocPrinter( hPrinter ) )
{
ClosePrinter( hPrinter );
printf("Error while EndDocPrinter");
return 0;
}
printf("\nEndDocPrinter done");
// Close the printer handle, we are done!
if ( ! ClosePrinter( hPrinter ) )
{
return 0;
}
printf("\nPrinter closed\n");
return(0);
}
All the Win32 API calls are returning success values.