'Using Windows System Error Messages


'neotreksofware.com


'For free use with gb32

'cut and paste the code


'Task: Using system error messages for file I/0.


'Windows System Error messages will detect errors in file paths file sharing, locking etc.

'on the local system and/or whether a file is already open or not.


'Discussion: Windows has built in error codes. Sometimes these are seen as an error number in

'in a message box with no explanation given if an error is not trapped (ie: an unhandled exception).

'You can experiment with this example by changing the filename "x:\myfile.txt" to non-existant

'files folders ect. to cause an exception (error) and trapping the error to see the built in 'windows

'error message, and we are able take steps necessary to handle the exception without ending

'the application process thread.


'the declaration is needed GB 32 does not include _lopen

Declare Function lOpen Lib "kernel32" Alias "_lopen" (ByVal lpPathName As String, ByVal iReadWrite As Long) As Long

Declare Function lClose Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) As Long

Global Const GENERIC_READ = &H80000000


@Openfile("x:\myfile.txt")

Function Openfile(FileName As String)


Dim hFile As Long

Dim dwError As Long

'Initialize file handle and error variable.

hFile = -1

dwError = 0


'Open for for read

hFile = lOpen(FileName, GENERIC_READ)

'If we couldn't open the file, get the last error.

If hFile = -1 Then

dwError = Err.LastDllError //point to Win 32 system error codes

Else 'Existing was sucessfully opened

MsgBox "OK the file was opened."

'

'

'****To Do:****

'Write your file input / output code here

'

'

'Close the file

lClose(hFile)

End If

If dwError <> 0


'we can add to this subset or devise a full set

'using standard array


If dwError = 2 'ERROR_FILE_NOT_FOUND

MsgBox "ERROR_FILE_NOT_FOUND"

Else If dwError = 3 'ERROR_PATH_NOT_FOUND

MsgBox "ERROR_PATH_NOT_FOUND"

Else If dwError = 4 'ERROR_TOO_MANY_OPEN_FILES

MsgBox "ERROR_TOO_MANY_OPEN_FILES"

Else If dwError = 5 'ERROR_ACCESS_DENIED

MsgBox "ERROR_ACCESS_DENIED"

Else If dwError = 6 'ERROR_INVALID_HANDLE

MsgBox "ERROR_INVALID_HANDLE"

Else If dwError = 32 'ERROR_SHARING_VIOLATION

MsgBox "ERROR_SHARING_VIOLATION"

Else If dwError = 33 'ERROR_LOCK_VIOLATION

MsgBox "ERROR_LOCK_VIOLATION"

Else If dwError = 110 'ERROR_OPEN_FAILED

MsgBox "ERROR_OPEN_FAILED"

Else If dwError = 111 'ERROR_BUFFER_OVERFLOW

MsgBox "ERROR_BUFFER_OVERFLOW"

Else If dwError = 112 'ERROR_DISK_FULL

MsgBox "ERROR_DISK_FULL"

EndIf

EndIf


End Function