Getting a handle on AVIs
Video Capture Help ] AVIFile Tutorial ] Licensing and Distribution ] Resources ] Awards ] Latest News ] Source Code ] Free Controls ]

 

STEP 1 - Opening and closing existing AVI files

HERE ARE THE PROJECT FILES FOR STEP 1 AVITutr1.zip

Before calling any of the AVIFile API functions you need to be sure that you call the AVIFileInit Sub.  This subroutine (or 'function which returns VOID' in C-speak) merely tells Windows to initialize the AVIFile library functions for later use.  When you are finished with all AVI functions you should make a corresponding call to the AVIFileExit Sub.

For our simple test program, put the following line of code in Form_Load:

Call AVIFileInit '// opens AVIFile library

And put the following line of code in Form_Unload:

Call AVIFileExit '// releases AVIFile library

If you were creating a class wrapper for working with AVI files, you could put AVIFileInit in the Class_Initialize method and the corresponding call to AVIFileExit in the Class_Terminate method.  The point here is that AVIFileInit needs to be called first and must be balanced by a call to AVIFileExit when you are finished using the AVIFile functions.

We also need to get the name of an AVI file to open.  I don't really like hard-coding path names in my code, even in sample apps like this - so I have included the cFileDlg Class in the project.  This class is one that I use to replace the bulky Common Dialog control.  Feel free to replace cFileDlg.cls class with the VB Common Dialog ocx if you really want to.  To get an AVI file to work with add a command button to the form in your test program.  In the click event of the command button add these lines of code:

Dim res As Long 'result code
Dim ofd As cFileDlg 'OpenFileDialog class
Dim szFile As String 'filename

'Get the name of an AVI file to work with
Set ofd = New cFileDlg
With ofd
    .OwnerHwnd = Me.hWnd
    .Filter = "AVI Files|*.avi"
    .DlgTitle = "Open AVI File"
End With
res = ofd.VBGetOpenFileName(szFile)
If res = False Then GoTo ErrorOut

All of this code is simply to display an open file dialog to the user and allow them to select an AVI file to work with.  If the user clicks the cancel button on the dialog, the GoTo will send the program to the ErrorOut tag (which we will add later).

The next step is to be able to open the AVI file that the user selects from the common dialog and get a PAVIFILE handle which can be passed to other AVIFile functions as necessary.  We use the AVIFileOpen function to do this.  Add these 2 lines of code to the command button click event:

Dim pAVIFile as Long 'pointer to AVI File (PAVIFILE handle)

res = AVIFileOpen(pAVIFile, szFile, OF_SHARE_DENY_WRITE, 0&)

The AVIFileOpen function accepts the same OF flags as the OpenFile API function.  The pAVIFile variable will contain the file interface pointer to the AVI file specified in the szFile varable.  If the call is successful, it will return AVIERR_OK (which is a constant defined in the mAVIDecs.bas file as 0). 

Since we are going to eventually be calling a lot of WinAPI functions that need clean-up, it's important to add a basic error-handling structure so the program can exit cleanly if there are problems.  To check the results of this function, I add the following line of code immediately after the call to AVIFileOpen:

If res <> AVIERR_OK Then GoTo ErrorOut

(*note - Please don't write telling me how horrible it is to use 'GoTo'.  I agree!  Normally all this stuff would be in a class and neatly componentized, but in order to keep the error/cleanup routine seperated out from the code I decided to use GoTo.  Feel free to not use it in your own program.)

Then I add cleanup code after the ErrorOut: tag and check to see if there was an error at the very end of the function.  This will allow us to properly clean up all resources which may have been allocated by API calls and show user feedback if there was an error.  The call to AVIFileRelease releases the file handle and closes the file.  You need to be careful that you always call AVIFileRelease eventually after calling AVIFileOpen.  Also, in this sample I show a message box to the user if there was an error.  Of course, if you are building a class-based program you would probably want to raise an error to the client instead of showing a message box.  Here is the code for the end of the Click Sub:

ErrorOut:
    If pAVIFile <> 0 Then
        Call AVIFileRelease(pAVIFile)
'// closes the file
    End If

    If (rc <> AVIERR_OK) Then
'if there was an error then show feedback to user
        MsgBox "There was an error working with the file:" _
                & vbCrLf & szFile, vbInformation, App.Title
    End If

That's all there is to it!  You can now open an existing AVI file from VB and get a PAVIFILE interface pointer to use with the AVIFile WinAPI functions.  Since this program doesn't really do anything, I added one MsgBox call to display the interface pointer value (probably a very long number) to the user.  You can download the entire sample program here (AVITutr1.zip).

Go to the next step

Go back to the Table of Contents

This Shrinkwrap Visual Basic AVIFile Tutorial is Copyright (C) 2000 by Ray Mercer
Redistribution of the tutorial text and/or samples is prohibited.  Please contact the author Ray Mercer <raymer@shrinkwrapvb.com> if you have a question about this policy.