USB Monitoring Control - ActiveX Component Library for USB Data Monitoring, API for Software Developers
Download USB Monitoring Control Hide this button

How to Receive Monitored Events

This section describes the steps you need to carry in order to receive monitored events in your code.

  1. Obtain the Monitor object, as described in this tutorial.

  2. Add your listener object (for native code) to the Monitor object or connect event handlers (for managed code):

    class CMyListener : public CComObjectRoot<CMyListener>, public INativeListener
    {
    public:
        // override all pure virtual methods in the INativeListener
        STDMETHOD(OnConnection)(FILETIME *,BOOL,LPCTSTR);
        STDMETHOD(OnGetDescriptorFromDevice)(FILETIME *fTime,void* Data,ULONG Size,BYTE Index,BYTE DescriptorType,USHORT LanguageId);
        // ...
    };
    
    ...
    
    CComObject<CMyListener> pMyListenerObject;
    CComObject<CMyListener>::CreateInstance(&pMyListenerObject);
    CComPtr<INativeListener> pMyListener;
    pMyListenerObject->QueryInterface(&pMyListener);
    pMonitor->AddNativeListener(pMyListener);
    
    monitor.OnConnection += new hhdusbmcLib._IMonitoringEvents_OnConnectionEventHandler(monitor_OnConnection);
    monitor.OnPacketUp += new hhdusbmcLib._IMonitoringEvents_OnPacketUpEventHandler(monitor_OnPacketUp);
    monitor.OnPacketDown += new hhdusbmcLib._IMonitoringEvents_OnPacketDownEventHandler(monitor_OnPacketDown);
    ...
    
  3. Attach the Monitor object to the USB device:

    • Attach to the given USB device:

      pMonitor->Connect(CComVariant(pDevice), /*headers-only*/ CComVariant(VARIANT_FALSE));
      
      hhdusbmcLib.Device device  = monitor.Devices[nIndex];
      monitor.Connect(device, /*headers-only*/ false);
      
    • Attach to the next USB device the user plugs into the computer:

      CComPtr<IDeviceCollection> pDeviceCollection;
      HRESULT hr = m_pUM->get_Devices(&pDeviceCollection);
      if(FAILED(hr))
          return hr;
      
      CComPtr<IDevice> pDevice;
      // first item is always NextConnected
      pDeviceCollection->get_Item(CComVariant(0),&pDevice);
      
      pMonitor->Connect(pDevice, /*headers-only*/ CComVariant(VARIANT_FALSE));
      
      // first device is always NextConnected device
      hhdusbmcLib.Device device  = monitor.Devices[0];
      monitor.Connect(device, /*headers-only*/ false);