From: Ian Williams on
RB Smissaert wrote:
> Hi Olaf,
>
> Had a better look now and yes, this knocks a few more percent off, more
> so when testing on a large file.
> This is the function I will be using then and thanks for the tip.
>
> Function FileSizeAPI(strFilePath As String) As Currency
>
> Dim hFind As Long
> Dim WFD As WIN32_FIND_DATA
>
> hFind = FindFirstFile(StrPtr(strFilePath), WFD)
>
> If hFind = -1& Then
> FileSizeAPI = -1@
> Else
> FindClose hFind
> If (WFD.nFileSizeHigh = 0&) And (WFD.nFileSizeLow > 0&) Then
> FileSizeAPI = WFD.nFileSizeLow
> Else
> WFD.dwReserved0 = WFD.nFileSizeHigh
> CopyMemory FileSizeAPI2, WFD.nFileSizeLow, 8
> FileSizeAPI = FileSizeAPI2 * 10000@
> End If
> End If
>
> End Function
>
>
> RBS
<snip>

Here's a slighter simpler version, no CopyMemory involved and no
redundant checking on the nFileSizeHigh value

Public Const INVALID_HANDLE_VALUE = -1
Public Const MAXDWORD = 4294967296#

Function FileSizeApi(ByVal FilePath As String) As Currency
Dim hFile As Long
Dim wfd As WIN32_FIND_DATA
hFile = FindFirstFile(FilePath, wfd)
If hFile <> INVALID_HANDLE_VALUE Then
FindClose hFile
With wfd
FileSizeApi = CCur(.nFileSizeHigh) * MAXDWORD + nFileSizeLow
End With
Else
FileSizeApi = -1
End If
End Function


Ian
From: Nobody on
"Ian Williams" <ian(a)kingsoft-dk.com> wrote in message
news:ubufu6FaKHA.4688(a)TK2MSFTNGP06.phx.gbl...
> Here's a slighter simpler version, no CopyMemory involved and no redundant
> checking on the nFileSizeHigh value
>
> Public Const INVALID_HANDLE_VALUE = -1
> Public Const MAXDWORD = 4294967296#
>
> Function FileSizeApi(ByVal FilePath As String) As Currency
> Dim hFile As Long
> Dim wfd As WIN32_FIND_DATA
> hFile = FindFirstFile(FilePath, wfd)
> If hFile <> INVALID_HANDLE_VALUE Then
> FindClose hFile
> With wfd
> FileSizeApi = CCur(.nFileSizeHigh) * MAXDWORD + nFileSizeLow
> End With
> Else
> FileSizeApi = -1
> End If
> End Function

Unfortunately, CConvert functions seem to be implemented in VB6 as function
calls. I have seen the assembly output for some of them(From memory CLng),
not CCur in particular. However, I believe that you can do what you want if
you declare the constant As Currency. Example:

Public Const MAXDWORD = 4294967296@

Or

Public Const MAXDWORD As Currency = 4294967296@

Then:

FileSizeApi = MAXDWORD * .nFileSizeHigh + .nFileSizeLow



From: Bob Butler on

"Ian Williams" <ian(a)kingsoft-dk.com> wrote in message
news:ubufu6FaKHA.4688(a)TK2MSFTNGP06.phx.gbl...
> RB Smissaert wrote:
<cut>
>> If (WFD.nFileSizeHigh = 0&) And (WFD.nFileSizeLow > 0&) Then
>> FileSizeAPI = WFD.nFileSizeLow
<cut>
> FileSizeApi = CCur(.nFileSizeHigh) * MAXDWORD + nFileSizeLow

If the file size is between &H80000000 and &HFFFFFFFF then won't
nFileSizeLow be a negative value?


From: Ian Williams on
Nobody wrote:
<
> Unfortunately, CConvert functions seem to be implemented in VB6 as function
> calls. I have seen the assembly output for some of them(From memory CLng),
> not CCur in particular. However, I believe that you can do what you want if
> you declare the constant As Currency. Example:
>
> Public Const MAXDWORD = 4294967296@
>
> Or
>
> Public Const MAXDWORD As Currency = 4294967296@
>
> Then:
>
> FileSizeApi = MAXDWORD * .nFileSizeHigh + .nFileSizeLow
>
>
>

It was the IDE that changed the type declaration to Double.

I've also spotted that the function messes up when checking a file which
is between 2GB & 4GB - you get a negative result, so here's one that works.

Ian

Public Const MAXDWORD As Currency = 4294967296@

Function FileSizeApi(ByVal FilePath As String) As Currency
Dim hFile As Long
Dim wfd As WIN32_FIND_DATA
hFile = FindFirstFile(FilePath, wfd)
If hFile <> INVALID_HANDLE_VALUE Then
FindClose hFile
With wfd
If .nFileSizeLow < 0 Then
FileSizeApi = MAXDWORD + .nFileSizeLow
Else
FileSizeApi = .nFileSizeLow
End If
FileSizeApi = MAXDWORD * .nFileSizeHigh + FileSizeApi
End With
Else
FileSizeApi = INVALID_HANDLE_VALUE
End If
End Function
From: bart.smissaert on
OK, can't remember getting wrong results with the code I posted last,
but maybe I missed that particular range.
Will check it out.
I take it it still makes sense to go with FindFirstFileW

RBS


> It was the IDE that changed the type declaration to Double.
>
> I've also spotted that the function messes up when checking a file which
> is between 2GB & 4GB - you get a negative result, so here's one that works.
>
> Ian
>
> Public Const MAXDWORD As Currency = 4294967296@
>
> Function FileSizeApi(ByVal FilePath As String) As Currency
> Dim hFile As Long
> Dim wfd As WIN32_FIND_DATA
> hFile = FindFirstFile(FilePath, wfd)
> If hFile <> INVALID_HANDLE_VALUE Then
>      FindClose hFile
>      With wfd
>          If .nFileSizeLow < 0 Then
>              FileSizeApi = MAXDWORD + .nFileSizeLow
>          Else
>              FileSizeApi = .nFileSizeLow
>          End If
>          FileSizeApi = MAXDWORD * .nFileSizeHigh + FileSizeApi
>      End With
> Else
>      FileSizeApi = INVALID_HANDLE_VALUE
> End If
> End Function