title: VB.Net Capture Video Using avicap32.dll without UI
body: <p>I am trying to create an application that records a video using avicap32.dll, but without using an user interface. The sample code that I found(the only code that shows how to record a video and not just capture an image) uses an hWnd that required the handle-ID of a picture-box. Is there any way that I can circumvent this, and just connect to the driver, record and save the video? </p>
  
  <p>My code below:
  </p>
  
  <pre><code>    Imports System
      Imports System.Runtime.InteropServices
      Public Class Recorder
          Const WM_CAP_START = &amp;H400S
          Const WS_CHILD = &amp;H40000000
          Const WS_VISIBLE = &amp;H10000000
  
          Const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10
          Const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11
          Const WM_CAP_EDIT_COPY = WM_CAP_START + 30
          Const WM_CAP_SEQUENCE = WM_CAP_START + 62
          Const WM_CAP_FILE_SAVEAS = WM_CAP_START + 23
  
          Const WM_CAP_SET_SCALE = WM_CAP_START + 53
          Const WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52
          Const WM_CAP_SET_PREVIEW = WM_CAP_START + 50
  
          Const SWP_NOMOVE = &amp;H2S
          Const SWP_NOSIZE = 1
          Const SWP_NOZORDER = &amp;H4S
          Const HWND_BOTTOM = 1
  
          Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriverIndex As Short, ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As String, ByVal cbVer As Integer) As Boolean
  
          Declare Function capCreateCaptureWindowA Lib "avicap32.dll" (ByVal lpszWindowName As String, ByVal dwStyle As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Short, ByVal hWnd As Integer, ByVal nID As Integer) As Integer
  
          Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, &lt;MarshalAs(UnmanagedType.AsAny)&gt; ByVal lParam As Object) As Integer
  
          Private Shared Function ReturnDriver() As Integer 'As String
              Dim DriverName As String = Space(100)
              Dim DriverVersion As String = Space(100)
              'Dim ReturnDriverName As String = ""
              Dim DriverIndex As Integer = Nothing
              For i As Integer = 0 To 9
                  If capGetDriverDescriptionA(i, DriverName, 80, DriverVersion, 80) Then
                      'lstVideoSources.Items.Add(DriverName.Trim)
                      'ReturnDriverName = DriverName.Trim
                      DriverIndex = i
                      Exit For
                  End If
              Next
              Return DriverIndex
          End Function
  
          Public Shared Sub StartRecording()
              Dim DriverVersion As Integer = ReturnDriver()
              If SendMessage(0, WM_CAP_DRIVER_CONNECT, DriverVersion, 0) Then
                  SendMessage(0, WM_CAP_SEQUENCE, 0, 0)
              End If
          End Sub
  
          Public Shared Sub StopRecording()
              SendMessage(0, WM_CAP_FILE_SAVEAS, 0, "C:\records\" &amp; Now().ToString() &amp; ".avi")
          End Sub
      End Class
  </code></pre>