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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <vcl.h>
//#include "stdafx.h"
#include <windows.h>
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <winspool.h>
#pragma hdrstop
#include "print.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
int dbmain(boolean val)
{
long bufsize=0x100;
//char *prnname = "Brother MFC-8220 USB Printer"; //Guestgulans printer name
char *prnname = " Canon MF4320-4350 ";
char buf[ 300];
int temp = GetLastError();
ifstream f;
string str;
//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("Canon MF4320-4350", &hPrinter, NULL) == 0 )
{
//cout << "OpenPrinter call failed" << endl;
ShowMessage( "OpenPrinter call failed");
return 0;
}
DWORD dwBufsize=0;
DWORD dwBytesWritten=0;
DOC_INFO_1 DocInfo ={0};
DocInfo.pOutputFile = NULL;
DocInfo.pDatatype = _T("text");//text format
HANDLE fHandle;
WIN32_FIND_DATA fData = {0};
string dirName("D:\\printfile\\"); //The folder where the text files are kept
//DocInfo.pDocName = "D:\\printfile\\";
fHandle = FindFirstFile(_T( (dirName + "*.txt").c_str()),&fData);
//fHandle = FindFirstFile("D:\\printfile\\*.txt" ,&fData);
do
{
f.open( (dirName+fData.cFileName).c_str());
if (f.is_open())
{
string ss;
while ( getline(f,ss))
{
ss += "\n";
str += ss;
}
f.close();
}
//Start the print
DocInfo.pDocName = _T(fData.cFileName );//Set document title to the current filename (without path)
StartDocPrinter(hPrinter, 1, (LPBYTE)&DocInfo);
StartPagePrinter(hPrinter);
//DWORD dwBytesWritten=0;
if(val == 0)
{
if (str.length() > 0)
{
WritePrinter(hPrinter, (LPVOID)str.c_str(), str.length() , &dwBytesWritten);
ShowMessage("Data is sent");
}
}
//Finish printing
EndPagePrinter(hPrinter);
EndDocPrinter(hPrinter);
//DeleteFile((dirName+fData.cFileName).c_str());
} while( FindNextFileA(fHandle,&fData) != FALSE);
//Getting some info from printer - particularly how many jobs
if( val == 1)
{
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;
ShowMessage("No printer jobs found.");
}
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);
//DeletePrinterDataEx( hPrinter, fData.cFileName, NULL);
//Loop and delete each waiting job
for(int count =0; count < jobsReturned; count ++)
{
//cout << "Deleting JobID " << pJobInfo[count].JobId;
//sprintf( buf,"Deleting JobID",pJobInfo[count].JobId);
//Form1->RichEdit1->Lines->Add( buf);
// int count;
if ( SetJob(hPrinter, pJobInfo[count].JobId,0,NULL, JOB_CONTROL_DELETE) ==0)
{
//cout << "...... Deleted OK" << endl;
ShowMessage( "...... Deleted OK");
}
else
{
//cout << "...... Failed to Delete" << endl;
ShowMessage("...... Deleted OK");
}
}
free(pJobInfo);//free now
}
}
//Finished with the printer
ClosePrinter( hPrinter );
return 0;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
dbmain(0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
dbmain( 1);
}
//---------------------------------------------------------------------------
|