2,892 questions
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