Intercept the print command

Posted on 2007-07-31
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

     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

    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

     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).
    LVL 9

    Expert Comment

    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

    Thanks, I will try this and see if it helps my cause
    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    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…
    Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now