none
V4 Printer Extension C++ RRS feed

  • Question

  • I'm trying to create a v4 Printer Extension that displays a MFC dialog using C++.

    I compile PrinterExtension.idl from WDK, then use code like below to get the Printer Manger.

      #include <initguid.h>
      #include "PrinterExtension_i.c"
      #include "PrinterExtension_h.h"

      #include "PrintManagerInterop_i.c"
      #include "PrintManagerInterop_h.h"

      
    HRESULT hr;
     hr = CoInitialize(NULL);
     CComPtr<IPrinterExtensionManager>  pIPrinterExtensionManager;
     hr = ::CoCreateInstance(
      CLSID_PrinterExtensionManager,
      NULL,
      CLSCTX_SERVER,
      //CLSCTX_INPROC_SERVER,
      IID_IPrinterExtensionManager,
      reinterpret_cast<LPVOID*>(&pIPrinterExtensionManager)
     );    //This succeeds

     CComPtr<IDispatch> pIDispatch;

     hr = pIPrinterExtensionManager->QueryInterface(IID_IDispatch, reinterpret_cast<LPVOID*>(&pIDispatch));

    // This fails, hr = 0x80004002 :  (E_NOINTERFACE)


     CComPtr<IPrinterExtensionEvent> pIPrinterExtensionEvent;
     hr = pIPrinterExtensionManager->QueryInterface(IID_IPrinterExtensionEvent, reinterpret_cast<LPVOID*>(&pIPrinterExtensionEvent));

    // This fails, hr = 0x80004002 :  (E_NOINTERFACE)

    How do I get access to the IPrinterExtensionEvent::OnDriverEvent so that I can override with my Event Handler?

    PrinterExtension.idl lists IPrinterExtensionEvent as a dispatched interface:

      [
       uuid(331B60DA-9E90-4DD0-9C84-EAC4E659B61F),
      ]
      coclass PrinterExtensionManager
      {
       [default] interface      IPrinterExtensionManager;
       [source]  dispinterface  IPrinterExtensionEvent;
      };

    I'm new to this so do appreciate your suggestions.

    • Edited by PLimburg Tuesday, August 8, 2017 3:56 PM
    Monday, August 7, 2017 5:18 PM

Answers

  • After some study, here is the outline of my successful solution to the issue (no error checking, etc.):

    // Create PrintExtensionManager as above

    // GetIConnectionPointContainer
    CComPtr<IConnectionPointContainer> pIConnectionPointContainer;

    hr = m_pIPrinterExtensionManager->QueryInterface(IID_IConnectionPointContainer, reinterpret_cast<LPVOID*>(&pIConnectionPointContainer));

    // Get the IID_IPrinterExtensionEvent connection point
    IConnectionPoint m_pEventConnectionPoint:
    hr = pIConnectionPointContainer->FindConnectionPoint(IID_IPrinterExtensionEvent, &m_pEventConnectionPoint);
    m_pEventConnectionPoint->AddRef();

    // Create CMyPrinterExtensionEventReceiver object that implements IPrinterExtensionEvent including rudimentary IDispatch
    // (The declaration and implementation of CMyPrinterExtensionEventReceiver is elsewhere)
    CMyPrinterExtensionEventReceiver * m_pPrinterExtensionEventReceiver = new CPrinterExtensionEventReceiver

    // Connect our Event Receiver to the connection point
    hr  = m_pEventConnectionPoint->Advise(reinterpret_cast<IUnknown *>(m_pPrinterExtensionEventReceiver), &m_EventConnectionCookie);

    // Enable events in the PrinterExtension Manager
    hr  = m_pIPrinterExtensionManager->EnableEvents(GUID_PrinterDriver);

    // Then respond to events using the methods in CMyPrinterExtensionEventReceiver

    Thursday, August 17, 2017 10:29 PM