1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
long bufsize=0x100;
char *prnname = "Brother MFC-8220 USB Printer"; //Guestgulans printer name
char buf[ 300];
int temp = GetLastError();
//Some text to write to the printer
char szData[512] ="AppWizard has created this print_Q_delete application for you.\
This file contains a summary of what you will find in each of the files that\
make up your print_Q_delete application.\
print_Q_delete.vcproj\
This is the main project file for VC++ projects generated using an Application Wizard.\
It contains information about the version of Visual C++ that generated the file, and\
information about the platforms, configurations, and project features selected with the\
Application Wizard. " ;
HANDLE hPrinter;
if ( OpenPrinter("Brother MFC-8220 USB Printer", &hPrinter, NULL) == 0 )
{
cout << "OpenPrinter call failed" << endl;
return 0;
}
DWORD dwBufsize=0;
DWORD dwBytesWritten=0;
DOC_INFO_1 DocInfo ={0};
DocInfo.pDocName = "realfile.txt";
DocInfo.pOutputFile = NULL;
DocInfo.pDatatype = _T("RAW");
//Start the print
StartDocPrinterA(hPrinter, 1, (LPBYTE)&DocInfo);
StartPagePrinter(hPrinter);
WritePrinter(hPrinter, (void*)szData, strlen(szData) , &dwBytesWritten);
//Finish printing
EndPagePrinter(hPrinter);
EndDocPrinter(hPrinter);
//Getting some info from printer - particularly how many jobs
GetPrinter(hPrinter, 2, NULL, 0, &dwBufsize);
PRINTER_INFO_2* pinfo = (PRINTER_INFO_2*)malloc(dwBufsize);
long result = GetPrinter(hPrinter, 2,(LPBYTE)pinfo, dwBufsize, &dwBufsize);
DWORD numJobs = pinfo->cJobs;
free(pinfo);//free now
if ( numJobs == 0)
{
cout << "No printer jobs found." << endl;
}
else //Some Jobs in queue
{
JOB_INFO_1 *pJobInfo=0;
DWORD bytesNeeded =0, jobsReturned=0;
//Get info about jobs in queue.
EnumJobs(hPrinter, 0, numJobs, 1, (LPBYTE)pJobInfo, 0,&bytesNeeded,&jobsReturned);
pJobInfo = (JOB_INFO_1*) malloc(bytesNeeded);
EnumJobs(hPrinter, 0, numJobs, 1, (LPBYTE)pJobInfo, bytesNeeded,&bytesNeeded,&jobsReturned);
//Loop and delete each waiting job
for(int count =0; count < jobsReturned; count ++)
{
cout << "Deleting JobID " << pJobInfo[count].JobId;
if ( SetJob(hPrinter, pJobInfo[count].JobId,0,NULL, JOB_CONTROL_DELETE) !=0)
{
cout << "...... Deleted OK" << endl;
}
else
{
cout << "...... Failed to Delete" << endl;
}
}
free(pJobInfo);//free now
}
//Finished with the printer
ClosePrinter( hPrinter );
return 0;
}
|