[Last Call] Learn how to a build a cloud-first strategyRegister Now


Intercept the print command

Posted on 2007-07-31
Medium Priority
Last Modified: 2013-11-22
I am currently using XP pro and Delphi 7 pro. I need to know how to intercept the print command. Basically I tried to create a message handler for the INIT_DIALOG handler but it doesn't seem to work.
It needs to detect whenever you click print, weather you use the dialog command or just print.
Can anyone help me. Please.
Question by:freelancedev
LVL 21

Accepted Solution

developmentguru earned 2000 total points
ID: 19602973
 I think you may be able to replace the print dialog in Windows by registering your own... Not everything uses a print dialog though.  I don't think you can force other software to use a print dialog if it is not designed to.  Word has a little printer icon and it prints directly to the default printer.  Do not pass "print dialog", do not collect 200 dollars.  I think in order to get that level of control you would need to hook the windows printing system (create your own DLL with all of the same calls, pass all of the calls on to the current - renamed -  DLL while reacting to the ones you want to).  This would have dangers of it's own (Windows updates would rewrite your version).  I am curious why you want this, heavy handed, level of control.

Author Comment

ID: 19603062
I won't be able to change  any files on the users PC only add my own so I cannot change the Dll.
The reason for this is I have a client and I am busy with a program for them but they want a mesasge box to intercept any print command so that they can change the default printer for a single print but the users do not have access for this so the app needs to set a new printer for that single print and then change it back to the original. The setting and resetting of the printer I have done but like I said my problems is detecting printing.
Hope this helps
LVL 21

Expert Comment

ID: 19608209
 If they want it for specific applications it may be possible... Office products etc.  Like I said, the problem is that each application can print in an independant manner.  In the case of the MS Office products they may be able to be scripted to intercept a print.  Then it is being handled from within the application.

  I did some more research and this linnk may help.  It discusses the calls that individual printer driver manufacturers may support.  You would have to enumerate all of the print devices on your system and try to use the most basic (OEMStartDoc?).


  The problem with this approach is that the program has likely already retrieved information about the printer that it will use to format it's output.  If you then redirect this to another printer the results could very well be skewed.

  If you are only worried about when an application brings up the print dialog you can hook it.  You will likely need to look into the print dialogs Delphi sets up to see how they interface with Windows. Here is the link.


Let me know if any of this helps (or just jogs loose more info I could use).
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 20019677
Check out this post

Try the PurgeJobsOnCurrentPrinter procedure given below. Not tested!

Uses Winspool, printers;

{-- GetCurrentPrinterHandle -------------------------------------------}
{: Retrieves the handle of the current printer
@Returns an API printer handle for the current printer
@Desc Uses WinSpool.OpenPrinter to get a printer handle. The caller
  takes ownership of the handle and <b>must</b> call ClosePrinter on it
  once the handle is no longer needed. Failing to do that creates a
  serious resource leak! <P>
  Requires Printers and WinSpool in the Uses clause.
@Raises EWin32Error if the OpenPrinter call fails.
}{ Created 30.9.2000 by P. Below

Function GetCurrentPrinterHandle: THandle;
    Defaults: TPrinterDefaults = (
      pDatatype : nil;
      pDevMode  : nil;
    Device, Driver, Port : array[0..255] of char;
    hDeviceMode: THandle;
  Begin { GetCurrentPrinterHandle }
    Printer.GetPrinter(Device, Driver, Port, hDeviceMode);
    If not OpenPrinter(@Device, Result, @Defaults) Then
  End; { GetCurrentPrinterHandle }

{: Kill all pending jobs on the current printer }
Procedure PurgeJobsOnCurrentPrinter;
    hPrinter: THandle;
    hPrinter:= GetCurrentPrinterHandle;
      If not WinSpool.SetPrinter( hPrinter, 0, nil, PRINTER_CONTROL_PURGE )
      ClosePrinter( hPrinter );
  End; { PurgeJobsOnCurrentPrinter }


Author Comment

ID: 20020525
Thanks, I will try this and see if it helps my cause

Expert Comment

ID: 21216470
Forced accept.

EE Admin

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month17 days, 17 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question