Link to home
Start Free TrialLog in
Avatar of gilles2
gilles2

asked on

Info on WINSPOOL.PAS

I need to count the pages printed from a PC. Printing is done from delphi applications I wrote, so I have control of th e print button.  The problem is that some of the printing is done from a WebBrowser component for which I only call a method Print. In other words I can code that runs when the user clicks on the print button. It looks like there is enough functionality in WINSPOOL.PAS to do this querying info from Print MAnager.  Does anybody know how to do this ?  I am thinkg of a function that would read info from print manager right after the print button is clicked. Events would be even better.

Thank you.
Avatar of inter
inter
Flag of Türkiye image

Yes your are right, the info can be retreived using the

BOOL GetJob(
    HANDLE hPrinter,      // handle of printer
    DWORD JobId,      // job identifier value
    DWORD Level,      // data-structure level
    LPBYTE pJob,      // address of data-structure array
    DWORD cbBuf,      // count of bytes in array
    LPDWORD pcbNeeded );

before you should open the printer with OpenPrinter and havi the hPrinter handle. If I have a printer at hand I can test this. Anyway after succesfully opening the printer you call
var
  JobInfo :TJobInfo1;
  needed : DWORD;
begin
  OpenPrinter('Hp1',hPrinter,nil);
  if hPrinter <> 0 then
  begin
    GetJob(hPrinter, 1, 1, JobInfo, Sizeof(JobInfo), needed);
  end;
  ClosePrinter(hPrinter);
end;

upon return JobInfo has the following info init:
  TJobInfo1A = record
   JobId: DWORD;
   pPrinterName: PAnsiChar;
   pMachineName: PAnsiChar;
   pUserName: PAnsiChar;
   pDocument: PAnsiChar;
   pDatatype: PAnsiChar;
   pStatus: PAnsiChar;
   Status: DWORD;
   Priority: DWORD;
   Position: DWORD;
   TotalPages: DWORD;  <- What you want
   PagesPrinted: DWORD;
   Submitted: TSystemTime;
  end;
But we should have some delay if you can print in background...
regards, igor
Avatar of gilles2
gilles2

ASKER

Thank you. That's exacly what I am looking for. I am having problems with


    GetJob(hPrinter, 1, 1, JobInfo, Sizeof(JobInfo), needed);

It expects a pointer for JobInfo. I tried just putting @ in front of it. It then compiles but crashes at runtime. Any suggestion ?

ASKER CERTIFIED SOLUTION
Avatar of inter
inter
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gilles2

ASKER

Thank you. It works. My only problem now is that I have to capture the info at the right time. Some jobs are in and out in no time. The option I am looking at right now is to use the message WM_SpoolerStatus(var Msg: TWMSPOOLERSTATUS); message WM_SPOOLERSTATUS;

This is called when jobs are added or removed from the queue. If the idle state of the printer is 'Paused' then I can print, the method WM_SpoolerStatus gets called when the job is added to the spooler, I get the job info, restart the printer. The job then prints. When it is finished printing (WM_SpoolerStatus is called again and I can see that Msg.JobsLeft has decreased) then I pause the printer again.

Does it make sense to you ?

Of course the problem now is how do I pause and restart the printer.

Thank you.
Avatar of gilles2

ASKER

Thank you. I got everything working now. Using the pausing seems to work.
Hi,
Sorry friend I was out at weekend. Thanks, now here for antthing...
regards, igor