Link to home
Start Free TrialLog in
Avatar of trekrok
trekrok

asked on

Update VBA macro from 32 bit to 64 bit

My knowledge of this code is minimal.

The Declare statements need updated with PtrSafe along with the 'pointer/handles' changed to LongPtr, I think. I don't understand what  a pointer or handle is, so I'm confused as to where I need to make this change. I don't believe I should change all the 'Long' to 'LongPtr', so how can I determine which need updated and which do not? As always, any help is appreciated.

Option Explicit

Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long

Const MAX_PATH = 260
Const MAXDWORD = &HFFFF
Const INVALID_HANDLE_VALUE = -1
Const FILE_ATTRIBUTE_ARCHIVE = &H20
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_SYSTEM = &H4
Const FILE_ATTRIBUTE_TEMPORARY = &H100

Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * MAX_PATH
    cAlternate As String * 14
End Type


Private mcolFiles As Collection
Private mlngFileCount As Long

Function StripNulls(OriginalStr As String) As String
    If (InStr(OriginalStr, Chr(0)) > 0) Then
        OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
    End If
    StripNulls = OriginalStr
End Function

Function FindFilesAPI(path As String, SearchStr As String, FileCount As Variant, DirCount As Variant)
   
    Dim strFile As String ' Walking strFile variable...
    Dim strDirName As String ' SubDirectory Name
    Dim strDirNames() As String ' Buffer for directory name entries
    Dim intDir As Integer ' Number of directories in this path
    Dim i As Integer ' For-loop counter...
    Dim hSearch As Long ' Search Handle
    Dim WFD As WIN32_FIND_DATA
    Dim intCont As Integer
    If Right(path, 1) <> "\" Then path = path & "\"
    ' Search for subdirectories.
    intDir = 0
    ReDim strDirNames(intDir)
    intCont = True
    hSearch = FindFirstFile(path & "*", WFD)
    If hSearch <> INVALID_HANDLE_VALUE Then
        Do While intCont
        strDirName = StripNulls(WFD.cFileName)
        ' Ignore the current and encompassing directories.
        If (strDirName <> ".") And (strDirName <> "..") Then
            ' Check for directory with bitwise comparison.
            If GetFileAttributes(path & strDirName) And FILE_ATTRIBUTE_DIRECTORY Then
                strDirNames(intDir) = strDirName
                DirCount = DirCount + 1
                intDir = intDir + 1
                ReDim Preserve strDirNames(intDir)
            End If
        End If
        intCont = FindNextFile(hSearch, WFD) 'Get next subdirectory.
        Loop
        intCont = FindClose(hSearch)
    End If
    ' Walk through this directory and sum file sizes.
    hSearch = FindFirstFile(path & SearchStr, WFD)
    intCont = True
    If hSearch <> INVALID_HANDLE_VALUE Then
        While intCont
            strFile = StripNulls(WFD.cFileName)
            If (strFile <> ".") And (strFile <> "..") Then
                FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
                FileCount = FileCount + CDbl(1)
                mlngFileCount = mlngFileCount + 1
                mcolFiles.Add path & strFile
            End If
            intCont = FindNextFile(hSearch, WFD) ' Get next file
        Wend
        intCont = FindClose(hSearch)
    End If
    ' If there are sub-directories...
    If intDir > 0 Then
        ' Recursively walk into them...
        For i = 0 To intDir - 1
            FindFilesAPI = FindFilesAPI + FindFilesAPI(path & strDirNames(i) & "\", SearchStr, FileCount, DirCount)
        Next i
    End If
End Function

Public Function fGetFilesRecurse(strPath As String, strPattern As String, dblTotalSize As Double) As Collection
    Dim dblFileCount As Double
    Dim dblDirCount As Double
   
   
    Set mcolFiles = New Collection
    dblTotalSize = FindFilesAPI(strPath, strPattern, dblFileCount, dblDirCount)
   
    Set fGetFilesRecurse = mcolFiles
    Set mcolFiles = Nothing

End Function

Public Function fNumFiles() As Long
    fNumFiles = mlngFileCount
End Function
End Function



End Function

End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trekrok
trekrok

ASKER

This seems to work! Thanks!
A handle is a unique identifier for an object or an element, a unique number. It may be a 32bit or a 64bit number.
A VBA "longptr" is an integer variable, which is in 32bit environment 32bit long, in 64bit environment 64bit long. Most often the varible type longptr is used for pointers to memory adresses. But it's also used for other purpose, where the length has changed from 32bit to 64bit with the change of the bitness of the application. So for handles the variable type longptr has to be used, even if it's not a pointer to memory address, but a "pointer" to an object/element. The handle to the object, which is created by FindFirstFile, used by FindNextFile and released by FindClose is such an element, for which in a 64bit environment a 64bit number is used, for 32bit environment a 32bit number.
For memory addresses it's obvious, that the length has changed from 32bit to 64bit with the bitness of the application. Where else such a change was done by Microsoft, needs to be looked up in the documentation of Windows-64bits.
For the hFindFile (return value of FindFirstFile and Parameter in FindNextFile and FindClose) some internet pages are using Long, some other LongPtr. In the moment I don't know, what's correct. That it's a LongPtr in the FindFirstFile return value and a Long in FindNextFile and FindClose parameters in the above Declare lines is clearly a mistake.