1

I use IWebBrowser2 in my simple win32-application. I want programatically get and set printer settings like page size or page orientation.

I have found a lot of examples with using C# or VB, but no one for C++.

Perfect variant would be with using some COM-interface.

1 Answers1

2

As you speak of COM-interface, I assume that you do not want NET functions but winAPI ones.

Here is a first glance view of referenced page:

  • EnumPrinters allows to get the list of printers - unsure whether you need it...
  • OpenPrinter give you a handle to a specific printer

    BOOL OpenPrinter(
      LPTSTR pPrinterName,         // printer or server name
      LPHANDLE phPrinter,          // printer or server handle
      LPPRINTER_DEFAULTS pDefault  // printer defaults
    );
    
  • GetPrinter gives you many information on a printer, and specifically the PRINTER_INFO_9 structure specifying the per-user default printer settings.

    BOOL GetPrinter(
      HANDLE hPrinter,    // handle to printer
      DWORD Level,        // information level (9 to get the PRINTER_INFO_9)
      LPBYTE pPrinter,    // printer information buffer
      DWORD cbBuf,        // size of buffer
      LPDWORD pcbNeeded   // bytes received or required
    );
    typedef struct _PRINTER_INFO_9 {
      LPDEVMODE pDevMode;   // contains actual setting like orientation...
    } PRINTER_INFO_9, *PPRINTER_INFO_9;
    
  • SetPrinter allows to set configuration back to printer

    BOOL SetPrinter(
      HANDLE hPrinter,  // handle to printer object
      DWORD Level,      // information level (9 to use the PRINTER_INFO_9)
      LPBYTE pPrinter,  // printer data buffer
      DWORD Command     // printer-state command
    );
    

You can then use the StartDoc, EndDoc calls to do actual printing enclosing each page with StartPage EndPage, optionnaly regestering an AbortProc procedure. Alternatively, you I think that you can use the configured printer through IWebBrowser2 interface but I never used that part.

I know that this is still far for a real example of configuring a printer and using it, but at least it should give enough hints and pointers.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Thanks, it was helpful. Unfortunately, I have a problem with EnumPrinters, because it returns only "Microsoft XPS Document Writer". And GetPrinter for this returns empty structure. I'll try to solve this. Another one point, at msdn written that PRINTER_INFO_9 is a level for default(!) user settings. Do you know, will some changes in UI affect on return of GetPrinter? – Константин Савельев Mar 30 '16 at 14:22