How to delete the job from the printer

Pages: 12
Hi All,

The program to print and delete the data is working fine. But I tried the next step.
i.e., With the same program I tried to alter the function block such that I have to send the data to printer manually from a particular folder and delete it from the print spooler using the logic from the program which I attached here. But getting lots of unsolved errors. Can you please give the solution for this..
I have attached the code below...
What I mean to say here is, suppose I got text files from two drives D and F i.e., "D:\\folder name\filename.txt", "F:\\folder name\filename.txt" and I will send these two text files to printer manually. Now I need to delete only D drive's text file but not F drive's file. How to do this programmatically.


Thanks in advance.....


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
#include <iostream>
#include <fstream>
#include <string>


using namespace std;


int main(int argc, char* argv[])
{

    long bufsize=0x100;
    char *prnname = "Brother MFC-8220 USB Printer"; //Guestgulkans printer name

    HANDLE hPrinter;
    if ( OpenPrinter("Brother MFC-8220 USB Printer", &hPrinter, NULL) == 0 )
    {
        cout << "OpenPrinter call failed" << endl;

        return 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:\\some_folder\\"); //The folder where the text files are kept

    fHandle = FindFirstFile(_T( (dirName + "*.txt").c_str()),&fData);

    if (fHandle != INVALID_HANDLE_VALUE)
    {

        ifstream f;
        string str;
        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 (str.length() > 0)
            {
                WritePrinter(hPrinter, (LPVOID)str.c_str(), str.length() , &dwBytesWritten);
            }

            //Finish printing
            str.clear();
            EndPagePrinter(hPrinter);
            EndDocPrinter(hPrinter);

            DeleteFile((dirName+fData.cFileName).c_str());

        }while( FindNextFileA(fHandle,&fData) != FALSE);

        FindClose(fHandle);


    }//If valid find handle


    //Finished with the printer
    ClosePrinter( hPrinter );

    return 0;  

}
Last edited on
Can anybody please help....? Am trying to fix this but its not happening.. So please help me.....
The above code only looped througn a single directory.
What does your current code look like?

Hi guestgulkan,

Thanks for your reply.. Good to see you again. The code above functions perfectly. But clears all the jobs from the print spooler. i.e., Suppose there is a text file in F drive and am sending that text file manually to printer to print. And of course, if I run this program then "D:\\some_folder\\" text file (line number 30) will be send to printer.
( Though the program prints and deletes the job by itself once I run it , I have done some modification so that I got two button functions which does the print and delete job. And I have used same concept,functions and variables ).

So there will be two text files in a printer, one from F drive and one from D drive. If I click the "delete" button then it clears all the text files from the printer according to this programming concept. But I need only D drive text file get cleared not F drive's text file which I sent manually. Can you please help..?
Hi guestgulkan,

Can you please help me with this issue..?
Post the code you currently have.

I'll look at it and maybe make some suggesstions over the weekend.
Hi guestgulkan,

I have attached the code here....





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);
}
//---------------------------------------------------------------------------
Hi ,
Sorry but i am not able to understand that how the job of particular drive is recognize in the above code . Please let me know your comments on this .

Just to confirm something - You do know that there is no need to actually remove jobs from the printer queue by youself.
When you send a job to the printer and it prints ok, it will automatically be reomved from the printer queue.
You only need to remove them if there is a problem with printing annd the printer queue is becoming
full of stuck jobs.

We put in that part of the code because you asked for it.
Last edited on
Hi guestgulkan,

You are absolutely correct. Well,, let me explain the scenario,what exactly it is so that you can understand the problem exactly.

Am working in a product based company as a software developer with few months of experience. We have a product called Polytech. It a LIS(Laboratory Information System) product which keeps all the details of the patient,doctors, treatments and so on.. This product also got the privilege to take the print out of the details. So the client can send the files apart from Polytech files also to the printer to take the print outs. Suppose, the client suddenly turns his mind decides to clear the Polytech files from the printer which is waiting for the print. At that time it has to clear only the Polytech files not all the files from the printer. Am working on this program only..

One more thing is, Once the client installs our product it will be in C drive with the folder name called Polytech. So this will be having the entire details about the print files, faxes and so on. So my program has to delete files only from the folder Polyetch which is sent to the printer. Not any other file. This is exactly what I needed. Please help me...
Hi guestgulkan,

Hope, you understood the concept behind my program. If so, can you please help me..?


Thanks in advance.....
One more thing is, Once the client installs our product it will be in C drive with the folder name called Polytech. So this will be having the entire details about the print files, faxes and so on. So my program has to delete files only from the folder Polyetch which is sent to the printer. Not any other file. This is exactly what I needed. Please help me...



It seems to me that a simple way to do this would be to make a sub-folder specially for documents to be printed.
So if your main folder is called c:\polytech , then make a folder c:\polytech\print Move the files which have been chosen for printing into this subfolder.
Print the files.
Then when the files have been printed, then we can simply delete the files from the c:\polytech\print folder.

This seems a straight forward method to me.

Just out of interest how is the user selecting what files they want to print - are they selecting
one file at a time a prssing a print button - or care they selecting a bunch of files at a time?
Last edited on
Hi guestgulkan

Users selects bunch of files at a time as there are number of medical related transcriptions.
It is an excellent idea. But the requirement what I got is, it has to delete from the print spooler. That to only polytech related files not other files. And it should not delete any files from the folder.. Everything working fine with this program. The only problem am facing here is, the delete function clears all the files from the print spooler which is not expected. Please help me....
The only problem am facing here is, the delete function clears all the files from the print spooler which is not expected. Please help me....


The StartDoc function returns an integer value - this value is the print job identifier - if the start Document started successfully yhis value is greater than 0.

What you could do is is a vector (of integers) and each time a succesfull ptint job starts you save the print job number in the vector, then at the end you go through the vector and
do a do a delete for every printjob id that was saved.
Hi guestgulkan,

I don't know the concept of vectors since am new to this language. But I tried to understand the concept through google. Am not getting the exact idea of how to do. In fact I scribbled my head with this for whole the day. Am not getting how to start with. If you don't mind can you add the concept what you said to the code...? Please show your kindness.....

Thank you.....
Hi guestgulkan,

Please help me...
OK - post the code you have now lets see what we can do with it.
(But still for the life of me I cannot understand why you are sending jobs to the printer, then turning around and deleting those jobs from the printer - see the last code you posted - these jobs would not have had time to print)
Last edited on
Hi guestgulkan,

Am really sorry for the delay in reply. For sending jobs I just tried from my personnel interest with your help. Actual thing what should happen in my code is it has to delete files from the printer which is sent from particular folder only. it should not delete rest of the files from spooler. This is my actual thing. Rest I tried for my curiosity.

As I told, it has to delete only "C:\\Polytech" folder files only. It should not delete any other files. Please help me.. I have attached the code here.

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

//---------------------------------------------------------------------------

#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("C:\\Polytech\\"); //The folder where the text files are kept
     fHandle = FindFirstFile(_T( (dirName + "*.txt").c_str()),&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);
}
//---------------------------------------------------------------------------


Last edited on
Hi guestgulkan,

I have posted the code.. Please help with some solution........
Thanks - give me a couple of days - busy at the moment.
Pages: 12