Variables Int and Long in 32 bit, converting to 64 bit

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,926
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
My programs have variables declared as Int and Long.

I'm in the process of converting my programs so they would work in 64 bit Office.

I know my APIs will have to be updated but what about "simple" variables such as Int and Long?

Would they need to be updated (eg change Long to LongLong) or will they work as expected in Office 64 bit?

If these variables need to be updated, what else, other than APIs would need updating?

Thanks
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Other than possibly some API Sub/Function that requires a signed 16-bit Integer as a parameter or returns said, I personally cannot think of when it would be advantageous to use the (VBA) Integer data type. Simply put, these are stored as 32-bit and need converted to 16-bit, so technically at least, using As Integer would process a teensy bit slower. So just use As Long, unless there is a specific reason for the Integer.

As far as regular variables, no, they do not need to change. The only time I can think of to use a LongLong if if you are working with large enough numbers to cause an overflow error with a Long.

Hope that helps,

Mark
 
Upvote 0
Mark,

Thanks for clarifying. I use Int only because right from the start of my programming days, I've been told to "keep things as tight as possible"!

Having said that, I NEVER use Single, it seems to be buggy.
 
Last edited:
Upvote 0
@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]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

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