'Sample Code: Microsoft Visual Basic version of IsFileAlreadyOpen
'----------------------------------------------------------------
' Declaration for APIs used by our function...
Private Declare Function lopen Lib "kernel32" Alias "_lopen" (ByVal
lpPathName As String, ByVal iReadWrite As Long) As Long
Private Declare Function GetLastError Lib "kernel32" () As Long
Private Declare Function lclose Lib "kernel32" Alias "_lclose" (ByVal
hFile As Long) As Long
Function IsFileAlreadyOpen(Filename As String) As Boolean
Dim hFile As Long
Dim lastErr As Long
' Initialize file handle and error variable.
hFile = -1
lastErr = 0
' Open for for read and exclusive sharing.
hFile = lopen(Filename, &H10)
' If we couldn't open the file, get the last error.
If hFile = -1 Then
lastErr = Err.LastDllError
' Make sure we close the file on success.
Else
lclose (hFile)
End If
' Check for sharing violation error.
If (hFile = -1) And (lastErr = 32) Then
IsFileAlreadyOpen = True
Else
IsFileAlreadyOpen = False
End If
End Function