若要正確設定 WIA 驅動程式以報告輪詢事件,請執行下列動作:
在裝置的 INF 檔案中設定 Capabilities=0x33 。 (如需詳細資訊,請參閱 WIA 裝置的 INF 檔案 。
在 IStiUSD::GetCapabilities 方法中報告STI_GENCAP_NOTIFICATIONS和STI_USD_GENCAP_NATIVE_PUSHSUPPORT。
報告 IWiaMiniDrv::drvGetCapabilities 方法中的所有支援事件。
回應 IStiUSD::GetStatus 方法的呼叫。 WIA 服務會以可在 INF 檔案中設定的預設間隔呼叫此方法。 默認設定為 1 秒間隔。
在 IStiUSD::GetNotificationData 方法中報告適當的事件信息回應。
WIA 服務會針對兩個主要作業來呼叫 IStiUSD::GetStatus 方法:
檢查裝置的在線狀態。
查詢或監控裝置事件,例如按鈕事件。
藉由檢查STI_DEVICE_STATUS結構的 StatusMask 成員,即可判斷作業要求。 StatusMask 成員可以是下列其中一個請求:
STI_DEVSTATUS_ONLINE_STATE (在線狀態)
此作業要求透過設定 STI_DEVICE_STATUS 結構的 dwOnlinesState 成員,來檢查裝置是否在線上,應予填寫。
STI_設備狀態_事件_狀態
此作業請求將檢查裝置事件。 應該藉由設定STI_DEVICE_STATUS結構的 dwEventHandlingState 成員來填入它。 應該使用的值是STI_EVENTHANDLING_PENDING。 (裝置有擱置中的事件,並正在等候向 WIA 服務報告。
設定STI_EVENTHANDLING_PENDING時,WIA 服務會發出訊號,表示 WIA 驅動程式中已發生事件。 WIA 服務會呼叫 IStiUSD::GetNotificationData 方法,以取得事件的詳細資訊。
IStiUSD::GetNotificationData 方法會針對輪詢事件和中斷事件呼叫。 在此方法中,您應該填寫適當的事件資訊以返回 WIA 服務。
注意 請務必清除 dwEventHandlingState 成員中的STI_EVENTHANDLING_PENDING旗標,以確保裝置事件發生時已正確設定。 當偵測到事件時,此 WIA 驅動程式應將類別成員變數 m_guidLastEvent 設定為適當的事件 GUID。 稍後當 WIA 服務呼叫 IStiUSD::GetNotificationData 方法時,會檢查 m_guidLastEvent。 m_guidLastEvent成員變數定義在 CWIADevice 類別中(在下列代碼段中),用來快取最後一個發出訊號的事件。 WIA 服務要求此成員變數之後,它一律會設定為 GUID_NULL。
下列範例示範 IStiUSD::GetStatus 方法的實作。
STDMETHODIMP CWIADevice::GetStatus(PSTI_DEVICE_STATUS pDevStatus)
{
//
// If the caller did not pass in the correct parameters,
// then fail the call with E_INVALIDARG.
//
if(!pDevStatus)
{
return E_INVALIDARG;
}
HRESULT hr = S_OK;
//
// If we are asked, verify state of an event handler
//(front panel buttons, user controlled attachments, etc.).
//
// If your device requires polling, then this is where you would
// specify the event result.
// However, It is not recommended to have polled events.
// Interrupt events are better for performance, and reliability.
// See the SetNotificationsHandle method for how to
// implement interrupt events.
//
//
// clear the dwEventHandlingState field first to make sure we are really setting
// a pending event.
//
pDevStatus->dwEventHandlingState &= ~STI_EVENTHANDLING_PENDING;
if (pDevStatus->StatusMask & STI_DEVSTATUS_EVENTS_STATE) {
//
// set the polled event result here, for the GetNotificationData()
// method to read and report.
// (m_guidLastEvent will be read in GetNotificationData)
//
LONG lEventResult = 0;
PollMyDeviceForEvents(&lEventResult)
if(lEventResult == DEVICE_SCAN_BUTTON_PRESSED) {
//
// polled event result was one we know about
//
m_guidLastEvent = WIA_EVENT_SCAN_IMAGE;
} else {
//
// nothing happened, so continue
//
m_guidLastEvent = GUID_NULL;
}
if (m_guidLastEvent != GUID_NULL) {
//
// if the event GUID is NOT GUID_NULL, set the
// STI_EVENTHANDLING_PENDING flag letting the WIA service
// know that we have an event ready. This will tell the WIA
// service to call GetNotificationData() for the event
// specific information.
//
pDevStatus->dwEventHandlingState |= STI_EVENTHANDLING_PENDING;
}
}
return S_OK;
}