Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools


This series of articles will show you step by step approach for working with AVI files in VB using Win32 API. In this article series we will go through following tutorials.

Other articles in this series

Working with AVI Files - Part 1 (Opening and Closing AVI file)
Working with AVI Files - Part 2 (Working with AVI streams)
Working with AVI Files - Part 3 (Working With Frames, AVI to BMP)


In our first article we will write a simple program that allows the user to choose an AVI file from the disk and get an interface pointer to it for use with the AVIFile functions. This is the framework sample which is built on in all the other steps. Be sure you understand it before going on.

Lets start With a very simple example which will

- Initialize AVI Library for API calls using AVIFileInit api
- Get a valid handle of AVI file using AVIFileOpen api
- Read AVI file information using AVIFileInfo api
- Print AVI file information which we retrived in AVI_FILE_INFO structure
- Release AVI file and AVIFile Lib using AVIFileRelease and AVIFileExit api

Initialize AVI Library for API calls using AVIFileInit api

Before you call any of the AVIFile API functions you need to be sure that you call the AVIFileInit API which initialize AVI Library for susecuent AVI related API calls. Once you done with all AVI Api calls you should call AVIFileExit to releases AVIFile library. Here is the VB declaration for AVIFileInit and AVIFileExit.

Click here to copy the following block
Public Declare Sub AVIFileInit Lib "avifil32.dll" ()

Public Declare Sub AVIFileExit Lib "avifil32.dll" ()

Get a valid handle of AVI file using AVIFileOpen api

The next step is to be able to open the AVI file that the user pass the AVI path a get a valid file handle hFile which can be passed to other AVIFile functions as necessary. We use the AVIFileOpen function to do this. Here is the VB declaration for AVIFileOpen.

Click here to copy the following block
Public Declare Function AVIFileOpen Lib "avifil32.dll" _
    (ByRef ppfile As Long, _
    ByVal szFile As String, _
    ByVal uMode As Long, _
    ByVal pclsidHandler As Long) As Long

The AVIFileOpen function accepts the same OF_XXXX flags as the OpenFile API function. The hFile 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 0)

Parameters:

ppfile
[in] Address to contain the new file interface pointer.

szFile
[in] Null-terminated string containing the name of the file to open.

mode
[in] Access mode to use when opening the file. The default access mode is OF_READ. The following access modes can be specified with AVIFileOpen:

  • OF_CREATE : Creates a new file. If the file already exists, it is truncated to zero length.
  • OF_SHARE_DENY_NONE : Opens the file nonexclusively. Other processes can open the file with read or write access. AVIFileOpen fails if another process has opened the file in compatibility mode.
  • OF_SHARE_DENY_READ : Opens the file nonexclusively. Other processes can open the file with write access. AVIFileOpen fails if another process has opened the file in compatibility mode or has read access to it.
  • OF_SHARE_DENY_WRITE : Opens the file nonexclusively. Other processes can open the file with read access. AVIFileOpen fails if another process has opened the file in compatibility mode or has write access to it.
  • OF_SHARE_EXCLUSIVE : Opens the file and denies other processes any access to it. AVIFileOpen fails if any other process has opened the file.
  • OF_READ : Opens the file for reading.
  • OF_READWRITE : Opens the file for reading and writing.
  • OF_WRITE : Opens the file for writing.

pclsidHandler
[in] Address of a class identifier of the standard or custom handler you want to use. If the value is NULL, the system chooses a handler from the registry based on the file extension or the RIFF type specified in the file.

Read AVI file information using AVIFileInfo api

After getting a valid handle to AVI file you can read AVI file information by calling AVIFileInfo. Here is the VB declaration for AVIFileInfo.

Click here to copy the following block
Public Declare Function AVIFileInfo Lib "avifil32.dll" _
    (ByVal pfile As Long, _
    pfi As AVI_FILE_INFO, _
    ByVal lSize As Long) As Long

This function takes 3 parameters

Parameters :

pfile
[in] Handle of an open AVI file.

pfi
[in, out] Address of the structure used to return file information. Typically, this parameter points to an AVIFILEINFO (we have renamed to AVI_FILE_INFO to avoid confusion with API and Structure with same name) structure.

lSize
[in] Size, in bytes, of the structure.

So our actual call may be like below

Click here to copy the following block
If AVIFileInfo(hFile, AviInfo, Len(AviInfo)) = 0 Then
  Call PrintAVIFileInfo(AviInfo, Text2)
Else
  MsgBox "Error while retrieving AVI information... " & GetAPIErrorDesc(Err.LastDllError)
End If

Print AVI file information which we retrived in AVI_FILE_INFO structure

Here is a simple routine to dump the information stored in AVI_FILE_INFO structure.

Click here to copy the following block
Public Sub PrintAVIFileInfo(afi As AVI_FILE_INFO, Optional txtDebug As TextBox)
  Dim txt As String
  
  txt = txt & vbCrLf & "**********************************"
  txt = txt & vbCrLf & "**** AVI_FILE_INFO (START) *******"
  txt = txt & vbCrLf & "**********************************"
  With afi
    txt = txt & vbCrLf & "dwMaxBytesPerSecond = " & .dwMaxBytesPerSecond
    txt = txt & vbCrLf & "dwFlags = " & .dwFlags
    txt = txt & vbCrLf & "dwCaps = " & .dwCaps
    txt = txt & vbCrLf & "dwStreams = " & .dwStreams
    txt = txt & vbCrLf & "dwSuggestedBufferSize = " & .dwSuggestedBufferSize
    txt = txt & vbCrLf & "dwWidth = " & .dwWidth
    txt = txt & vbCrLf & "dwHeight = " & .dwHeight
    txt = txt & vbCrLf & "dwScale = " & .dwScale
    txt = txt & vbCrLf & "dwRate = " & .dwRate
    txt = txt & vbCrLf & "dwLength = " & .dwLength
    txt = txt & vbCrLf & "dwEditCount = " & .dwEditCount
    txt = txt & vbCrLf & "szFileType = " & .szFileType
  End With
  txt = txt & vbCrLf & "**********************************"
  txt = txt & vbCrLf & "**** AVI_FILE_INFO (END) *********"
  txt = txt & vbCrLf & "**********************************"
  txt = txt & vbCrLf & ""
  
  If IsMissing(txtDebug) Then
    '//No TextBox is supplied so just dump in Immidiate window
    Debug.Print txt
  Else
    txtDebug = txt
  End If
End Sub

Release AVI file and AVIFile Lib using AVIFileRelease and AVIFileExit api

And at last once we done we can call cleanup routines

Click here to copy the following block
Public Declare Function AVIFileRelease Lib "avifil32.dll" _
  (ByVal pfile As Long) As Long

Public Declare Sub AVIFileExit Lib "avifil32.dll" ()

The AVIFileRelease function decrements the reference count of an AVI file interface handle and closes the file if the count reaches zero.

AVIFileRelease takes only one parameter pfile which is our AVI file handle hFile.

The AVIFileExit function exits the AVIFile library and decrements the reference count for the library. It takes no argument.


And here is the full copy/paste implementation of our example. To run this example

- Create a standard exe project
- Place one command button
- Place 2 text box. Set MultiLine=True and Scrollbar=both for text2
- Run the project
- Specify valid AVI path and click on Command button

Place following code in Form1

Form1.frm

Click here to copy the following block
Private Sub Command1_Click()
Private Sub Command1_Click()
  On Error GoTo errHandler

  Dim hFile As Long, AviInfo As AVI_FILE_INFO

  'initialize the AVIFile library
  Call AVIFileInit
  'create a handle to the AVI file
  If AVIFileOpen(hFile, Text1, OF_SHARE_DENY_WRITE, ByVal 0&) = 0 Then
    'retrieve the AVI information
    If AVIFileInfo(hFile, AviInfo, Len(AviInfo)) = 0 Then
      Call PrintAVIFileInfo(AviInfo, Text2)
    Else
      MsgBox "Error while retrieving AVI information... " & GetAPIErrorDesc(Err.LastDllError)
    End If
  Else
    MsgBox "Error while opening the AVI file... " & GetAPIErrorDesc(Err.LastDllError)
  End If

errHandler:
  If hFile <> 0 Then
    'closes the file
    Call AVIFileRelease(hFile)

    'exit the AVIFile library and decrement the reference count for the library
    Call AVIFileExit
  End If
End Sub

Private Sub Form_Load()
'//Check if application of root (i.e. C:\ or D:\ etc)
  If Len(App.Path) > 3 Then
    Text1 = App.Path & "\" & "test.avi"
  Else
    Text1 = App.Path & "test.avi"
  End If
End Sub

Place following code in the module

Module1.bas

Click here to copy the following block
Option Explicit

'////////////////////////////////////////
'//To diaplay API Error
'////////////////////////////////////////
Global Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
Global Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Global Const LANG_NEUTRAL = &H0
Global Const SUBLANG_DEFAULT = &H1

Public Declare Function GetLastError Lib "kernel32" () As Long
Public Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Long)
Public Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long
'////////////////////////////////////////
'//API for AVI
'////////////////////////////////////////

Public Declare Function AVIFileInfo Lib "avifil32.dll" _
    (ByVal pfile As Long, _
    pfi As AVI_FILE_INFO, _
    ByVal lSize As Long) As Long  'HRESULT

Public Declare Sub AVIFileInit Lib "avifil32.dll" ()

Public Declare Function AVIFileOpen Lib "avifil32.dll" _
    (ByRef ppfile As Long, _
    ByVal szFile As String, _
    ByVal uMode As Long, _
    ByVal pclsidHandler As Long) As Long  'HRESULT

Public Declare Function AVIFileRelease Lib "avifil32.dll" _
    (ByVal pfile As Long) As Long

Public Declare Sub AVIFileExit Lib "avifil32.dll" ()

'**************************************************************************
' AVIFile Types
'***************************************************************************/
'for use with AVIFIleInfo Api
Public Type AVI_FILE_INFO '108 bytes?
  dwMaxBytesPerSecond As Long
  dwFlags As Long
  dwCaps As Long
  dwStreams As Long
  dwSuggestedBufferSize As Long
  dwWidth As Long
  dwHeight As Long
  dwScale As Long
  dwRate As Long
  dwLength As Long
  dwEditCount As Long
  szFileType As String * 64
End Type

'// file open mode
Global Const OF_CREATE = &H1000
Global Const OF_READ = &H0
Global Const OF_READWRITE = &H2
Global Const OF_SHARE_DENY_WRITE = &H20
Global Const OF_WRITE = &H1

Public Sub PrintAVIFileInfo(afi As AVI_FILE_INFO, Optional txtDebug As TextBox)
  Dim txt As String

  txt = txt & vbCrLf & "**********************************"
  txt = txt & vbCrLf & "**** AVI_FILE_INFO (START) *******"
  txt = txt & vbCrLf & "**********************************"
  With afi
    txt = txt & vbCrLf & "dwMaxBytesPerSecond = " & .dwMaxBytesPerSecond
    txt = txt & vbCrLf & "dwFlags = " & .dwFlags
    txt = txt & vbCrLf & "dwCaps = " & .dwCaps
    txt = txt & vbCrLf & "dwStreams = " & .dwStreams
    txt = txt & vbCrLf & "dwSuggestedBufferSize = " & .dwSuggestedBufferSize
    txt = txt & vbCrLf & "dwWidth = " & .dwWidth
    txt = txt & vbCrLf & "dwHeight = " & .dwHeight
    txt = txt & vbCrLf & "dwScale = " & .dwScale
    txt = txt & vbCrLf & "dwRate = " & .dwRate
    txt = txt & vbCrLf & "dwLength = " & .dwLength
    txt = txt & vbCrLf & "dwEditCount = " & .dwEditCount
    txt = txt & vbCrLf & "szFileType = " & .szFileType
  End With
  txt = txt & vbCrLf & "**********************************"
  txt = txt & vbCrLf & "**** AVI_FILE_INFO (END) *********"
  txt = txt & vbCrLf & "**********************************"
  txt = txt & vbCrLf & ""

  If IsMissing(txtDebug) Then
    '//No TextBox is supplied so just dump in Immidiate window
    Debug.Print txt
  Else
    txtDebug = txt
  End If
End Sub

Public Function GetAPIErrorDesc(ErrorCode As Long) As String
  Dim Buffer As String
  Buffer = Space(200)
  FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, ErrorCode, LANG_NEUTRAL, Buffer, 200, ByVal 0&
  GetAPIErrorDesc = Buffer
End Function

Other articles in this series

Working with AVI Files - Part 1 (Opening and Closing AVI file)
Working with AVI Files - Part 2 (Working with AVI streams)
Working with AVI Files - Part 3 (Working With Frames, AVI to BMP)


Submitted By : Jojo Desuja  (Member Since : 8/10/2004 10:56:17 PM)

Job Description : The King of Night....... Love to do programming specially night time....
View all (22) submissions by this author  (Birth Date : )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.