@
tiredofit
What dictates which data type you will need to use when calling APIs is what it says in the MS Documentation. So for example, if an API function expects a signed INT argument then you should pass to it a 32 Bit long argument when calling it in VBA ... APIs are written in C and a signed Int in C corresponds to a 32bit Long signed variable in VBA.
Take a look here for some commun data type conversion between C and VB :
Data Type Conversions (COM)
f these variables need to be updated, what else, other than APIs would need updating?
In order for you vba code to be compatible with all office versions ie 32 and 64 Bits , you will need to update your code with conditional compilation. This conditional compilation will have to be used in the API declarations section
as well as in your variables declarations.
For example, the API
GetParent takes one 32 bit Long argument and returns a 32 bit Long value in Office 32 bit, whereas in 64 bit Office, it takes one 64 bit argument and returns a 64 Long value... This is because the argument as well as the return value are both Window Handles which are 32 bit in 32 bit systems and 64 bit in 64 bit systems. (Just like Pointers)
So based on the above, the API declartion in VBA would be as follows :
Code:
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL] VBA7 Then
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL] Win64 Then '(64bit OS) AND (64bit Office)
Declare PtrSafe Function GetParent Lib "user32" (ByVal hwnd As LongLong) As LongLong
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL] '(32 or 64 bit OS) AND (32bit Office)
Declare PtrSafe Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL] If
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL] ' (OFFICE 2007 AND BEFORE) .. Both (32bit OS) AND (32bit Office)
Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL] If
Or this more simple version using the Alias
LongPtr :
Code:
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL] VBA7 Then
Declare PtrSafe Function GetParent Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL]
Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL] If
Bear in mind that
LongPtr is not actually a true data type, It is is an alias and it points to the correct data type when using 64bit or 32bit . So
LongPtr would point to
LongLong on 64bit office and
Long on 32bit office.
Now , in your code, you will also need to conditional compile your variables along these lines :
Code:
Sub Test()
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL] VBA7 Then
Dim hWndChild As LongPtr, hwndParent As LongPtr
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL]
Dim hWndChild As Long, hwndParent As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL] If
hWndChild = Application.hwnd
hwndParent = GetParent(hWndChild)
Debug.Print hWndChild
Debug.Print hwndParent
End Sub
Here is a table breakdown of the different scenarios :
[TABLE="width: 562"]
<tbody>[TR]
[TD]
Office Version[/TD]
[TD]
Office Bitnes[/TD]
[TD]
OS Bitness[/TD]
[TD]
VarType[/TD]
[/TR]
[TR]
[TD]2007 and Before (VBA6)[/TD]
[TD]32[/TD]
[TD]32[/TD]
[TD]Long = (Signed 32 Bits)[/TD]
[/TR]
[TR]
[TD]2007 and Before (VBA6)[/TD]
[TD]32[/TD]
[TD]64[/TD]
[TD]Long = (Signed 32 Bits)[/TD]
[/TR]
[TR]
[TD]2010 and Later (VBA7)[/TD]
[TD]32[/TD]
[TD]32[/TD]
[TD]LongPtr = Long = (Signed 32 Bits)[/TD]
[/TR]
[TR]
[TD]2010 and Later (VBA7)[/TD]
[TD]32[/TD]
[TD]64[/TD]
[TD]LongPtr = Long = (Signed 32 Bits)[/TD]
[/TR]
[TR]
[TD]2010 and Later (VBA7)[/TD]
[TD]64[/TD]
[TD]64[/TD]
[TD]LongPtr = LongLong = (Signed 64 Bits)[/TD]
[/TR]
</tbody>[/TABLE]