PDA

View Full Version : [SOLVED] Change VBA code to fit 64-bit environment



Bo Hansson
11-02-2017, 05:19 AM
Following declarations from my earlier 32-bit environment must be corrected, but I dont know how. Please help...


Declare Function FindWindowByClass Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, ByVal _
lpWindowName As Long) As Long


Declare Function RegisterWindowMessage Lib "user32" Alias _
"RegisterWindowMessageA" (ByVal lpstring As String) As Long


Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As Any, ByVal _
lpWindowName As Any) As Long


Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, ByVal _
wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

mancubus
11-04-2017, 02:17 PM
Declare PtrSafe Function FindWindowByClass Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, ByVal _
lpWindowName As Long) As LongPtr


Declare PtrSafe Function RegisterWindowMessage Lib "user32" Alias _
"RegisterWindowMessageA" (ByVal lpstring As String) As LongPtr


Declare PtrSafe Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As Any, ByVal _
lpWindowName As Any) As LongPtr


Declare PtrSafe Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, ByVal _
wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPtr


http://www.jkp-ads.com/articles/apideclarations.asp

Bo Hansson
11-05-2017, 03:10 AM
Tanks!

mancubus
11-05-2017, 01:08 PM
you are welcome. if you are sorted, pls mark the thread as solved for future references.

Aflatoon
11-06-2017, 01:53 PM
The SendMessage one should be:


Public Declare PtrSafe Function SendMessageA Lib "user32" (ByVal hWnd As LongPtr, ByVal wMsg As Long, _
ByVal wParam As LongPtr, lParam As Any) As LongPtr

Kenneth Hobs
11-06-2017, 02:05 PM
JKP's link is a good one to show several of those and how to do both types.

Since this has so many views, some might like those listed at.
https://support.microsoft.com/en-us/help/2030490/office-2010-help-files-win32api-ptrsafe-with-64-bit-support

Rolly_Sefu
07-05-2019, 02:02 AM
Hy, i have a similar code lines that worked for 32 but don't work for 64.

Can you please have a look, maybe you know how to modify them:


Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Long, Source As Long, ByVal Length As Long)Private Declare Function VirtualProtect Lib "kernel32" (lpAddress As Long, ByVal dwSize As Long, ByVal flNewProtect As Long, lpflOldProtect As Long) As Long
Private Declare Function GetModuleHandleA Lib "kernel32" (ByVal lpModuleName As String) As Long
Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Private Declare Function DialogBoxParam Lib "user32" Alias "DialogBoxParamA" (ByVal hInstance As Long, ByVal pTemplateName As Long, ByVal hWndParent As Long, ByVal lpDialogFunc As Long, ByVal dwInitParam As Long) As Integer


Thank you.

Rolly_Sefu
07-11-2019, 02:33 AM
Hello, i have the code lines below that help me protect multiple sheets at the same time but this only worked for office 32 bit


Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As LongPrivate Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Declare Function SendDlgItemMessage Lib "user32" Alias "SendDlgItemMessageA" (ByVal hDlg As Long, ByVal nIDDlgItem As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long

Any ideas how to change this to work for 64 bit ?
Thanks.

Bob Phillips
07-11-2019, 11:34 AM
Private Declare PtrSafe Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
Destination Destination As LongPtr, _
Source As LongPtr, _
ByValByVal Length As LongPtr)
Private DeclarePtrSafe Function VirtualProtect Lib "kernel32" ( _
lpAddress As LongPtr, _
ByVal dwSize As LongPtr, _
ByVal flNewProtect As LongPtr, _
lpflOldProtect As LongPtr) As LongPtr
Private Declare PtrSafe Function GetModuleHandleA Lib "kernel32" ( _
ByVal lpModuleName As String) As LongPtr
Private Declare PtrSafe Function GetProcAddress Lib "kernel32" ( _
ByVal hModule As LongPtr, _
ByVal lpProcName As String) As LongPtr
Private Declare PtrSafe Function DialogBoxParam Lib "user32" Alias "DialogBoxParamA" ( _
ByVal hInstance As LongPtr, _
ByVal pTemplateName As LongPtr, _
ByVal hWndParent As LongPtr, _
ByVal lpDialogFunc As LongPtr, _
ByVal dwInitParam As LongPtr) As Integer

Bob Phillips
07-11-2019, 11:37 AM
Private Declare PtrSafe Function CallNextHookEx Lib "user32" ( _
hHook As LongPtr, _
ncode As LongPtr, _
wParam As LongPtr, lParam As Any) As LongPtr
Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" ( _
lpModuleName As String) As LongPtr
Private Declare PtrSafe Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" ( _
idHook As LongPtr, _
lpfn As LongPtr, _
hmod As LongPtr, _
dwThreadId As LongPtr) As LongPtr
Private Declare PtrSafe Function UnhookWindowsHookEx Lib "user32" ( _
hHook As LongPtr) As LongPtr
Private Declare PtrSafe Function SendDlgItemMessage Lib "user32" Alias "SendDlgItemMessageA" ( _
hDlg As LongPtr, _
nIDDlgItem As LongPtr, _
wMsg As LongPtr, _
wParam As LongPtr, _
lParam As LongPtr) As LongPtr
Private Declare PtrSafe Function GetClassName Lib "user32" Alias "GetClassNameA" ( _
hWnd As LongPtr, _
lpClassName As String, _
nMaxCount As LongPtr) As LongPtr
Private Declare PtrSafe Function GetCurrentThreadId Lib "kernel32" () As LongPtr

Rolly_Sefu
07-12-2019, 05:39 AM
Great, thanks :)

nimesh29
02-17-2020, 03:19 PM
I had to deal with same issue and below is the solution that worked;


Option Explicit

'Windows API calls to do all the dirty work!
#If Win64 Then
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare PtrSafe Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Declare PtrSafe Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Private Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare PtrSafe Function EnableWindow Lib "user32" (ByVal hwnd As Long, ByVal fEnable As Long) As Long
Private Declare PtrSafe Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
Private Declare PtrSafe Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long
Private Declare PtrSafe Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Long) As Long
Private Declare PtrSafe Function LockWindowUpdate Lib "user32" (ByVal hWndLock As Long) As Long


#Else
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function EnableWindow Lib "user32" (ByVal hwnd As Long, ByVal fEnable As Long) As Long
Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Long) As Long
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hWndLock As Long) As Long
#End If

Aflatoon
02-19-2020, 05:59 AM
@nimesh29

Pretty much all of those 64bit calls are actually wrong. I suggest you have a look at Jan Karel's site linked earlier in the thread.

DanWall
12-02-2020, 11:23 AM
Forgive me if this is obvious to everyone, is there a tool or a macro or something that can lookup/replace the conversions for me? I`m thinking of a macro that, for example, will either regex or parse its ways through the VBComponents and replace the API function/sub with its PtrSafe equivalent. I`m getting tired of constantly checking Jan Karel`s site (as great as it is) or the Win32API_PtrSafe.txt file. I`ve started to cobble something together than will do the task for me, but would rather not reinvent the wheel if I can avoid it.

Bob Phillips
12-02-2020, 12:28 PM
I don't know of anything, but I am surprised if no-one has, and even more surprised I haven't built such a thing myself. Maybe a new project.

Paul_Hossler
12-02-2020, 01:18 PM
The new Readiness Toolkit is better than the previous one

It doesn't fix the code, but does give you the suggested changes


Use the Readiness Toolkit to assess application compatibility for Microsoft 365 Apps - Deploy Office | Microsoft Docs (https://docs.microsoft.com/en-us/deployoffice/readiness-toolkit-application-compatibility-microsoft-365-apps)



Use the Readiness Toolkit to assess application compatibility for Microsoft 365 Apps


The Readiness Toolkit for Office add-ins and VBA can help you identify compatibility issues with your Microsoft Visual Basic for Applications (VBA) macros and add-ins that you use with Office. The Readiness Toolkit includes the Readiness Report Creator, which creates an Excel report with VBA macro compatibility and add-in readiness information to help your organization assess its readiness to move to Microsoft 365 Apps.


You can download the Readiness Toolkit for free from the Microsoft Download Center (https://go.microsoft.com/fwlink/p/?linkid=859119). We recommend that you always download and use the most current version. The Readiness Toolkit checks if you're using the most current version when you run a report and will prompt you to download the most current version. You don't have to uninstall the older version of the Readiness Toolkit before installing the most current version.

The Readiness Toolkit doesn't repair or fix the code in your VBA macros. If you create an advanced report, the report does provide guidance, when available, for remediating your VBA macro code.



27523

27524

DanWall
04-09-2022, 10:24 AM
Would you believe I've been looking for this thread for the longest time, and today I stumbled across it looking for something else. I had meant to thank Paul Hossier for his suggestion. I tried it out but it (surprisingly) missed quite a few obvious API Declarations - though, as you say, the suggestions are useful guides. Since I asked the question, I've ended up coding my own solution (or more accurately, solutions) - it's been a fascinating exploration into API Declarations. I'm wondering - Bob Phillips - did you ever take on the challenge, after all?

arick111
04-13-2023, 12:04 AM
Following declarations from my earlier 32-bit environment must be corrected, but I dont know how. Please help...


Declare Function FindWindowByClass Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, ByVal _
lpWindowName As Long) As Long


Declare Function RegisterWindowMessage Lib "user32" Alias _
"RegisterWindowMessageA" (ByVal lpstring As String) As Long


Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As Any, ByVal _
lpWindowName As Any) As Long


Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, ByVal _
wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

This is a Visual Basic for Applications (VBA) code that declares several functions from the "user32" library. These functions can be used to find windows and send messages to them. The code is incomplete and missing some parameters for the functions, (https://gbplus.net/download-gbwhatsapp-apk/) so it may not compile or work correctly without further modifications.

Aussiebear
04-13-2023, 12:07 PM
This thread is over 6 years old. I am closing this thread because we keeping mulling over the very same points. Arick111, if you wish to follow the intent of the thread please follow the link as indicated in post #2.