Re: Division causes overflow
The reason for the error is that integer division is used (\). Use (/) instead
1 Attachment(s)
Re: Division causes overflow
The FileLen() function doesn't handle huge files.
Assuming there is really any need for this at all, I'd probably choose something like this:
Code:
Option Explicit
Private Const GENERIC_READ As Long = &H80000000
Private Const OPEN_ALWAYS = 4
Public Enum SHARE_MODES
FILE_SHARE_EXCLUSIVE = 0
FILE_SHARE_READ = 1
FILE_SHARE_WRITE = 2
FILE_SHARE_DELETE = 4
FILE_SHARE_ALL = FILE_SHARE_READ Or FILE_SHARE_WRITE Or FILE_SHARE_DELETE
End Enum
#If False Then
Dim FILE_SHARE_EXCLUSIVE, FILE_SHARE_READ, FILE_SHARE_WRITE, FILE_SHARE_DELETE, FILE_SHARE_ALL
#End If
Private Declare Function CloseHandle Lib "kernel32" ( _
ByVal hObject As Long) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileW" ( _
ByVal lpFileName As Long, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As SHARE_MODES, _
ByVal lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As Long
Private Declare Function GetFileSizeEx Lib "kernel32" ( _
ByVal hFile As Long, _
ByRef curFileSize As Currency) As Long
Private Sub Form_Load()
Const TEST_FILE As String = "D:\VHDs\Vista w IE8\Vista SP1 x86 with IE8 2009-Apr.vhd"
Dim hFile As Long
Dim curFileSize As Currency
Dim MBFileSize As Double
hFile = CreateFile(StrPtr(TEST_FILE), _
GENERIC_READ, _
FILE_SHARE_ALL, _
0&, _
OPEN_ALWAYS, _
0&, _
0&)
GetFileSizeEx hFile, curFileSize
CloseHandle hFile
MBFileSize = CDbl(curFileSize) / (1000000 / 10000)
AutoRedraw = True
ScaleLeft = -60
ScaleTop = -60
Cls
Print TEST_FILE
Print Format$(MBFileSize, "#,##0.000 \M\B")
End Sub
That uses a Currency as a stand-in for a 64-bit integer type. The result is converted to a Double (or you might use a Decimal Variant) and then corrected by 10000 before finally dividing by 1000000.
Attachment 169627
Re: Division causes overflow
You might also want to show more detail:
Code:
Print Format$(MBFileSize, "#,##0.000000 \M\B")
Re: Division causes overflow
Thanks both of you very much!