close the twain scanner

AMER SAID 396 Reputation points
2021-08-01T16:35:41.153+00:00

hi

I used the following code to read the scanner file and review it in the image box with twain scanner.
It works well if the scanner is connected to the computer.
If the scanner is not connected, the code stops the window form and does not close.
I want a way to check the scanner connection or cancel if the scanner is not connected to the device (pc)

 Try
            Dim s As Collection

            s = SCANNER.ScanImages()

            For Each k In s

                PictureBox1.Load(k)

            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2021-08-01T20:07:51.59+00:00

    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
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2021-08-01T19:13:43.4+00:00

    Try something like this:

    Dim s As Collection
    
    Dim t As New Thread(Sub()
                            s = SCANNER.ScanImages()
                        End Sub)
    t.IsBackground = True
    
    t.Start()
    
    REM wait 20 seconds
    
    If Not t.Join(TimeSpan.FromSeconds(20)) Then
        t.Abort()
        MsgBox("Scanner is not responding")
    Else
        For Each k In s
            PictureBox1.Load(k)
        Next
    End If
    

    Or check the SCANNER object and the documentation. Maybe there are more suitable methods.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.