Declaring API functions in 64 bit Office
Introduction
With the introduction of Windows 7 and Office 2010 VBA developers face a new challenge: ensuring their applications work on both 32 bit and 64 bit platforms.
This page is meant to become the first stop for anyone who needs the proper syntax for his API declaration statement in Office VBA.
Most of the declarations placed here when I first wrote the article were figured out by Charles Williams of www.decisionmodels.com when he created the 64 bit version of our Name Manager (to be published soon).
Links
Of course Microsoft documents how to do this. There is an introductory article on Microsoft MSDN:
Compatibility Between the 32-bit and 64-bit Versions of Office 2010
That article describes the how-to's to properly write the declarations. What is missing is which type declarations go with which API function or sub.
Microsoft also published a tool to check your code for 64 bit related problems, called the Microsoft Office Code Compatibility inspector addin.
Declarations by API function
| Function name | Declarations (32 bit followed by 64 bit) |
| FindWindow | Private Declare Function FindWindow Lib "USER32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare PtrSafe Function FindWindow Lib "USER32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr |
| getDC |
Private Declare Function GetDC Lib "USER32" (ByVal hWnd As Long) As Long
Private Declare PtrSafe Function GetDC Lib "USER32" (ByVal hWnd As LongPtr) As LongPtr |
| getDeviceCaps |
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
Private Declare PtrSafe Function GetDeviceCaps Lib "gdi32" (ByVal hDC As LongPtr, ByVal nIndex As Long) As Long |
| getFrequency |
Declare Function getFrequency Lib "kernel32" Alias "QueryPerformanceFrequency" (cyFrequency As Currency) As Long
Private Declare PtrSafe Function getFrequency Lib "kernel32" Alias "QueryPerformanceFrequency" (cyFrequency As Currency) As Long |
| GetKeyState |
Declare Function GetKeyState Lib "USER32" (ByVal vKey As Long) As Integer Declare PtrSafe Function GetKeyState Lib "USER32" (ByVal vKey As Long) As Integer |
| GetSystemMetrics |
Private Declare Function GetSystemMetrics Lib "USER32" (ByVal nIndex As Long) As Long
Private Declare PtrSafe Function GetSystemMetrics Lib "USER32" (ByVal nIndex As Long) As Long |
| getTickCount |
Private Declare Function getTickCount Lib "kernel32" Alias "QueryPerformanceCounter" (cyTickCount As Currency) As Long
Private Declare PtrSafe Function getTickCount Lib "kernel32" Alias "QueryPerformanceCounter" (cyTickCount As Currency) As Long ' |
| getTime |
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Private Declare PtrSafe Function timeGetTime Lib "winmm.dll" () As Long |
| GetWindowLongptr |
Private Declare Function GetWindowLongptr Lib "USER32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long Private Declare PtrSafe Function GetWindowLongptr Lib "USER32" Alias "GetWindowLongA" (ByVal hWnd As LongPtr, ByVal nIndex As Long) As LongPtr |
| IsCharAlphaNumericA |
Private Declare Function IsCharAlphaNumericA Lib "USER32" (ByVal byChar As Byte) As Long Private Declare PtrSafe Function IsCharAlphaNumericA Lib "USER32" (ByVal byChar As Byte) As Long |
| ReleaseDC |
Private Declare Function ReleaseDC Lib "USER32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
Private Declare PtrSafe Function ReleaseDC Lib "USER32" (ByVal hWnd As LongPtr, ByVal hDC As LongPtr) As Long |
| SetWindowLongPtr |
Private Declare Function SetWindowLongPtr Lib "USER32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Private Declare PtrSafe Function SetWindowLongPtr Lib "USER32" Alias "SetWindowLongA" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr |
| timeGetTime |
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Private Declare PtrSafe Function timeGetTime Lib "winmm.dll" () As Long |
Other API functions
Have a function declaration which is not on this list? I invite you to send me your (working and tested!!!) declarations so I can add them here.
I also welcome comments and suggestions on improvements!




Comments
All comments about this page:
Comment by: Alexander Wolff (1/28/2010 12:01:43 PM)On a first glance:
- PtrSafe before "Function"
- LongPtr instead of "Long" (in some cases)
IsCharAlphaNumericA: "#Else" is probably not intended to be there?
Comment by: Jan Karel Pieterse (1/29/2010 2:40:08 AM)Hi Alexander,
Thanks for spotting the anomalies. Care to tell me which Long's must become LongPtr's?
Have a question, comment or suggestion? Then please use this form.