📄 basbigfiles.bas
字号:
curFilesize = 0@
lngHighOrder = 0& ' Initialize high order value
' Open file as read only and get the file handle
If OpenReadOnly(strFileName, hFile) Then
SetFilePointer hFile, 0, 0, FILE_BEGIN ' Set pointer to beginning of file
lngLowOrder = GetFileSize(hFile, lngHighOrder) ' Get API file size values
API_CloseFile hFile ' Close file handle
' See if the user wants to stop processing
DoEvents
If gblnStopProcessing Then
GoTo CalcFileSize_CleanUp
End If
Long2Size curFilesize, lngLowOrder, lngHighOrder ' Calculate actual file size
' See if the user wants to stop processing
DoEvents
If gblnStopProcessing Then
GoTo CalcFileSize_CleanUp
End If
'************************
'* Optional return data *
'************************
' Test for overflow when calculating the
' number of bits. This information is
' used for generating file hashes.
If (curFilesize * 8) >= MAXLONG Then
' File size is greater than or equal to 2gb
curHexTemp = (curFilesize * 8)
strBitsInHex = NumberToHex(curHexTemp)
Else
' File size is less than 2gb
strBitsInHex = Hex$(curFilesize * 8)
End If
End If
CalcFileSize_CleanUp:
API_CloseFile hFile ' Verify file handle has been released
On Error GoTo 0
Exit Sub
CalcFileSize_Error:
ErrorMsg MODULE_NAME, ROUTINE_NAME, Err.Description
curFilesize = 0
strBitsInHex = ""
Resume CalcFileSize_CleanUp
End Sub
' ***************************************************************************
' Routine: API_ReadFile
'
' Description: This routine is used to read data from an opened file.
'
' This works much like the standard Read # command. You pass
' the function the file handle you got when opening the file,
' the byte position you want to start our read from, the size
' of the data block you require, and a buffer. For the buffer,
' you use an array of bytes because this seems to be the best
' method to ensure that any type of data can be read and
' written with it.
'
' You use your conversion function to split the position info
' into the two 32-bit variables needed for the SetFilePointer
' API. Set the location in your file and then call your
' ReadFile API. And, for when you read at the end of the file,
' you pass the number of bytes that have been read.
'
' Parameters: hFile - Numeric value designating an open file
' curPosition - Current position within the file
' abytData() - Byte array to hold the data that was read
'
' Returns: TRUE - Successfully read the file
' FALSE - An error occurred while accessing the file
'
' ===========================================================================
' DATE NAME / DESCRIPTION
' ----------- --------------------------------------------------------------
' 22-Jan-2007 Richard Newcombe
' Wrote routine
' 03-Mar-2008 Kenneth Ives kenaso@tx.rr.com
' Modified and documented
' ***************************************************************************
Public Function API_ReadFile(ByVal hFile As Long, _
ByVal curPosition As Currency, _
ByRef abytData() As Byte) As Boolean
Dim lngRetCode As Long
Dim lngLowOrder As Long
Dim lngHighOrder As Long
Dim lngBytesRead As Long
Dim lngBytesToRead As Long
Const ROUTINE_NAME As String = "API_ReadFile"
On Error GoTo API_ReadFile_Error
API_ReadFile = False ' Preset to bad ending
' See if the user wants to stop processing
DoEvents
If gblnStopProcessing Then
Exit Function
End If
' Get size of data to write
lngBytesToRead = (UBound(abytData) + 1) * LenB(abytData(0))
' Calculate the current position within the file
Size2Long curPosition, lngLowOrder, lngHighOrder
' See if the user wants to stop processing
DoEvents
If gblnStopProcessing Then
GoTo API_ReadFile_CleanUp
End If
' Set the pointer to start at specific position within the file
SetFilePointer hFile, lngLowOrder, lngHighOrder, FILE_BEGIN
' See if the user wants to stop processing
DoEvents
If gblnStopProcessing Then
GoTo API_ReadFile_CleanUp
End If
' Test for successful file pointer
If lngHighOrder = &HFFFFFFFF Then
Err.Raise DUMMY_NUMBER, ROUTINE_NAME, _
"Failed to set read file pointer."
End If
' Read this portion of the file
lngRetCode = ReadFile(hFile, abytData(0), lngBytesToRead, lngBytesRead, 0&)
' Test for successful file read
If lngRetCode = 0 Then
Err.Raise DUMMY_NUMBER, ROUTINE_NAME, _
"Failed to read file."
End If
API_ReadFile = True ' Set flag for successful finish
API_ReadFile_CleanUp:
' See if the user wants to stop processing
DoEvents
If gblnStopProcessing Then
API_CloseFile hFile ' Verify file handle has been released
API_ReadFile = False
End If
On Error GoTo 0
Exit Function
API_ReadFile_Error:
ErrorMsg MODULE_NAME, ROUTINE_NAME, Err.Description
API_CloseFile hFile ' Verify file handle has been released
API_ReadFile = False
Resume API_ReadFile_CleanUp
End Function
' ***************************************************************************
' Routine: API_WriteFile
'
' Description: This routine is used to write data to the file.
'
' This works much like the standard Write # command. You pass
' the function the file handle you got when opening the file,
' the byte position you want to start our read from, the size
' of the data block you require, and a buffer. For the buffer,
' you use an array of bytes because this seems to be the best
' method to ensure that any type of data can be read and
' written with it.
'
' You use your conversion function to split the position info
' into the two 32-bit variables needed for the SetFilePointer
' API. Set the location in your file and then call your
' ReadFile API. And, for when you read at the end of the file,
' you pass the number of bytes that have been written.
'
' Parameters: hFile - Numeric value designating an open file
' curPosition - Current position within the file
' abytData() - Byte array that holds the data to be written
'
' Returns: TRUE - Successfully updated the file
' FALSE - An error occurred while accessing the file
'
' ===========================================================================
' DATE NAME / DESCRIPTION
' ----------- --------------------------------------------------------------
' 22-Jan-2007 Richard Newcombe
' Wrote routine
' 03-Mar-2008 Kenneth Ives kenaso@tx.rr.com
' Modified and documented
' ***************************************************************************
Public Function API_WriteFile(ByVal hFile As Long, _
ByVal curPosition As Currency, _
ByRef abytData() As Byte) As Boolean
Dim lngRetCode As Long
Dim lngLowOrder As Long
Dim lngHighOrder As Long
Dim lngBytesToWrite As Long
Dim lngBytesWritten As Long
Const ROUTINE_NAME As String = "API_WriteFile"
On Error GoTo API_WriteFile_Error
API_WriteFile = False ' Preset to bad ending
' See if the user wants to stop processing
DoEvents
If gblnStopProcessing Then
Exit Function
End If
' Get the length of data to write
lngBytesToWrite = (UBound(abytData) + 1) * LenB(abytData(0))
' Calculate the current position within the file
Size2Long curPosition, lngLowOrder, lngHighOrder
' See if the user wants to stop processing
DoEvents
If gblnStopProcessing Then
GoTo API_WriteFile_CleanUp
End If
' Set the pointer to start at specific position within the file
SetFilePointer hFile, lngLowOrder, lngHighOrder, FILE_BEGIN
' See if the user wants to stop processing
DoEvents
If gblnStopProcessing Then
GoTo API_WriteFile_CleanUp
End If
' Test for successful file pointer
If lngHighOrder = &HFFFFFFFF Then
Err.Raise DUMMY_NUMBER, ROUTINE_NAME, _
"Failed to set write file pointer."
End If
' Write to the file
lngRetCode = WriteFile(hFile, abytData(0), lngBytesToWrite, lngBytesWritten, 0&)
' Test for successful file read
If lngRetCode = 0 Then
Err.Raise DUMMY_NUMBER, ROUTINE_NAME, _
"Failed to write to file."
End If
FlushFileBuffers hFile ' Flush the file buffers to force writing of the data
API_WriteFile = True ' Set flag for successful finish
API_WriteFile_CleanUp:
' See if the user wants to stop processing
DoEvents
If gblnStopProcessing Then
API_CloseFile hFile ' Verify file handle has been released
API_WriteFile = False
End If
On Error GoTo 0
Exit Function
API_WriteFile_Error:
ErrorMsg MODULE_NAME, ROUTINE_NAME, Err.Description
API_CloseFile hFile ' Verify file handle has been released
API_WriteFile = False
Resume API_WriteFile_CleanUp
End Function
' ***************************************************************************
' Routine: API_SetEndOfFile
'
' Description: Sets the pointer to the end of the file designating that
' we are now finished with this file.
'
' Parameters: hFile - Numeric value designating an open file
' curPosition - Current position within the file
'
' ===========================================================================
' DATE NAME / DESCRIPTION
' ----------- --------------------------------------------------------------
' 22-Jan-2007 Richard Newcombe
' Wrote routine
' 03-Mar-2008 Kenneth Ives kenaso@tx.rr.com
' Modified and documented
' ***************************************************************************
Public Sub API_SetEndOfFile(ByVal hFile As Long, _
ByVal curPosition As Currency)
Dim lngRetCode As Long
Dim lngLowOrder As Long
Dim lngHighOrder As Long
Const ROUTINE_NAME As String = "API_SetEndOfFile"
On Error GoTo API_SetEndOfFile_Error
' Calculate the current position within the file
Size2Long curPosition, lngLowOrder, lngHighOrder
' Set the pointer to the end of the file
lngRetCode = SetFilePointer(hFile, lngLowOrder, lngHighOrder, FILE_BEGIN)
' Test for successful file pointer
If lngHighOrder = &HFFFFFFFF Then
Err.Raise DUMMY_NUMBER, ROUTINE_NAME, _
"Failed to set end of file pointer."
End If
' Set end of file marker
lngRetCode = SetEndOfFile(hFile)
API_SetEndOfFile_CleanUp:
On Error GoTo 0
Exit Sub
API_SetEndOfFile_Error:
ErrorMsg MODULE_NAME, ROUTINE_NAME, Err.Description
Resume API_SetEndOfFile_CleanUp
End Sub
' ***************************************************************************
' Routine: API_CloseFile
'
' Description: Closes an open file.
'
' Parameters: hFile - Numeric value designating an open file
'
' ===========================================================================
' DATE NAME / DESCRIPTION
' ----------- --------------------------------------------------------------
' 22-Jan-2007 Richard Newcombe
' Wrote routine
' 03-Mar-2008 Kenneth Ives kenaso@tx.rr.com
' Modified and documented
' ***************************************************************************
Public Sub API_CloseFile(ByRef hFile As Long)
' Always close a file when not in use or
' undesired consequences may happen.
If hFile > 0 Then
CloseHandle hFile ' Release file handle
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -