64 Bit Compile error

towners

Board Regular
Joined
Mar 12, 2009
Messages
225
Office Version
  1. 365
Platform
  1. Windows
Hi,

It's been a while since I used an old application I put together and after a few years when I open the app I received a compile error saying my code must be updated for 64 but systems.

The code in question, if I can remember correctly sets up the initial display and restricts user control. Can anyone help me to update this?

Code:
Private Declare Function GetSystemMenu _
Lib "user32" ( _
ByVal hWnd As Long, _
ByVal bRevert As Long) _
As Long


Private Declare Function RemoveMenu _
Lib "user32" ( _
ByVal hMenu As Long, _
ByVal nPosition As Long, _
ByVal wFlags As Long) _
As Long


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


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 FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
    ByVal hWnd1 As Long, _
    ByVal hWnd2 As Long, _
    ByVal lpsz1 As String, _
    ByVal lpsz2 As String) As Long
    
Private Declare Function DrawMenuBar Lib "user32" ( _
    ByVal hWnd As Long) As Long


Private Declare Function GetMenuItemCount Lib "user32" ( _
    ByVal hMenu As Long) As Long
    
Private Declare Function EnableMenuItem Lib "user32" ( _
    ByVal hMenu As Long, _
    ByVal wIDEnableItem As Long, _
    ByVal wEnable As Long) As Long

Thanks

Towners
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi there,

Certainly a 'rookie' with declared functions, but I believe your error means you have Excel installed in 64 bit mode. Anyways, you can download Win32API_PtrSafe.TXT from https://www.microsoft.com/en-us/download/details.aspx?id=9970

If anyone who uses the file uses Excel in its default installation (32-bit), then you will want to conditionally compile the declared functions.

Hope that helps,

Mark
 
Upvote 0
This should handle the declarations of the API functions so that it will work on either 32 or 64bit Office, but you will also need to update the routines that actually use these, since you will have variables that need to be adjusted from Long to LongPtr:

Code:
#If Win64 Then
    Private Declare PtrSafe Function GetSystemMenu Lib "user32" (ByVal hWnd As LongPtr, _
                                                                 ByVal bRevert As Long) As LongPtr

    Private Declare PtrSafe Function RemoveMenu Lib "user32" (ByVal hMenu As LongPtr, _
                                                              ByVal nPosition As Long, _
                                                              ByVal wFlags As Long) As Long

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

    Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongPtrA" (ByVal hWnd As LongPtr, _
                                                                                        ByVal nIndex As Long) As LongPtr

    Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongPtrA" (ByVal hWnd As LongPtr, _
                                                                                ByVal nIndex As Long, _
                                                                                ByVal dwNewLong As Long) As LongPtr

    Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, _
                                                                                      ByVal hWnd2 As LongPtr, _
                                                                                      ByVal lpsz1 As String, _
                                                                                      ByVal lpsz2 As String) As LongPtr

    Private Declare PtrSafe Function DrawMenuBar Lib "user32" (ByVal hWnd As LongPtr) As Long


    Private Declare PtrSafe Function GetMenuItemCount Lib "user32" (ByVal hMenu As LongPtr) As Long

    Private Declare PtrSafe Function EnableMenuItem Lib "user32" (ByVal hMenu As LongPtr, _
                                                                  ByVal wIDEnableItem As Long, _
                                                                  ByVal wEnable As Long) As Long
#Else
    Private Declare Function GetSystemMenu _
                          Lib "user32" ( _
                              ByVal hWnd As Long, _
                              ByVal bRevert As Long) _
                              As Long


    Private Declare Function RemoveMenu _
                          Lib "user32" ( _
                              ByVal hMenu As Long, _
                              ByVal nPosition As Long, _
                              ByVal wFlags As Long) _
                              As Long


    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 FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
                                          ByVal hWnd1 As Long, _
                                          ByVal hWnd2 As Long, _
                                          ByVal lpsz1 As String, _
                                          ByVal lpsz2 As String) As Long

    Private Declare Function DrawMenuBar Lib "user32" ( _
                                         ByVal hWnd As Long) As Long


    Private Declare Function GetMenuItemCount Lib "user32" ( _
                                              ByVal hMenu As Long) As Long

    Private Declare Function EnableMenuItem Lib "user32" ( _
                                            ByVal hMenu As Long, _
                                            ByVal wIDEnableItem As Long, _
                                            ByVal wEnable As Long) As Long

#End If
 
Last edited:
Upvote 0
Hi again (and a Howdy to Rory as well),

You will probably spot it right away, but just in case not, include the PtrSafe attribute when calling SetWindowLongPtrA in the part for 64-bit.

Mark
 
Upvote 0
Thank you very much both. I'll work through the changes and advice.

Towners
 
Upvote 0
Thanks Mark - well spotted.
 
Upvote 0
Gosh, I am just happy to spot anything this late at night (still on graves...). Have a great day Rory :)
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,255
Members
448,556
Latest member
peterhess2002

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top