Thread: How to print and delete the printer's job

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    4

    How to print and delete the printer's job

    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.

    Code:
    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;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    First, you need an indent style which isn't NONE
    Indent style - Wikipedia, the free encyclopedia

    Second, with only one job in the queue, chances are it has already been sent to the printer and you're too late (especially if it's a small text file).

    Try queuing several files and see if you can delete some of those.

    You might also clarify "it doesn't work".
    What are your actual observations / error messages.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    It is compiling fine. But does nothing when I run this program. So if I need to print more files from particular folder say "D:\printer files" then what change should I make in this line of code. " DocInfo.pDocName = "realfile.txt"; "

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    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.
    Last edited by Ananth_304; 07-16-2010 at 02:13 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Read our FAQ on getting a list of files in a directory.

    Then use a loop to pass each filename where you have your assignment.

    Dunno - this seems like a very easy step compared to what you've done already.
    Are you roaming the forums accumulating other peoples work without actually understanding anything which is going on?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    I didn't understand what you saying. It shows the message box saying "SetPrinter call failed". So it is satisfying the condition " if ( SetPrinter(hPrinter, 0, 0, PRINTER_CONTROL_PURGE) == 0 ) ". But I need the else part of this condition to get executed. So please give me a solution for this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 05-17-2010, 08:12 PM
  2. Structs, dynamic memory, and phone book entries
    By wkfcs in forum C Programming
    Replies: 5
    Last Post: 10-09-2009, 03:57 PM
  3. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  4. How to delete part of an array?
    By Ash1981 in forum C Programming
    Replies: 11
    Last Post: 01-01-2006, 06:06 PM
  5. Delete & print func()
    By spentdome in forum C Programming
    Replies: 5
    Last Post: 05-29-2002, 09:02 AM

Tags for this Thread