An upgrade was done on the first of many computers in our ogranization; an 'upgrade' to a 64bit operating system, which caused the following error in one of my modules:
"Complile error: The code in this project must be updated for use on 64-bit systems. Please review and update Declare statements and then mark them with PtrSafe attribute."
The code below is what the error applied to. I am thinking I have to change something on the .dll? Anyway, I am going to need the code to work on both 32bit and 64bit operating systems for some time. Any help is greatly appreciated!
'Constants Const IMAGE_ICON As Long = &H1 Const WM_SETICON As Long = &H80 Const ICON_BIG As Long = &H1 'Constants for Load Image's fuLoad Parameter (Load Resource) Const LR_DEFAULTCOLOR As Long = &H0 Const LR_MONOCHROME As Long = &H1 Const LR_COLOR As Long = &H2 Const LR_COPYRETURNORG As Long = &H4 Const LR_COPYDELETEORG As Long = &H8 Const LR_LOADFROMFILE As Long = &H10 Const LR_LOADTRANSPARENT As Long = &H20 Const LR_DEFAULTSIZE As Long = &H40 Const LR_VGACOLOR As Long = &H80 Const LR_LOADMAP3DCOLORS As Long = &H1000 Const LR_CREATEDIBSECTION As Long = &H2000 Const LR_COPYFROMRESOURCE As Long = &H4000 Const LR_SHARED As Long = &H8000 'Returns an Icon from a File (.ico) Private Declare Function LoadImage _ Lib "user32.dll" Alias "LoadImageA" _ (ByVal hInst As Long, _ ByVal lpsz As String, _ ByVal uType As Long, _ ByVal cxDesired As Long, _ ByVal cyDesired As Long, _ ByVal fuLoad As Long) _ As Long 'Direct System what to do with the Window Private Declare Function SendMessage _ Lib "user32.dll" Alias "SendMessageA" _ (ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ lParam As Long) _ As Long 'Redraw the Icons on the Window's Title Bar Private Declare Function DrawMenuBar _ Lib "user32.dll" _ (ByVal hwnd As Long) _ As Long Public Function ChangeExcelIcon(ByVal Icon_File_Path As String) Dim hwnd As Long Dim hIcon As Long Dim LoadMask As Long hwnd = Application.hwnd LoadMask = LR_LOADFROMFILE Or LR_DEFAULTSIZE Or LR_SHARED hIcon = LoadImage(0&, Icon_File_Path, IMAGE_ICON, 32, 32, LoadMask) Call SendMessage(hwnd, WM_SETICON, ICON_BIG, ByVal hIcon) Call DrawMenuBar(hwnd) End Function
Last edited by jonvanwyk; 01-05-2012 at 11:34 AM. Reason: Clarity
That error is not related to a 64 bit operating system, but to using 64bit Office. I would suggest you read through this MSDN article. There is also a download available that lists many of the API declaration statements in both 32 and 64 bit syntax.
Good luck.
@onerrorgoto0: That is a good start at least to know that it is the version of Office and not the OS. My programming skills may not be sufficient to know on my own how to modify the delcaration statements to be compatible with both. So again, thank you to any one that can assist with this. I am sure it is probably something simple, I just have never done it before. I pride myself on just never having to ask the same question twice. It is how I learn![]()
I have been working on following through with OnErrorGoto0's suggestions, but I have insufficient administrative rights to run some of the stuff required to do it. any assistance offered with achieving resolution to this issue is greatly appreciated!
In 64-bit Office VBA, you'll need to declare API calls this way:
Private Declare PtrSafe Function LoadImage _ Lib "user32.dll" Alias "LoadImageA" _ (ByVal hInst As Long, _ ...etc
Does that help?
You will also need to replace any pointer arguments so that they are LongPtr rather than Long. I will see if I can put it together for you shortly (if the forum remains up) but I do not have 64bit Excel to test on, I am afraid, so I will have to leave that to you.
Good luck.
OK, based on the Microsoft documentation, I think the code should be:
Can you let me know if that works OK?'Constants Const IMAGE_ICON As Long = &H1 Const WM_SETICON As Long = &H80 Const ICON_BIG As Long = &H1 'Constants for Load Image's fuLoad Parameter (Load Resource) Const LR_DEFAULTCOLOR As Long = &H0 Const LR_MONOCHROME As Long = &H1 Const LR_COLOR As Long = &H2 Const LR_COPYRETURNORG As Long = &H4 Const LR_COPYDELETEORG As Long = &H8 Const LR_LOADFROMFILE As Long = &H10 Const LR_LOADTRANSPARENT As Long = &H20 Const LR_DEFAULTSIZE As Long = &H40 Const LR_VGACOLOR As Long = &H80 Const LR_LOADMAP3DCOLORS As Long = &H1000 Const LR_CREATEDIBSECTION As Long = &H2000 Const LR_COPYFROMRESOURCE As Long = &H4000 Const LR_SHARED As Long = &H8000 'Returns an Icon from a File (.ico) Private Declare Function LoadImage _ Lib "user32.dll" Alias "LoadImageA" _ (ByVal hInst As LongPtr, _ ByVal lpsz As String, _ ByVal uType As Long, _ ByVal cxDesired As Long, _ ByVal cyDesired As Long, _ ByVal fuLoad As Long) _ As LongPtr 'Direct System what to do with the Window Private Declare Function SendMessage _ Lib "user32.dll" Alias "SendMessageA" _ (ByVal hwnd As LongPtr, _ ByVal wMsg As Long, _ ByVal wParam As LongPtr, _ lParam As Long) _ As LongPtr 'Redraw the Icons on the Window's Title Bar Private Declare PtrSafe Function DrawMenuBar _ Lib "user32.dll" _ (ByVal hwnd As LongPtr) _ As Long Public Function ChangeExcelIcon(ByVal Icon_File_Path As String) Dim hwnd As LongPtr Dim hIcon As LongPtr Dim LoadMask As Long hwnd = Application.hwnd LoadMask = LR_LOADFROMFILE Or LR_DEFAULTSIZE Or LR_SHARED hIcon = LoadImage(0&, Icon_File_Path, IMAGE_ICON, 32, 32, LoadMask) Call SendMessage(hwnd, WM_SETICON, ICON_BIG, ByVal hIcon) Call DrawMenuBar(hwnd) End Function
Good luck.
If it should run on both 64 bit and older version you have to declare in an if statement.
When opening in 64 bit the else part will be marked with red and the other way round if it's not 64 bit.#If Win64 ' When declaring for 64 bit then you should do as ron said with ptrsafe #Else ' as you've done it so far #End if
This is how it's supposed to be.
Strictly speaking, as in the MS article I linked to, you only need #Win64 if you are using 64bit specific code - e.g. code that uses LongLong. For the rest you only need the #VBA7 constant, since 32bit Office 2010 is fine with LongPtr and PtrSafe.
Good luck.
@onErrorGoto0: Thank you very much for taking such time to assist me. I also do not have 64bit Office, so will have to wait to test your code on the computer that has it when it becomes available. I will keep you posted as I know more...and thank you again for your help!
PtrSafe Compile Error.jpg
I installed your code into module 3 of my workbook (where it was originally located before modification). Before I could text it on the 64bit Office, it gave me the attached Complile Error on my 32bit version. Thoughts?
Per the article I linked to, if you do not have office 2010 you will need conditional compilation since VBA6 has no concept of PtrSafe or any 64bit compatibility issues.
Good luck.
I am sorry. I have attempted multiple ways of accomplishing this, but am still at a loss. :-/
Try this
Option Explicit 'Constants Const IMAGE_ICON As Long = &H1 Const WM_SETICON As Long = &H80 Const ICON_BIG As Long = &H1 'Constants for Load Image's fuLoad Parameter (Load Resource) Const LR_DEFAULTCOLOR As Long = &H0 Const LR_MONOCHROME As Long = &H1 Const LR_COLOR As Long = &H2 Const LR_COPYRETURNORG As Long = &H4 Const LR_COPYDELETEORG As Long = &H8 Const LR_LOADFROMFILE As Long = &H10 Const LR_LOADTRANSPARENT As Long = &H20 Const LR_DEFAULTSIZE As Long = &H40 Const LR_VGACOLOR As Long = &H80 Const LR_LOADMAP3DCOLORS As Long = &H1000 Const LR_CREATEDIBSECTION As Long = &H2000 Const LR_COPYFROMRESOURCE As Long = &H4000 Const LR_SHARED As Long = &H8000 #If vba7 Then '############################################ ' USE 64 BIT COMPATIBLE DECLARATIONS '############################################ 'Returns an Icon from a File (.ico) Private Declare ptrsafe Function LoadImage _ Lib "user32.dll" Alias "LoadImageA" _ (ByVal hInst As LongPtr, _ ByVal lpsz As String, _ ByVal uType As Long, _ ByVal cxDesired As Long, _ ByVal cyDesired As Long, _ ByVal fuLoad As Long) _ As LongPtr 'Direct System what to do with the Window Private Declare ptrsafe Function SendMessage _ Lib "user32.dll" Alias "SendMessageA" _ (ByVal hwnd As LongPtr, _ ByVal wMsg As Long, _ ByVal wParam As LongPtr, _ lParam As Long) _ As LongPtr 'Redraw the Icons on the Window's Title Bar Private Declare PtrSafe Function DrawMenuBar Lib "user32.dll" _ (ByVal hwnd As LongPtr) As Long #Else '############################################ ' USE 32 BIT COMPATIBLE DECLARATIONS '############################################ 'Returns an Icon from a File (.ico) Private Declare Function LoadImage _ Lib "user32.dll" Alias "LoadImageA" _ (ByVal hInst As Long, _ ByVal lpsz As String, _ ByVal uType As Long, _ ByVal cxDesired As Long, _ ByVal cyDesired As Long, _ ByVal fuLoad As Long) _ As Long 'Direct System what to do with the Window Private Declare Function SendMessage _ Lib "user32.dll" Alias "SendMessageA" _ (ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ lParam As Long) _ As Long 'Redraw the Icons on the Window's Title Bar Private Declare Function DrawMenuBar _ Lib "user32.dll" _ (ByVal hwnd As Long) _ As Long #End If Public Function ChangeExcelIcon(ByVal Icon_File_Path As String) #If vba7 Then Dim hwnd As LongPtr Dim hIcon As LongPtr #Else Dim hwnd As Long Dim hIcon As Long #End If Dim LoadMask As Long hwnd = Application.hwnd LoadMask = LR_LOADFROMFILE Or LR_DEFAULTSIZE Or LR_SHARED hIcon = LoadImage(0&, Icon_File_Path, IMAGE_ICON, 32, 32, LoadMask) Call SendMessage(hwnd, WM_SETICON, ICON_BIG, ByVal hIcon) Call DrawMenuBar(hwnd) End Function
Good luck.
I will see if it works. Thanks again for helping me out!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks