PtrSafe
The PtrSafe attribute indicates to the VBA compiler that the Declare statement is targeted for the 64–bit version of Office. Without this attribute, using the Declare statement in a 64–bit system will result in a compile-time error.
Conversion and Types
- PtrSafe: Shows that the Declare statement is compatible with 64-bits. This attribute is mandatory on 64-bit systems.
- LongPtr: Variable data type which is a 4-bytes data type on 32-bit versions and an 8-byte data type on 64-bit versions of Microsoft Office. This is the recommended way of declaring a pointer or a handle for new code but also for legacy code if it has to run in the 64-bit version of Office. It is only supported in the VBA 7 runtime on 32-bit and 64-bit. Note that you can assign numeric values to it but not numeric types.
- LongPtr: An 8-byte data type which is available only in 64-bit versions of Microsoft Office. You can assign numeric values but not numeric types (to avoid truncation).
- CLngPtr: Converts a simple expression to a LongPtr data type.
- CLngLng: Converts a simple expression to a LongLong data type.
- VarPtr: Variant converter. Returns a LongPtr on 64-bit versions, and a Long on 32-bit versions (4 bytes).
- ObjPtr: Object converter. Returns a LongPtr on 64-bit versions, and a Long on 32-bit versions (4 bytes).
- StrPtr: String converter. Returns a LongPtr on 64-bit versions, and a Long on 32-bit versions (4 bytes).
Declare code example
Private PtrSafe Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private PtrSafe Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hWnd As LongPtr, ByVal lpString As String, ByVal cch As LongPtr) As Long Private PtrSafe Declare Function GetWindowTextLength Lib "User32" Alias "GetWindowTextLengthA" (ByVal hWnd As LongPtr) As Long Private PtrSafe Declare Function GetWindow Lib "User32" (ByVal hWnd As LongPtr, ByVal wCmd As LongPtr) As Long Private PtrSafe Declare Function IsWindowVisible Lib "User32" (ByVal hWnd As LongPtr) As Boolean
Compatible code example
#if Win64 then Private PtrSafe Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private PtrSafe Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hWnd As LongPtr, ByVal lpString As String, ByVal cch As LongPtr) As Long Private PtrSafe Declare Function GetWindowTextLength Lib "User32" Alias "GetWindowTextLengthA" (ByVal hWnd As LongPtr) As Long Private PtrSafe Declare Function GetWindow Lib "User32" (ByVal hWnd As LongPtr, ByVal wCmd As LongPtr) As Long Private PtrSafe Declare Function IsWindowVisible Lib "User32" (ByVal hWnd As LongPtr) As Boolean #else Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Private Declare Function GetWindowTextLength Lib "User32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long Private Declare Function GetWindow Lib "User32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long Private Declare Function IsWindowVisible Lib "User32" (ByVal hWnd As Long) As Boolean #end if #if VBA7 then Private PtrSafe Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private PtrSafe Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hWnd As LongPtr, ByVal lpString As String, ByVal cch As LongPtr) As Long Private PtrSafe Declare Function GetWindowTextLength Lib "User32" Alias "GetWindowTextLengthA" (ByVal hWnd As LongPtr) As Long Private PtrSafe Declare Function GetWindow Lib "User32" (ByVal hWnd As LongPtr, ByVal wCmd As LongPtr) As Long Private PtrSafe Declare Function IsWindowVisible Lib "User32" (ByVal hWnd As LongPtr) As Boolean #else Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Private Declare Function GetWindowTextLength Lib "User32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long Private Declare Function GetWindow Lib "User32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long Private Declare Function IsWindowVisible Lib "User32" (ByVal hWnd As Long) As Boolean #end if