winapivb6

How can I detect if there is a webcam connected to the PC?


I have the source code for taking a picture via webcam, but I need to know if there's a webcam connected to the PC. If there's no webcam, the program won't call the function for taking photos. If necessary, I need to know which window's Api I have to use in this case.

Language: Visual Basic 6 using windows Apis.


Solution

  • The problem with the WIA is that not all devices are compatible. But I found the solution myself: SendMessage and capCreateCaptureWindowA work together. If there's no camera, SendMessage returns a "0". This is the code:

    imports:

    'This code is in Module1.bas
    Option Explicit
    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Public Declare Function capCreateCaptureWindow Lib "avicap32.dll" Alias "capCreateCaptureWindowA" (ByVal lpszWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hwndParent As Long, ByVal nID As Long) As Long
    
    Public Const WM_USER = 1024
    Public Const WM_CAPCONNECT As Long = 1034
    Public Const DISCONNECT As Long = 1035
    Public Const WM_CAP_SET_PREVIEW = WM_USER + 50
    Public mCapHwnd As Long
    

    Code:

    mCapHwnd = capCreateCaptureWindowA("WebCap", 0, 0, 0, m_Width, m_Height, Me.hwnd, 0)
    DoEvents
    If SendMessage(mCapHwnd, WM_CAP_CONNECT, 0, 0) <> 0 Then
    Call SendMessage(mCapHwnd, WM_CAP_CONNECT, 0, 0)
    DoEvents
    Call SendMessage(mCapHwnd, WM_CAP_SET_PREVIEW, 0, 0)
    Else
    MsgBox "No Camera Detected"
    End If
    

    I hope somebody can find this useful :)