32bit and 64bit?

Gasman

Enthusiastic Amateur
Local time
Today, 09:28
Joined
Sep 21, 2011
Messages
16,450
Hi all,
In attempting to help another member on another site, ChatGPT gave me code but for 64bit, and I am on 32bit Office.

I amended that code to have the conditional compile directive VBA7, and that appeared to work.
The code was to close a particular window with a known title.

I then thought about finding out the actual name of the title of the window we are loooking for, which generally is in the form Filename - Application Name.
However despite using VBA7 to determine which code to call, 32bit or 64bit, it was always calling the 64bit code.

I changed it to WIN64 and now it behaves as it should (for 32bit at least)
So when should you use one or the other? My impression was VB7 was suffcient from what I have see from other code?

Code:
Option Compare Database
Option Explicit


#If VBA7 Then
' For window closing
    Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _
               ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr

    Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" ( _
               ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, _
               ByVal lParam As LongPtr) As LongPtr
' For window finding
    Declare PtrSafe Function EnumWindows Lib "user32" ( _
               ByVal lpEnumFunc As LongPtr, _
               ByVal lParam As LongPtr) As Long

    Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" ( _
               ByVal hwnd As LongPtr, _
               ByVal lpString As String, _
               ByVal cch As Long) As Long

    Declare PtrSafe Function IsWindowVisible Lib "user32" ( _
               ByVal hwnd As LongPtr) As Long

#Else
' For window closing
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
                                           ByVal lpClassName As String, ByVal lpWindowName As String) As Long

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
                                            ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
                                            ByVal lParam As Long) As Long
' For window finding
    
    Declare Function EnumWindows Lib "user32" ( _
                                    ByVal lpEnumFunc As Long, _
                                    ByVal lParam As Long) As Long

    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" ( _
                                      ByVal hwnd As Long, _
                                      ByVal lpString As String, _
                                      ByVal cch As Long) As Long

    Declare Function IsWindowVisible Lib "user32" ( _
                                        ByVal hwnd As Long) As Long

#End If
Private Const WM_CLOSE = &H10


Sub CloseWindow(strFileName As String, strAppName As String)
    #If VBA7 Then
        Dim hwnd As LongPtr
    #Else
        Dim hwnd As Long
    #End If

    Dim strWindowTitle As String

    strWindowTitle = strFileName & " - " & strAppName
    hwnd = FindWindow(vbNullString, strWindowTitle)

    If hwnd <> 0 Then
        Call SendMessage(hwnd, WM_CLOSE, 0, 0)
    Else
        MSGBOX "Window " & strWindowTitle & " not found."
    End If
End Sub

Function EnumWindowsProc64(ByVal hwnd As LongPtr, ByVal lParam As LongPtr) As Long
    Dim title As String * 256
    If IsWindowVisible(hwnd) Then
        If GetWindowText(hwnd, title, Len(title)) > 0 Then
            Debug.Print Trim(title)
        End If
    End If
    EnumWindowsProc64 = 1 ' continue enumeration
End Function
Function EnumWindowsProc32(ByVal hwnd As Long, ByVal lParam As Long) As Long
    Dim title As String * 256
    If IsWindowVisible(hwnd) Then
        If GetWindowText(hwnd, title, Len(title)) > 0 Then
            Debug.Print Trim(title)
        End If
    End If
    EnumWindowsProc32 = 1 ' continue enumeration
End Function


Sub ListAllOpenWindowTitles()
#If Win64 Then
    EnumWindows AddressOf EnumWindowsProc64, 0
#Else
    EnumWindows AddressOf EnumWindowsProc32, 0
#End If
End Sub

Code in question is ListAllOpenWindowTitles()
 
Hi all,
In attempting to help another member on another site, ChatGPT gave me code but for 64bit, and I am on 32bit Office.

I amended that code to have the conditional compile directive VBA7, and that appeared to work.
The code was to close a particular window with a known title.

I then thought about finding out the actual name of the title of the window we are loooking for, which generally is in the form Filename - Application Name.
However despite using VBA7 to determine which code to call, 32bit or 64bit, it was always calling the 64bit code.

I changed it to WIN64 and now it behaves as it should (for 32bit at least)
So when should you use one or the other? My impression was VB7 was suffcient from what I have see from other code?
Code:
Option Compare Database
Option Explicit


#If VBA7 Then
' For window closing
    Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _
               ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr

    Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" ( _
               ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, _
               ByVal lParam As LongPtr) As LongPtr
' For window finding
    Declare PtrSafe Function EnumWindows Lib "user32" ( _
               ByVal lpEnumFunc As LongPtr, _
               ByVal lParam As LongPtr) As Long

    Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" ( _
               ByVal hwnd As LongPtr, _
               ByVal lpString As String, _
               ByVal cch As Long) As Long

    Declare PtrSafe Function IsWindowVisible Lib "user32" ( _
               ByVal hwnd As LongPtr) As Long

#Else
' For window closing
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
                                           ByVal lpClassName As String, ByVal lpWindowName As String) As Long

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
                                            ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
                                            ByVal lParam As Long) As Long
' For window finding
   
    Declare Function EnumWindows Lib "user32" ( _
                                    ByVal lpEnumFunc As Long, _
                                    ByVal lParam As Long) As Long

    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" ( _
                                      ByVal hwnd As Long, _
                                      ByVal lpString As String, _
                                      ByVal cch As Long) As Long

    Declare Function IsWindowVisible Lib "user32" ( _
                                        ByVal hwnd As Long) As Long

#End If
Private Const WM_CLOSE = &H10


Sub CloseWindow(strFileName As String, strAppName As String)
    #If VBA7 Then
        Dim hwnd As LongPtr
    #Else
        Dim hwnd As Long
    #End If

    Dim strWindowTitle As String

    strWindowTitle = strFileName & " - " & strAppName
    hwnd = FindWindow(vbNullString, strWindowTitle)

    If hwnd <> 0 Then
        Call SendMessage(hwnd, WM_CLOSE, 0, 0)
    Else
        MSGBOX "Window " & strWindowTitle & " not found."
    End If
End Sub

Function EnumWindowsProc64(ByVal hwnd As LongPtr, ByVal lParam As LongPtr) As Long
    Dim title As String * 256
    If IsWindowVisible(hwnd) Then
        If GetWindowText(hwnd, title, Len(title)) > 0 Then
            Debug.Print Trim(title)
        End If
    End If
    EnumWindowsProc64 = 1 ' continue enumeration
End Function
Function EnumWindowsProc32(ByVal hwnd As Long, ByVal lParam As Long) As Long
    Dim title As String * 256
    If IsWindowVisible(hwnd) Then
        If GetWindowText(hwnd, title, Len(title)) > 0 Then
            Debug.Print Trim(title)
        End If
    End If
    EnumWindowsProc32 = 1 ' continue enumeration
End Function


Sub ListAllOpenWindowTitles()
#If Win64 Then
    EnumWindows AddressOf EnumWindowsProc64, 0
#Else
    EnumWindows AddressOf EnumWindowsProc32, 0
#End If
End Sub

Code in question is ListAllOpenWindowTitles()
My understanding is that VBA7 is only useful in determining whether the accd is running in a version earlier than Access 2010. I.e. VBA7 was released with A2010 IIRC.

As people replace older versions of Office/Access, the VBA7 compiler directive is less and less useful. If you know users are going to be running your accdb on a pre-2010 installation of Access, it can be useful. Otherwise no.
 
However despite using VBA7 to determine which code to call, 32bit or 64bit, it was always calling the 64bit code.

That is exactly what will happen. The #If VBA7 clause is used for any app running 2010 or later (32-bit or 64-bit). Its not sprcifically 64-bit code.
The #Else clause is only needed if there are users running A2007 or earlier (VBA6)

See my series of 5 articles starting with
 
Well as you know, I am now on 2019. Previously 2007.
I changed the VBA7 to WIN64 and it still works. So from now on, should that be all I need to do, should I want any 64bit code to work on my DBs?
 
Not necessary. Read my comments again. Better still, read my articles.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom