Scanner status detection

Apostolos Doudakmanis 101 Reputation points
2024-12-16T17:31:54.6866667+00:00

How can I identify the status of a scanner, i.e. if it is active, if it is closed, or if it is on standby? without ManagementObject

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,760 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 86,486 Reputation points
    2024-12-16T21:26:10.82+00:00

    A way can be with WIA

    I did this test in a Button click :

    • Add Reference "Microsoft Windows Image Acquisition Library v2.0" on the COM tab.

    Test code (add a Button on the Form...) :

    Imports System.Runtime.InteropServices
    Imports WIA
    
    Public Class Form1
    
        Public Const WIA_DPA_CONNECT_STATUS As String = "Connect Status"
        Public Const WIA_DIP_DEV_NAME As String = "Name"
    
        Public Const WIA_DEVICE_NOT_CONNECTED = 0
        Public Const WIA_DEVICE_CONNECTED = 1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim deviceManager As New DeviceManager()
            For Each deviceInfo As DeviceInfo In deviceManager.DeviceInfos
                If deviceInfo.Type = WiaDeviceType.ScannerDeviceType Then
                    Try
                        Dim device As Device = deviceInfo.Connect()
    
                        ' Test to display all properties
                        'Dim properties As Properties = device.Properties
                        'For Each prop In properties
                        '    Debug.WriteLine($"{prop.Name} : {prop.Value}")
                        'Next
    
                        If device.Properties.Exists(WIA_DIP_DEV_NAME) Then
                            Dim sName As String = CType(device.Properties(WIA_DIP_DEV_NAME).Value, String)
                            Debug.WriteLine($"{WIA_DIP_DEV_NAME} : {sName}")
                        Else
                            Debug.WriteLine($"{WIA_DIP_DEV_NAME} property not supported.")
                        End If
    
                        If device.Properties.Exists(WIA_DPA_CONNECT_STATUS) Then
                            Dim sStatus As String = CType(device.Properties(WIA_DPA_CONNECT_STATUS).Value, String)
                            sStatus = If(sStatus = WIA_DEVICE_CONNECTED, "Connected", "Disconnected")
                            Debug.WriteLine($"{WIA_DPA_CONNECT_STATUS} : {sStatus}")
                        Else
                            Debug.WriteLine($"{WIA_DPA_CONNECT_STATUS} property not supported.")
                        End If
                    Catch ex As COMException When ex.ErrorCode = &H80210006 ' WIA_ERROR_BUSY : HRESULT : 0x80210006
                        Debug.WriteLine("The device is busy. Try again later.")
                    Catch ex As Exception
                        Debug.WriteLine($"An error occurred: {ex.Message}")
                    End Try
                End If
            Next
    
        End Sub
    End Class
    
    

0 additional answers

Sort by: Most 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.