How to delete the job from the printer

Pages: 12
Hi guestgulkan,

That should be fine....
Are you using an old compiler - because I see that you used the old style C++ standard library
header file name (using the .h extension) like
#include <iostream.h>

Tidy it up as you need to suit your compiler

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
//---------------------------------------------------------------------------

#include <vcl.h>

#pragma hdrstop
#include <vcl.h>
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>

#include <winspool.h>
#pragma hdrstop

using namespace std;//*************

#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 = " Canon MF4320-4350 ";
    int temp = GetLastError();
    ifstream f;
    string str;
    vector<DWORD>jobList;//*************************


    HANDLE hPrinter;
    if ( OpenPrinter(prnname, &hPrinter, NULL) == 0 )
    {
        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(_T("C:\\Polytech\\")); //The folder where the text files are kept
    fHandle = FindFirstFile( (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
        //We will record the ID number of each print job as it is printed************************

        DWORD jobNum;//*************************
        
        DocInfo.pDocName = _T(fData.cFileName );//Set document title to the  current filename (without path)

        
        jobNum = StartDocPrinter(hPrinter, 1, (LPBYTE)&DocInfo);//********************
        jobList.push_back(jobNum);//****************************************
        
        StartPagePrinter(hPrinter);

        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);
    } while( FindNextFileA(fHandle,&fData) != FALSE);



    //************Deleting jobs using thelist of jobs that was save earlier
    if (val ==1)
    {
        vector<DWORD>::iterator it;
        for (it = jobList.begin(); it != jobList.end(); ++it)
        {
            if ( SetJob(hPrinter, *it,0,NULL, JOB_CONTROL_DELETE) ==FALSE)
            {
                ShowMessage( "...... not Deleted OK");
            }
            else
            {
                ShowMessage("...... Deleted OK");
            }         

        }
    }

    //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 guestgulkan,

I removed all those header files(iostream,stdio,conio,stdlib) and I tried to work with the program which u sent. And thank you so much for your help. Am really thankful to you for spending much time with me.

But am really sorry to say this. When I worked with the program,it is sending the files to the printer but delete function doesn't working perfectly. Let me brief you with the example.

I have 4 files in C:\\Polytech, file1.txt, file2.txt, file3.txt, file4.txt. when I click the button (Button1Click() function) it is sending all these files to the printer. But when I tried to delete the files using other button (Button2Click() function),message box appears saying "Deleted OK". The moment it appears it is sending three more files to the printer i.e., file2.txt, file3.txt, file4.txt (not "file1.txt").So totally 7 files and when I click "ok" in the message box it deletes only 3 files (file2.txt, file3.txt, file4.txt) from the printer and rest of the files will remain as it is. It is not clearing it. Here I didn't understand how the delete button also sending the files to printer and deleting only those files which it sent. Can you please help me with this issue.

Thanks in advance........
Hi guestgulkan,

Can I have suggestion from you for this problem..?
Hi guestgulkan,

I tried a lot but still getting the same result.. Can you please help me...?
Hi guestgulkan,

I don't understand though level and pjob parameter is set to 0 and NULL, how the setJob function sending the files to printer and deleting only those files which it sent.. Please help me with this issue..?
Hi guestgulkan,

I removed all those header files(iostream,stdio,conio,stdlib) and I tried to work with the program which u sent. And thank you so much for your help. Am really thankful to you for spending much time with me.

But am really sorry to say this. When I worked with the program,it is sending the files to the printer but delete function doesn't working perfectly. Let me brief you with the example.

I have 4 files in C:\\Polytech, file1.txt, file2.txt, file3.txt, file4.txt. when I click the button (Button1Click() function) it is sending all these files to the printer. But when I tried to delete the files using other button (Button2Click() function),message box appears saying "Deleted OK". The moment it appears it is sending three more files to the printer i.e., file2.txt, file3.txt, file4.txt (not "file1.txt").So totally 7 files and when I click "ok" in the message box it deletes only 3 files (file2.txt, file3.txt, file4.txt) from the printer and rest of the files will remain as it is. It is not clearing it. Here I didn't understand how the delete button also sending the files to printer and deleting only those files which it sent. Can you please help me with this issue.


In your code you obviously have two buttons on your form and you have this code:
1
2
3
4
5
6
7
8
9
10
11
void __fastcall TForm1::Button1Click(TObject *Sender)
{
dbmain(0);    
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
dbmain( 1);
}
//--------------------------------------------------------------------------- 


As you can see each time any of the two buttons is clicked, it calls the dbmain function.
One calls it with 0 and the other calls it with value 1.
Only when you call it with value 1 does it delete the files from the spooler. When you call it with value 0 it does not delete the files from the spooler.
You see what I mean??
So what do you want to do.?

(I notice that the buttons are called Button1 and Button2 - maybe you should give them more
meaningfull names?)
Hi guestgulkan,

Button1 is for sending the files to the printer and Button2 is for deleting the files, which we have sent. I don't have any problem with Button1. Its working fine. But Button2 is not working as it is intended to work. Fine, I will give some meaningful names to the Buttons.


Here is the part of the code..
1
2
3
4
5
6
7
8
9
10
11
12

void __fastcall TForm1::sendButton(TObject *Sender)
{
dbmain(0);    
}
//---------------------------------------------------------------------------

void __fastcall TForm1::clearSpoolerButton(TObject *Sender)
{
dbmain( 1);
}


As I mentioned earlier the clearSpoolerButton again sending the same files which sendButton has done and deleting those files which clearSpoolerButton has sent and leaving remaining files which sendButton has sent. Can you please help me with this issue.....?
Last edited on
How does this work for you.
(Are you trying to program this all by yourself??)

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
//---------------------------------------------------------------------------

#include <vcl.h>

#pragma hdrstop
#include <vcl.h>
#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>
#pragma hdrstop

using namespace std;//*************

#include "print.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------


int dbmain(boolean val)
{
    
    static vector<DWORD>jobList;//****************************

    
    
    long bufsize=0x100;
    char *prnname ="Brother MFC-8220 USB Printer";// " Canon MF4320-4350 "; //************
    int temp = GetLastError();
    ifstream f;
    string str;


    //We will always need the handle to the printer   
    HANDLE hPrinter;
    if ( OpenPrinter(prnname, &hPrinter, NULL) == 0 )
    {
        ShowMessage( "OpenPrinter call failed");
        return 0;
    }

 
    if (val ==0) //If val is 0 we only want to print.
    {

        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(_T("C:\\Polytech\\")); //The folder where the text files are kept
        fHandle = FindFirstFile( (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
            //We will record the ID number of each print job as it is printed

            DWORD jobNum;

            DocInfo.pDocName = _T(fData.cFileName );//Set document title to the  current filename (without path)

            jobNum = StartDocPrinter(hPrinter, 1, (LPBYTE)&DocInfo);
            jobList.push_back(jobNum);//

            StartPagePrinter(hPrinter);

            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);
        } while( FindNextFile(fHandle,&fData) != FALSE);

    }

    //************Deleting jobs using thelist of jobs that was save earlier
    else if (val ==1) // val
    {
        {

            stringstream ss;
            vector<DWORD>::iterator it;
            for (it = jobList.begin(); it != jobList.end(); ++it)
            {
                ss << *it;

                if ( SetJob(hPrinter, *it,0,NULL, JOB_CONTROL_DELETE) ==FALSE)
                {
                    ShowMessage( (ss.str() + "...... not Deleted OK").c_str());
                }
                else
                {
                    ShowMessage( (ss.str() + "...... Deleted OK").c_str());
                }

                ss.str(string());

            }
        }
    }

    
    //In all cases  - Finished with the printer
    ClosePrinter( hPrinter );


    return 0;

}


void __fastcall TForm1::sendButton(TObject *Sender)
{
dbmain(0);    
}
//---------------------------------------------------------------------------

void __fastcall TForm1::clearSpoolerButton(TObject *Sender)
{
dbmain( 1);
}

Last edited on
Hi guestgulkan,

Yes.. Am trying all these... Even am using the google to find out the solution. But am failing all the time. And thanks for your reply. I will try this and I will let you know the result soon....
Hi guestgulkan,

Thanks for your help. It is working fine. No problem in this. Am really sorry for disturbing you a lot. Please don't mistake me. Am just testing the different possibilities. I just tried to send the text files manually which is in "C:\\Polytech" and used the delete button from the program to delete the files. But its not deleting it. I traced the program completely. No where the send and delete buttons are linked. It has to delete any text files from that directory if it is send through manually or using send button. But its not happening. Why so..? Can you please help me..?

Correct me if am wrong anywhere... Thank you
Hi guestgulkan,

Am sorry. It is linked. I got it... If am not wrong, because of following two operations the delete button is not deleting the files which is sent manually

jobNum = StartDocPrinter(hPrinter, 1, (LPBYTE)&DocInfo);
jobList.push_back(jobNum);

If thats case, how to delete the C:\\Polytech files, though it is sent manually.... Am sorry for troubling you much. I just tested this with different possibilities and found this problem. Please help me...?
Topic archived. No new replies allowed.
Pages: 12