You should use WIA for scanners ("Windows Image Acquisition Library v2.0" on the COM tab),
more recent than Twain
There is also STI
A test with IStillImage.GetDeviceList to check if there is a scanner connected (it returns 0x80070491 with my scanner when I disconnect it) :
Dim CLSID_StillImage As New Guid("B323F8E0-2E68-11D0-90EA-00AA0060F86C")
Dim StillImageType As Type = Type.GetTypeFromCLSID(CLSID_StillImage, True)
Dim StillImage As Object = Activator.CreateInstance(StillImageType)
pStillImage = DirectCast(StillImage, IStillImage)
If (pStillImage IsNot Nothing) Then
Dim nNbItems As UInteger = 0
Dim pBuffer As IntPtr = IntPtr.Zero
' HRESULT = 0x80070491 Message = Aucune correspondance pour la clé indiquée dans l'index. (Exception de HRESULT : 0x80070491)
Dim hr As HRESULT = pStillImage.GetDeviceList(0, 0, nNbItems, pBuffer)
If (hr <> HRESULT.S_OK) Then
MessageBox.Show("No scanner found", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Number of scanners detected : " & nNbItems.ToString(), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
Simplified declarations :
Public Enum HRESULT As Integer
S_OK = 0
S_FALSE = 1
E_NOINTERFACE = &H80004002
E_NOTIMPL = &H80004001
E_FAIL = &H80004005
E_UNEXPECTED = &H8000FFFF
E_OUTOFMEMORY = &H8007000E
End Enum
<ComImport, Guid("641BD880-2DC8-11D0-90EA-00AA0060F86C"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
Public Interface IStillImage
Function Initialize(hinst As IntPtr, dwVersion As UInteger) As HRESULT
<PreserveSig>
Function GetDeviceList(dwType As UInteger, dwFlags As UInteger, ByRef pdwItemsReturned As UInteger, ByRef ppBuffer As IntPtr) As HRESULT
' incomplete, not used...
End Interface