Greetings tiredofit,
Only as Jaafar, Rory or another member with more of a tight grasp on this has not stopped in, let me see if I can answer...
Did you download Win32API_PtrSafe.txt? I provided you a link in post # 7 and would suggest you download this and find a copy of WIN32API.TXT as well. Plus start searching for what you can find in regards to Microsoft Windows API as accessed through VBn/VBA.
Anyways, if you look for either Get|Set(Window), you will find this:
NOTE: Example declarations only (GetClassLongPtr and SetClassLongPtr removed for clarity)
Rich (BB code):
#If Win64 Then
Declare PtrSafe Function GetWindowLongPtr Lib "user32" Alias "GetWindowLongPtrA" (ByVal hwnd As LongPtr, ByVal nIndex As Long) As LongPtr
Declare PtrSafe Function SetWindowLongPtr Lib "user32" Alias "SetWindowLongPtrA" (ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
#Else
Declare PtrSafe Function GetWindowLongPtr Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long) As LongPtr
Declare PtrSafe Function SetWindowLongPtr Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
' Provided for reference only. Please use the LongPtr versions instead.
Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long) As Long
Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
#End If
Let us compare these to what is in WIN32API.TXT (at least the version/copy as updated by Karl E. Peterson)
Rich (BB code):
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
The first thing one might notice is that only in the section under
#If Win64 do we actually call different functions Get|Set(WindowLongPtrA). Quite frankly, Jaafar or Rory can probably speak authoritatively to this, but for me it would be a guess if we are accessing user32.dll under System32 rather than the user32 in sysWOW32 for these functions. (My guess would be yes)
Regardless, and specific to your question, if we look at the documention:
GetWindowLong function we see that GetWindowLong returns a LONG (in C++ I believe?), and that if we look up what this type of LONG in
Windows Data Types, we find this info:
A 32-bit signed integer.
The range is -2147483648 through 2147483647 decimal.
This type is declared in WinNT.h as follows:
typedef long LONG;
Now, we need to match that with something VBA has, lest we watch not-so-funny things happen when we throw bad stuff to a dll. (Simply put, when dealing with API, we are skipping the 'safeties' present in native VBA functions.) So looking at our available data types, we see that a (VBA) Long is defined as:
Long (long integer) 4 bytes (or 32-bits), range of -2,147,483,648 to 2,147,483,647
Presto! We have something we can safely pass to (or return from) the API function!
Now, as to using LongPtr instead, you can find a number of MSDN articles advising to use this whenever referring to a handle or pointer (in VBA7). This is because while LongPtr is not actually a true data type, it automatically casts itself to a (VBA) Long in 32-bit Excel, or a LomngLong if in 64-bit Excel.
Thus, I would not say that Jan Karel's decarations are a 'mistake' in any fashion, and in fact follow the general advisements from MSDN articles. Nor, in than manner in which we have structured our
#If ...
#Else ...
#End If, would I find worry in using As Long for the return. After all, the LongPtr is going to evaluate to a Long in 32-bit.
I hope that all made sense,
Mark
PS. - Here are some links, all in no order...
GetWindowLong function (Windows)
GetWindowLongPtr function (Windows)
SetWindowLong function (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/ms644898(v=vs.85).aspx
https://en.wikipedia.org/wiki/C_data_types
https://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/s3f49ktz.aspx
https://docs.microsoft.com/en-us/cpp/cpp/data-type-ranges
https://msdn.microsoft.com/en-us/library/windows/desktop/aa384264(v=vs.85).aspx
https://msdn.microsoft.com/en-us/vb...64-bit-visual-basic-for-applications-overview
https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/longptr-data-type
https://bettersolutions.com/vba/numbers/longptr-data-type.htm
https://en.wikipedia.org/wiki/Two's_complement
https://en.wikipedia.org/wiki/Ones'_complement
https://msdn.microsoft.com/en-us/library/ee691831.aspx