How to print and delete all the jobs from the printer

Hi All,

I am new to this forum and also to C++ program. I tried a program to send the jobs to the printer and to delete the jobs from the printer. For this I have used win32 API. But it doesn't work. I did this with the help of Borland builder. I have attached part of the code which would help you to find the solution. I need the solution soon since am in urgent.. Please help me with this concept.


long bufsize=0x100;
char *prnname = "Canon MF4320-4350";
char buf[ 300];
int temp = GetLastError();
char szData[512];
GetProfileStringA("Windows", "Device", ",,,",
prnname, bufsize);


HANDLE hPrinter;
if ( OpenPrinter("Canon MF4320-4350", &hPrinter, NULL) == 0 )
{
free(prnname);
ShowMessage("OpenPrinter call failed");

return 0;
}

DWORD dwBufsize=0;
DWORD dwBytesWritten=0;

DOC_INFO_1 DocInfo;

DocInfo.pDocName = "realfile.txt";
DocInfo.pOutputFile = NULL;
DocInfo.pDatatype = _T("RAW");


StartDocPrinter(hPrinter, 1, (LPBYTE)&DocInfo);
StartPagePrinter(hPrinter);


WritePrinter(hPrinter, (void*)szData, strlen(szData) , &dwBytesWritten);

GetPrinter(hPrinter, 2, NULL, 0, &dwBufsize);

PRINTER_INFO_2* pinfo = (PRINTER_INFO_2*)malloc(dwBufsize);
long result = GetPrinter(hPrinter, 2,
(LPBYTE)pinfo, dwBufsize, &dwBufsize);

if ( pinfo->cJobs == 0 )
{
ShowMessage("No printer jobs found.");
sprintf( buf, "%d", temp);
Form1->RichEdit1->Lines->Add( buf);
}
else
{
if ( SetPrinter(hPrinter, 0, 0, PRINTER_CONTROL_PURGE) == 0 )
{
ShowMessage("SetPrinter call failed");
sprintf( buf, "%d", temp);
Form1->RichEdit1->Lines->Add( buf);
}
else
{
ShowMessage("Number of printer jobs deleted");
sprintf( buf,"%d", pinfo->cJobs);
Form1->RichEdit1->Lines->Add( buf);
free(pinfo);
free(prnname);
}
}

ClosePrinter( hPrinter );
free(pinfo);
free(prnname);

return 0;
}
-nevermind-


Also what errors are you getting?

btw use [ code] ... [ /code] tags
Last edited on
Hi,,

Thank you so much for your reply. And am sorry for not using [code] tags.
Before I made this change I wasn't getting any error. It was compiling fine. But now I got the error " Cannot convert 'unsigned long' to 'char[512]' " for the change which I made to szData
Sorry nevermind what i said i was being a little stupid >.<

Anyway, do you get errors when you run it? Or does it just do nothing at all?
Hi,

Thats okay...Not a problem.. Yes, it is not giving any error. At the same time it is not working fine. In fact it does nothing though it compiles fine.. I doubt, whether any misplace of API in my code... Can I have your suggestion or solution....?
I apologize for the wrong information in the previous reply. Once I compile the program and run the program it pops up the message box saying " "SetPrinter call failed " after I hit the Button.
Please help me.. I need the solution badly......
I tried with some extra method to trace out the error using GetLastError to see why it is showing the message box saying "SetPrinter call failed ". It returned me error number 6. So it means invalid handler. So what could be the problem in my program.?
Using the SetPrinter function to cancel the printjobs did not work for me (using Windows 7) - it always came back with Error Code 5 (Access Denied).

So I tried the the SetJob function - that works for me with no problem.

SetJob (with the JOB_CONTROL_DELETE command) only deletes a job at a time.
To delete all the jobs - you can use EnumJobs function to get a list of jobs and then delete them
using a loop.

**By the way in tour code - you should not free(prnname) because it was not allocated using malloc**
Last edited on
Proof of concept:


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;  

}
Hi,,

Thank you so much for the reply. I was really worried like why nobody responding but once I saw your response i got my confidence back. Thousands of thanks to you.. I will try out the solution which you gave and I let you know the feedback.

Thanks once again......
Hi,,

Thank you so much Sir.. It is working fine without any problem..

I need one more favor. This program is to send a raw data but how to send and delete the files of particular directory. Suppose say, in D drive and in folder "printfiles" whatever text file I send it should show in print spooler and should get deleted. I know some alteration as to be done in line number 30 i.e., DocInfo.pDocName = "realfile.txt";...But I don't know what exactly to be done.. Can you please help..?
What something like this??
(print the files in a given directory, then delete them from the directory)

A working Proof of concept example - obviously not the final polished article.
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;  

}
Hi,,

Yes.. Exactly..... Thank you so much. Everything is working fine... Thanks for your help..
Topic archived. No new replies allowed.