VBcoders Browse New Submit Contact Sign In

No account? Register free

Forgot password?

API AppendToLog

Adam Murray  (4 Submissions)   Files/File Controls/Input/Output   Visual Basic 3.0   Advanced   Wed 3rd February 2021

Appends a string to a specified file (or creates the file if needed). Unlike the open/print/close method, this subroutine uses Win32 API calls. This ensures that your application will be able to write to its log file correctly, even if another user has that log file open in an editor during your write attempt. This is the most practical approach to production-quality logging.

Inputs
lpFileName As String - the name of the file to append to sMessage As String - the string to append to the log file

Returns
True on success, False otherwise

Side Effects
none known

API Declarations
Const GENERIC_WRITE = &H40000000
Const FILE_SHARE_READ = &H1
Const CREATE_NEW = 1
Const OPEN_EXISTING = 3
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_BEGIN = 0
Const INVALID_HANDLE_VALUE = -1
Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Any) As Long
Declare Function FlushFileBuffers Lib "kernel32" (ByVal hFile As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Rate API AppendToLog (6(6 Vote))
API AppendToLog.bas

API AppendToLog Comments

No comments yet — be the first to post one!

Post a Comment

0/1000 characters