Although scanners and cameras are the most common types of Twain devices, they are not necessarily the only types. Furthermore, the capabilities present in both types of devices overlap, meaning there could be many capabilities common to both.
You could make use of the fact that ICAP_UNITS is required by all types of sources, and that cameras almost always support only the PIXELS unit, while most if not all scanners support the INCHES unit.
I tried the following code with 4 different Twain drivers (1 camera and 3 scanners) and it worked. However, there's no guarantee it will always work, because you can never know how different vendors write their Twain drivers.
Sub CheckTwainDeviceType()
Dim var As New LEADRasterVariant
Dim TwainCap As New LEADTwainCapability_D ' or LEADTwainCapability
Set RasTwain = New LEADRasterTwain_D ' or LEADRasterTwain
RasTwain.InitSession Me.hWnd
RasTwain.SelectSource
TwainCap.CapInfo.ConType = L_TWON_ONEVALUE
TwainCap.CapInfo.Capability = L_ICAP_UNITS
TwainCap.EnableMethodErrors = False
var.Type = VALUE_USHORT
var.LongValue = L_TWUN_INCHES
TwainCap.CapOneValue.OneValItemType = L_TWTY_UINT16
TwainCap.CapOneValue.OneValCapValue = var
RasTwain.EnableMethodErrors = False
Dim nRet As Integer
nRet = RasTwain.SetCapability2(TwainCap, L_LTWAIN_CAPABILITY_SET)
If 0 = nRet Then 'success
MsgBox "Most likely a scanner"
Else
MsgBox "Most likely a camera"
End If
Set TwainCap = Nothing
Set var = Nothing
RasTwain.EndSession
End Sub
About the Query Capability method, here's a code sample that uses it. If you want to perform the same check using it, the camera will have TwainCap.CapEnum.EnumNumOfItems equal to 1, and TwainCap.CapEnum.EnumCapValue(0).LongValue equal to 5 (L_TWUN_PIXELS):
Sub TwainQueryCap()
Dim var As New LEADRasterVariant
Dim TwainCap As New LEADTwainCapability_D ' or LEADTwainCapability
Set RasTwain = New LEADRasterTwain_D ' or LEADRasterTwain
RasTwain.InitSession Me.hWnd
RasTwain.SelectSource
TwainCap.CapInfo.ConType = L_TWON_ENUMERATION
TwainCap.CapInfo.Capability = L_ICAP_UNITS
TwainCap.EnableMethodErrors = False
RasTwain.EnableMethodErrors = False
Dim nRet As Integer
nRet = RasTwain.QueryCapability2(TwainCap)
Dim msg As String, i As Integer
If 0 = nRet Then 'success
msg = "Num of values = " & TwainCap.CapEnum.EnumNumOfItems & vbCrLf
For i = 0 To TwainCap.CapEnum.EnumNumOfItems - 1
msg = msg & "value " & i & " is " & TwainCap.CapEnum.EnumCapValue(i).LongValue & vbCrLf
Next
MsgBox msg
End If
Set TwainCap = Nothing
Set var = Nothing
RasTwain.EndSession
End Sub
Amin Dodin
Senior Support Engineer
LEAD Technologies, Inc.