Click here to Skip to main content
15,900,108 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

now i have code that Monitors Printed job but it is Using Polling Method so that it is using 100% CPU . Now I need a code that Monitors Printed Jobs by events not by using Polling Method so that it can very efficient .......The code wt i have is as Follow............

#include "stdafx.h"
#include <windows.h>
#include <winerror.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <tchar.h>
#define MAX_PRINTERS 256
#define dwFlags PRINTER_ENUM_CONNECTIONS | PRINTER_ENUM_LOCAL
 
int main(int argc, char * argv[])
{
      HANDLE                  hPrinter[MAX_PRINTERS];
      DWORD                     pcbNeeded=0,pcReturned=0,dwBytesNeeded=0,dwReturned=0;
      JOB_INFO_2 *         pJobInfo = NULL;
      PRINTER_INFO_4 *   pinfo4 = NULL;
      int                        previous[MAX_PRINTERS], i =0;
      SYSTEMTIME            st;
     LPWSTR fp;
     HANDLE handle;
     DWORD s = 0;
     char NO_OF_COPIES = 0;
     TCHAR msg[142];
     DWORD NO_OF_PRINTS = NULL;
     DWORD NO_OF_BYTES = NULL;
 
    
     handle = CreateFile(L"D:\\PrinterDatabase.txt",FILE_APPEND_DATA, FILE_SHARE_READ | FILE_SHARE_WRITE,
                                  NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
                         SetFilePointer ( handle, 1, 0, FILE_BEGIN );
    
      for (i=0; i<MAX_PRINTERS; i++)
     {
          previous[i]=0;
     }
     
        
      while ( !EnumPrinters (dwFlags, NULL, 4, (LPBYTE) pinfo4, dwBytesNeeded,
                        &dwBytesNeeded, &dwReturned) )
      {
         
            if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
            {
              
                  if (pinfo4)
                    LocalFree (pinfo4);
              
               pinfo4 = (PRINTER_INFO_4 *) LocalAlloc (LPTR, dwBytesNeeded);
            }
            else
            {
               MessageBoxW(NULL, L"EnumPrinters failed: ", L"INFO", MB_ICONWARNING);
                  return 1;
            }
      }
    
      printf("\n*******************************************************\n\n");
    
      printf("# OF PRINTERS (Local/Networked) INSTALLED ON YOUR MACHINE = %lu\n",
                  dwReturned);
     printf("\n Press any key if you want to quit\n\n");
     
                 
      for (i = 0; i < (int)dwReturned; i++)
      {
            if ( !OpenPrinter( pinfo4[i].pPrinterName, (LPHANDLE)&hPrinter[i],
                  (LPPRINTER_DEFAULTS)NULL) )
            {
              
               MessageBoxW (NULL, L"OpenPrinter failed", L"INFO", MB_ICONWARNING);     
                  return 1;
            }
      }
     
  
     
      while (!kbhit())
                              // Keep polling forever and dump the info whenever a
                              // job is submitted to any printer.
      {
           
            for(i=0; i<(int)dwReturned; i++) // Number of printers installed.
            {                          
                             
                  while ( !EnumJobs((HANDLE)hPrinter[i], 0, 1, 2, (LPBYTE)pJobInfo,
                        pcbNeeded, (LPDWORD)&pcbNeeded, (LPDWORD)&pcReturned) )
                  {                          
                       
                        if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
                        {
                        
                              if (pJobInfo) LocalFree(pJobInfo);
                              pJobInfo = (JOB_INFO_2 *) LocalAlloc(LPTR, pcbNeeded);
                        
                        }
                        else
                        {
                        
                         MessageBoxW (NULL, L"EnumJobs on printer failed ", L"INFO", MB_ICONWARNING);
                              goto Cleanup;
                        }
                  }
                 
                 
                  if (pcReturned > 0) // There is a JOB to print from printer[i]
                  {                       
                       
                        if ((int)pJobInfo->JobId != previous[i])
                        {
                         char *Statement = "\r\n**********************************PRINTER INFORMATION *****************************";
                              WriteFile ( handle, Statement, strlen ( Statement ) + 28, &NO_OF_BYTES, NULL );
                         char *Jobid = "\r\nJOBID :";
 
                        
                         char Jobinfo_ID[5] = {0};
                         itoa ( pJobInfo->JobId, Jobinfo_ID, 10 );
                         WriteFile ( handle, Jobid, strlen(Jobid) , &NO_OF_BYTES, NULL );
                              WriteFile ( handle, LPCTSTR(Jobinfo_ID), strlen ( Jobinfo_ID ), &NO_OF_BYTES, NULL );
 
                        
 
                         char *PrinterName = "\r\r\nPrinter Name   :";
                         WriteFile ( handle, PrinterName, strlen ( PrinterName ), &NO_OF_BYTES, NULL );
                         WriteFile ( handle, (pJobInfo->pPrinterName), strlen ( (char *)(pJobInfo->pPrinterName) ) + 56 , &NO_OF_BYTES, NULL );
 
                          
                         char *MachineName = "\r\nMachineName :";
                         WriteFile ( handle, MachineName, strlen ( MachineName ), &NO_OF_BYTES, NULL   );
                         WriteFile ( handle, pJobInfo->pMachineName, strlen ( (char *)(pJobInfo->pMachineName) ) + 25, &NO_OF_BYTES, NULL );
                             
                         char *UserName = "\r\nUserName      :";
                         WriteFile ( handle, UserName, strlen ( UserName ), &NO_OF_BYTES, NULL   );
                         WriteFile ( handle, pJobInfo->pUserName, strlen ( (char *)(pJobInfo->pUserName) ) + 12, &NO_OF_BYTES, NULL );
                             
                         char *DocumentName = "\r\nDocumentName   :";
                         WriteFile ( handle, DocumentName, strlen ( DocumentName ), &NO_OF_BYTES, NULL   );
                         WriteFile ( handle, pJobInfo->pDocument, strlen ( (char *)(pJobInfo->pDocument) ) + 58, &NO_OF_BYTES, NULL );
 
                         if(pJobInfo->pDevMode->dmFields & DM_COPIES)
                             
                         itoa (pJobInfo->pDevMode->dmCopies, &NO_OF_COPIES, 10);
                                
                         char *Copies = "\r\nNo Of Copies :";
                         WriteFile ( handle, Copies, strlen ( Copies ), &NO_OF_BYTES, NULL );
                         char dmCopies[3] = {0};
                         itoa ( pJobInfo->pDevMode->dmCopies, dmCopies, 10 );
                         WriteFile ( handle, dmCopies, strlen (dmCopies) + 2, &NO_OF_BYTES, NULL );
                       
                        
                        
                        
                             
 
                             if (handle != INVALID_HANDLE_VALUE)
                                  MessageBoxW (NULL,L"file creation Success ",L"info",MB_OKCANCEL);
         
                             else
                              MessageBoxA (NULL,"file creation failed ","info",MB_ICONWARNING);
 
                        
                        
                                      
                              GetLocalTime (&st);   /* Get Local time */
                             
                              //printf("Date                  : %d/%d/%d Time                  : %d:%02d:%02d:%02d\n",
                         char date[20] = {0};
                         char Month[4] = {0};
                         itoa ( st.wMonth, Month, 10 );
                         char Day[4] = {0};
                         itoa ( st.wDay, Day, 10 );
                         char Year[6] = {0};
                         itoa ( st.wYear, Year, 10 );
                         strcat ( date, Month );
                         strcat ( date, "-");
                         strcat ( date, Day );
                         strcat ( date, "-");
                         strcat ( date, Year );
                         char *dated = "\r\nDate :";
                         WriteFile ( handle, dated, strlen( dated ) , &NO_OF_BYTES, NULL );
                         WriteFile ( handle, date, strlen ( date ) + 6, &NO_OF_BYTES, NULL );
                                
                         char Time[20] = {0};
                         char Minute[4] = {0};
                         itoa ( st.wMinute, Minute, 10 );
                         char Seconds[4] = {0};
                         itoa ( st.wSecond, Seconds, 10 );
                         char hour[6] = {0};
                         itoa ( st.wHour, hour, 10 );
                         strcat ( Time, hour );
                         strcat ( Time, ":");
                         strcat ( Time, Minute );
                         strcat ( Time, ":");
                         strcat ( Time, Seconds );
                        
                        
                         char *Timed = "\r\nTime :";
                         WriteFile ( handle, Timed, strlen ( Timed ), &NO_OF_BYTES, NULL );
                         WriteFile ( handle, Time, strlen ( Time ) + 6, &NO_OF_BYTES, NULL );
                        
                         printf("\n Press any key if you want to quit\n\n");
                        }                       
                  }           
            }
      }
     
      getch ();
 
Cleanup :
      LocalFree (pinfo4); LocalFree (pJobInfo); //cleanup memory!
     
      for (i=0; i< (int)dwReturned; i++){
            if ( !ClosePrinter( (HANDLE)hPrinter[i] ) )
            {
               MessageBoxW (NULL , L"ClosePrinter failed", L"INFO", MB_ICONWARNING);
                  return 1;
            }
      }
      return 0;
}

Please Help me soon...

Thanks
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900