共用方式為


新增插斷事件支援

若要正確設定 WIA 驅動程式以報告中斷事件,請執行下列動作:

  1. 在裝置的 INF 檔案中設定 Capabilities=0x31 。 (如需詳細資訊,請參閱 WIA 裝置的 INF 檔案 。)

  2. IStiUSD::GetCapabilities方法中的報表STI_GENCAP_NOTIFICATIONS和STI_USD_GENCAP_NATIVE_PUSHSUPPORT。

  3. 報告 IWiaMiniDrv::d rvGetCapabilities 方法中的所有支援事件。

  4. 快取並使用 IStiUSD::SetNotificationHandle 方法中傳遞的事件控制碼。 這是裝置訊號或 WIA 迷你驅動程式直接使用SetEvent (的事件控制碼,如Microsoft Windows SDK檔) 中所述。 在此方法中,您會起始 WIA 裝置的等候狀態。

  5. IStiUSD::GetNotificationData 方法中報告適當的事件資訊回應。

下列兩個範例示範如何設定裝置以實作 IWiaMiniDrv::d rvGetCapabilitiesIStiUSD::SetNotificationHandle 方法的中斷。

注意 請務必將重迭的 I/O 呼叫與涉及核心模式驅動程式的所有活動搭配使用。 這允許適當的逾時和取消裝置要求。

IWiaMiniDrv::d rvGetCapabilities 實作的說明

WIA 服務會呼叫 IWiaMiniDrv::d rvGetCapabilities 方法來取得 WIA 裝置支援的事件和命令。 WIA 驅動程式應該先查看傳入 lFlags 參數,以判斷它應該回應的要求。

WIA 驅動程式應該配置記憶體 (供 WIA 驅動程式使用,並) 釋放,以包含 WIA_DEV_CAP_DRV 結構的陣列。 在 IWiaMiniDrv::d rvGetCapabilities的呼叫中,將指標傳遞至存放 ppCapabilities 參數中 WIA 驅動程式配置記憶體位址的記憶體位置。

注意 WIA 服務不會釋放此記憶體。 WIA 驅動程式必須管理配置的記憶體。

下列範例示範 IWiaMiniDrv::d rvGetCapabilities 方法的實作。

HRESULT _stdcall CWIADevice::drvGetCapabilities(
  BYTE            *pWiasContext,
  LONG            lFlags,
  LONG            *pcelt,
  WIA_DEV_CAP_DRV **ppCapabilities,
  LONG            *plDevErrVal)
{
  //
  // If the caller did not pass in the correct parameters,
  //  then fail the call and return E_INVALIDARG.
  //

  if (!pWiasContext) {

    //
    // The WIA service may pass in a NULL for the pWiasContext. 
    // This is expected because there is a case where no item 
    // was created at the time the event was fired.
    //
  }

  if (!plDevErrVal) {
      return E_INVALIDARG;
  }

  if (!pcelt) {
      return E_INVALIDARG;
  }

  if (!ppCapabilities) {
      return E_INVALIDARG;
  }

  *plDevErrVal = 0;

  HRESULT hr = S_OK;

  LONG lNumberOfCommands = 1;
  LONG lNumberOfEvents   = 2;

  //
  // initialize WIA driver capabilities ARRAY
  // a member WIA_DEV_CAP_DRV m_Capabilities[3] variable
  // This memory should live with the WIA minidriver.
  // A pointer to this structure is given to the WIA service using
  // ppCapabilities.  Do not delete this memory until
  // the WIA minidriver has been unloaded.
  //

  // This ARRAY should only be initialized once.
  // The Descriptions and Names should be read from the proper
  // string resource.  These string values should be localized in
  // multiple languages because an application will be use them to
  // be displayed to the user.
  //

  // Command #1
  m_Capabilities[0].wszDescription =   L"Synchronize Command";
  m_Capabilities[0].wszName = L"Synchronize";
  m_Capabilities[0].guid    = (GUID*)&WIA_CMD_SYNCHRONIZE;
  m_Capabilities[0].lFlags = 0;
  m_Capabilities[0].wszIcon = WIA_ICON_SYNCHRONIZE;

  // Event #1
  m_Capabilities[1].wszDescription = L"Scan Button";
  m_Capabilities[1].wszName = L"Scan";
  m_Capabilities[1].guid    = (GUID*)&WIA_EVENT_SCAN_IMAGE;
  m_Capabilities[1].lFlags = WIA_NOTIFICATION_EVENT | WIA_ACTION_EVENT;
  m_Capabilities[1].wszIcon = WIA_ICON_SCAN_BUTTON_PRESS;

  // Event #2
  m_Capabilities[2].wszDescription = L"Copy Button";
  m_Capabilities[2].wszName = L"Copy";
  m_Capabilities[2].guid    = (GUID*)&WIA_EVENT_SCAN_PRINT_IMAGE;
  m_Capabilities[2].lFlags = WIA_NOTIFICATION_EVENT | WIA_ACTION_EVENT;
  m_Capabilities[2].wszIcon = WIA_ICON_SCAN_BUTTON_PRESS;


  //
  //  Return depends on flags.  Flags specify whether we should return
  //  commands, events, or both.
  //
  //

  switch (lFlags) {
  case WIA_DEVICE_COMMANDS:

    //
    //  report commands only
    //

    *pcelt          = lNumberOfCommands;
    *ppCapabilities = &m_Capabilities[0];
    break;
  case WIA_DEVICE_EVENTS:

    //
    //  report events only
    //

    *pcelt          = lNumberOfEvents;
    *ppCapabilities = &m_Capabilities[1]; // start at the first event in the ARRAY
    break;
  case (WIA_DEVICE_COMMANDS | WIA_DEVICE_EVENTS):

    //
    //  report both events and commands
    //

     *pcelt          = (lNumberOfCommands + lNumberOfEvents);
     *ppCapabilities = &m_Capabilities[0];
     break;
  default:

    //
    //  invalid request
    //
    hr = E_INVALIDARG;
    break;
  }

  return hr;
}

IStiUSD::SetNotificationHandle方法是由 WIA 服務或在此驅動程式內部呼叫,以啟動或停止事件通知。 WIA 服務會傳入有效的控制碼,該控制碼會使用 Microsoft Windows SDK 檔) 中所述的CreateEvent (所建立,指出 WIA 驅動程式是在硬體中發生事件時發出此控制碼的訊號。

Null 可以傳遞至 IStiUSD::SetNotificationHandle 方法。 Null 表示 WIA 迷你驅動程式是停止所有裝置活動,並結束任何事件等候作業。

下列範例示範 IStiUSD::SetNotificationHandle 方法的實作。

STDMETHODIMP CWIADevice::SetNotificationHandle(HANDLE hEvent)
{
  HRESULT hr = S_OK;

  if (hEvent && (hEvent != INVALID_HANDLE_VALUE)) {

    //
    // A valid handle indicates that we are asked to start our "wait"
    // for device interrupt events
    //

    //
    // reset last event GUID to GUID_NULL
    //

    m_guidLastEvent = GUID_NULL;

    //
    // clear EventOverlapped structure
    //

    memset(&m_EventOverlapped,0,sizeof(m_EventOverlapped));

    //
    // fill overlapped hEvent member with the event passed in by 
    // the WIA service. This handle will be automatically signaled
    //  when an event is triggered at the hardware level.
    //

    m_EventOverlapped.hEvent = hEvent;

    //
    // clear event data buffer.  This is the buffer that will be used
    //  to determine what event was signaled from the device.
    //

    memset(m_EventData,0,sizeof(m_EventData));

    //
    // use the following call for interrupt events on your device
    //

    DWORD dwError = 0;
    BOOL bResult = DeviceIoControl( m_hDeviceDataHandle,
                                    IOCTL_WAIT_ON_DEVICE_EVENT,
                                    NULL,
                                    0,
                                    &m_EventData,
                                    sizeof(m_EventData),
                                    &dwError,
                                    &m_EventOverlapped );

    if (bResult) {
        hr = S_OK;
    } else {
        hr = HRESULT_FROM_WIN32(::GetLastError());
    }

  } else {

    //
    // stop any hardware waiting events here, the WIA service has
    // notified us to stop all hardware event waiting
    //

    //
    // Stop hardware interrupt events. This will stop all activity on
    // the device. Since DeviceIOControl was used with OVERLAPPED i/o 
    // functionality the CancelIo() can be used to stop all kernel
    // mode activity.
    //


    if(m_hDeviceDataHandle){
        if(!CancelIo(m_hDeviceDataHandle)){

            //
            // canceling of the IO failed, call GetLastError() here to determine the cause.
            //

            LONG lError = ::GetLastError();

        }
    }
  }
  return hr;
}

當 WIA 迷你驅動程式或 WIA 裝置偵測到併發出事件訊號時,WIA 服務會呼叫 IStiUSD::GetNotificationData 方法。 在此方法中,WIA 迷你驅動程式應該報告所發生事件的詳細資料。

WIA 服務會呼叫 IStiUSD::GetNotificationData 方法,以取得剛收到訊號的事件相關資訊。 您可以呼叫 IStiUSD::GetNotificationData方法,做為兩個事件作業之一的結果。

  1. IStiUSD::GetStatusSTI_DEVICE_STATUS 結構中設定STI_EVENTHANDLING_PENDING旗標,回報有擱置中的事件。

  2. IStiUSD::SetNotificationHandle傳入的hEvent控制碼是由硬體發出訊號,或呼叫 Microsoft Windows SDK 檔) 中所述的SetEvent (。

WIA 驅動程式負責填寫 STIFY 結構

下列範例示範 IStiUSD::GetNotificationData 方法的實作。

STDMETHODIMP CWIADevice::GetNotificationData( LPSTINOTIFY pBuffer )
{
  //
  // If the caller did not pass in the correct parameters,
  // then fail the call with E_INVALIDARG.
  //

  if(!pBuffer){
      return E_INVALIDARG;
  }
 
  GUID guidEvent = GUID_NULL;
  DWORD dwBytesRet = 0;
  BOOL bResult = GetOverlappedResult(m_hDeviceDataHandle, &m_EventOverlapped, &dwBytesRet, FALSE );
  if (bResult) {
    //
    // read the m_EventData buffer to determine the proper event.
    // set guidEvent to the proper event GUID
    // set guidEvent to GUID_NULL when an event has
    // not happened that you are concerned with
    //

    if(m_EventData[0] == DEVICE_SCAN_BUTTON_PRESSED) {
       guidEvent = WIA_EVENT_SCAN_IMAGE;
    } else {
       guidEvent = GUID_NULL;
    }
  }

  //
  // If the event was triggered, then fill in the STINOTIFY structure
  // with the proper event information
  //

  if (guidEvent != GUID_NULL) {
    memset(pBuffer,0,sizeof(STINOTIFY));
    pBuffer->dwSize               = sizeof(STINOTIFY);
    pBuffer->guidNotificationCode = guidEvent;        
  } else {
    return STIERR_NOEVENTS;
  }

  return S_OK;
}

只要傳遞 Null 做為事件控制碼,即可隨時停止中斷事件。 迷你驅動程式應該將此解譯為訊號,以停止硬體裝置上的任何等候狀態。

IWiaMiniDrv::d rvNotifyPnpEvent方法可以接收影響事件等候狀態的電源管理事件。

WIA 服務會呼叫 IWiaMiniDrv::d rvNotifyPnpEvent 方法,並在系統即將進入睡眠狀態時傳送WIA_EVENT_POWER_SUSPEND事件。 如果發生此呼叫,裝置可能已經離開其等候狀態。 睡眠狀態會自動觸發核心模式驅動程式以結束任何等候狀態,以允許系統進入此關機狀態。 當系統從其睡眠狀態繼續時,WIA 服務會傳送WIA_EVENT_POWER_RESUME事件。 此時,WIA 迷你驅動程式必須重新建立中斷事件等候狀態。 如需睡眠狀態的詳細資訊,請參閱 系統電源狀態裝置電源狀態

建議 WIA 迷你驅動程式快取事件控制碼一開始傳遞至 IStiUSD::SetNotificationHandle 方法,以便在系統從睡眠或休眠喚醒時重複使用。

WIA 服務 不會 在系統繼續之後呼叫 IStiUSD::SetNotificationHandle 方法。 建議迷你驅動程式呼叫其 IStiUSD::SetNotificationHandle 方法,並傳遞快取的事件控制碼。

WIA 服務會在發生系統事件時呼叫 IWiaMiniDrv::d rvNotifyPnpEvent 方法。 WIA 驅動程式應該檢查 pEventGUID 參數,以判斷正在處理的事件。

必須處理的一些常見事件如下:

WIA_EVENT_POWER_SUSPEND
系統會進入暫停/睡眠模式。

WIA_EVENT_POWER_RESUME
系統會從暫停/睡眠模式喚醒。

WIA 驅動程式應該在從暫停返回之後還原任何事件中斷等候狀態。 這可確保當系統喚醒時,事件仍會正常運作。

下列範例示範 IWiaMiniDrv::d rvNotifyPnpEvent 方法的實作。

HRESULT _stdcall CWIADevice::drvNotifyPnpEvent(
  const GUID *pEventGUID,
  BSTR       bstrDeviceID,
  ULONG      ulReserved)
{
  //
  // If the caller did not pass in the correct parameters,
  // then fail the call with E_INVALIDARG.
  //

  if ((!pEventGUID)||(!bstrDeviceID)) {
      return E_INVALIDARG;
  }

  HRESULT hr = S_OK;

  if(*pEventGUID == WIA_EVENT_POWER_SUSPEND) {

    //
    // disable any driver activity to make sure we properly
    // shut down (the driver is not being unloaded, just disabled)
    //

  } else if(*pEventGUID == WIA_EVENT_POWER_RESUME) {

    //
    // reestablish any event notifications to make sure we properly
    // set up any event waiting status using the WIA service supplied
    // event handle
    //

    if(m_EventOverlapped.hEvent) {

      //
      // call ourselves with the cached EVENT handle given to
      // the WIA driver by the WIA service.
      //

        SetNotificationHandle(m_EventOverlapped.hEvent);
    }
  }
  return hr;
}