SetWindowLong API

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,926
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Can someone please check if the following API conversion, taken from here:


is correct?

Rich (BB code):
#If VBA7 Then
     #If Win64 Then
         Private Declare PtrSafe  Function SetWindowLongPtr Lib "USER32" Alias "SetWindowLongPtrA" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
     #Else
         Private Declare Function SetWindowLongPtr Lib "USER32" Alias "SetWindowLongA" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
     #End If
 #Else
     Private Declare Function SetWindowLongPtr Lib "USER32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong  As Long) As Long 
 #End If

These lines are in red:

Rich (BB code):
Private Declare Function SetWindowLongPtr Lib "USER32" Alias "SetWindowLongA" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
     #End If
 #Else
     Private Declare Function SetWindowLongPtr Lib "USER32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong  As Long) As Long


Thanks
 
Last edited:
Mark,

Obviously I wasn't aware you goofed at all because I copied and pasted your code into both 64 bit and 32 bit Excel 2013, running off Windows 7 64 bit and both ran fine!

Jaafar,

Thanks for your corrections.
@tiredofit
Glad you have this working!
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
@Mark.

Just for clarity's sake, shouldn't your comment for the first #Else read ' (32 or 64 bit OS) AND (32bit Office) ?

Well spotted Mark! but I think the change should be :

#If win64 '(64Bit OS) AND (64Bit Office OR 32Bit Office)

With your addition, our breakdon table should look like this :

[TABLE="width: 1413"]
<tbody>[TR]
[TD]OS Bitness[/TD]
[TD]Office Version[/TD]
[TD]Office Bitness[/TD]
[TD] API Declaration[/TD]
[/TR]
[TR]
[TD]32[/TD]
[TD]2007 and Before (VBA6)[/TD]
[TD]32[/TD]
[TD="align: left"]Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long[/TD]
[/TR]
[TR]
[TD]64[/TD]
[TD]2007 and Before (VBA6)[/TD]
[TD]32[/TD]
[TD="align: left"]Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long[/TD]
[/TR]
[TR]
[TD]32[/TD]
[TD]2010 and Later (VBA7)[/TD]
[TD]32[/TD]
[TD="align: left"]Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long[/TD]
[/TR]
[TR]
[TD]64[/TD]
[TD]2010 and Later (VBA7)[/TD]
[TD]64[/TD]
[TD="align: left"]Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongPtrA" (ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr[/TD]
[/TR]
[TR]
[TD]64[/TD]
[TD]2010 and Later (VBA7)[/TD]
[TD]32[/TD]
[TD="align: left"]Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongPtrA" (ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr[/TD]
[/TR]
</tbody>[/TABLE]

Thanks.
 
Last edited:
Upvote 0
The above table would translate to this:

Code:
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  VBA7 Then  [COLOR=#008000][B]' (OFFICE 2010 AND LATER - Either 32bit or 64bit Editions)[/B][/COLOR]

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


    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  Win64 Then [B][COLOR=#008000]'(64Bit OS) AND (64Bit Office OR 32Bit Office)[/COLOR][/B]
        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 LongPtr) As LongPtr
    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL]   [B][COLOR=#008000]' (32bit OS) AND (32bit Office)[/COLOR][/B]
        Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long) As Long
        Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong 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]   [B][COLOR=#008000]' (OFFICE 2007 AND BEFORE)  .. Both (32bit OS) AND (32bit Office)[/COLOR][/B]
    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) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If
 
Upvote 0
I agree with Mark. Win64 means that Office is 64 bit, not 32 bit. It doesn't relate to the bitness of Windows despite its name (other than that of course you must have 64bit Windows in order to be using 64 bit Office.)
 
Upvote 0
Thanks for the correction Rory and Thanks to Mark too... I keep forgetting about this misleading Win64 name .

Just to wrap this up and so that we can come back to this thread for reference on the subject when confused, I am placing here the following table that breaks down all possible scenarios in a logical manner:

[TABLE="width: 1434"]
<tbody>[TR]
[TD]Office Bitness[/TD]
[TD]Office Version[/TD]
[TD]OS Bitness[/TD]
[TD]API Declaration[/TD]
[/TR]
[TR]
[TD]32[/TD]
[TD]2007 and Before (VBA6)[/TD]
[TD]32[/TD]
[TD="align: left"]Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long[/TD]
[/TR]
[TR]
[TD]32[/TD]
[TD]2007 and Before (VBA6)[/TD]
[TD]64[/TD]
[TD="align: left"]Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long[/TD]
[/TR]
[TR]
[TD]32[/TD]
[TD]2010 and Later (VBA7)[/TD]
[TD]32[/TD]
[TD="align: left"]Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long[/TD]
[/TR]
[TR]
[TD]32[/TD]
[TD]2010 and Later (VBA7)[/TD]
[TD]64[/TD]
[TD="align: left"]Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long[/TD]
[/TR]
[TR]
[TD]64[/TD]
[TD]2010 and Later (VBA7)[/TD]
[TD]64[/TD]
[TD="align: left"]Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongPtrA" (ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr[/TD]
[/TR]
</tbody>[/TABLE]


And here is how it would translate in code :

Code:
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL]  VBA7 Then  [B][COLOR=#008000]' (OFFICE 2010 AND LATER - Either 32bit or 64bit Editions)[/COLOR][/B]

    [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL]  Win64 Then [COLOR=#008000][B]'(64bit OS) AND (64bit Office)[/B][/COLOR]
        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 LongPtr) As LongPtr
    [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL]  [B][COLOR=#008000]'(32 or 64 bit OS) AND (32bit Office)[/COLOR][/B]
        Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long) As Long
        Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong 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]  [B][COLOR=#008000] ' (OFFICE 2007 AND BEFORE)  .. Both (32bit OS) AND (32bit Office)[/COLOR][/B]
    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) As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL]  If

I hope this one is now 100 per cent correct .. If any of you can confirm that would be nice.
 
Last edited:
Upvote 0
The final else could be 64 or 32 bit Windows but Office would be 32 bit. Apart from that, it looks spot on.
 
Upvote 0
The final else could be 64 or 32 bit Windows but Office would be 32 bit. Apart from that, it looks spot on.

Yep ! It is covered in the table (Scenarios 1 and 2) .

Thanks for the verification.
 
Upvote 0
Thanks for the correction Rory and Thanks to Mark too... I keep forgetting about this misleading Win64 name .

Just to wrap this up and so that we can come back to this thread for reference on the subject when confused, I am placing here the following table that breaks down all possible scenarios in a logical manner:

[TABLE="width: 1434"]
<tbody>[TR]
[TD]Office Bitness
[/TD]
[TD]Office Version
[/TD]
[TD]OS Bitness
[/TD]
[TD]API Declaration
[/TD]
[/TR]
[TR]
[TD]32
[/TD]
[TD]2007 and Before (VBA6)
[/TD]
[TD]32
[/TD]
[TD="align: left"]Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
[/TD]
[/TR]
[TR]
[TD]32
[/TD]
[TD]2007 and Before (VBA6)
[/TD]
[TD]64
[/TD]
[TD="align: left"]Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
[/TD]
[/TR]
[TR]
[TD]32
[/TD]
[TD]2010 and Later (VBA7)
[/TD]
[TD]32
[/TD]
[TD="align: left"]Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
[/TD]
[/TR]
[TR]
[TD]32
[/TD]
[TD]2010 and Later (VBA7)
[/TD]
[TD]64
[/TD]
[TD="align: left"]Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
[/TD]
[/TR]
[TR]
[TD]64
[/TD]
[TD]2010 and Later (VBA7)
[/TD]
[TD]64
[/TD]
[TD="align: left"]Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongPtrA" (ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
[/TD]
[/TR]
</tbody>[/TABLE]


And here is how it would translate in code :

Code:
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL]  VBA7 Then  [B][COLOR=#008000]' (OFFICE 2010 AND LATER - Either 32bit or 64bit Editions)[/COLOR][/B]

    [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL]  Win64 Then [COLOR=#008000][B]'(64bit OS) AND (64bit Office)[/B][/COLOR]
        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 LongPtr) As LongPtr
    <a href="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else" target="_blank">[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL]   [B][COLOR=#008000]'(32 or 64 bit OS) AND (32bit Office)[/COLOR][/B]
        Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long) As Long
        Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
     [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL]  If

<a href="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else" target="_blank">[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL]   [B][COLOR=#008000] ' (OFFICE 2007 AND BEFORE)  .. Both (32bit OS) AND (32bit Office)[/COLOR][/B]
    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) As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL]  If

I hope this one is now 100 per cent correct .. If any of you can confirm that would be nice.

Jaafar,

I think you left out this line (just before the last #Else ) that you had previously:

Rich (BB code):
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
 
Last edited:
Upvote 0
Thanks for the correction Rory and Thanks to Mark too... I keep forgetting about this misleading Win64 name .
...

Salute and utter agreement!

Not having 64-bit Excel to test against makes me a bit iffy anyways, but that Constant's name just makes me batty! Whenever I do attempt to provide declarations for both 64|32 bit Excel, I make myself read it as "#If Excel64 Then..."

Which brings me to a question for you and Rory that I've been pondering since post #15 ... Would there be something wrong (some failure to correctly compile conditionally) if we were to ask '#If Win64' first? And (sorry, one more question) could we (for one's personal clarity or preference) use a substitute for Win64?

For instance, using Jaafar's declarations (with the exception that I dis-included PtrSafe in the [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else If[/URL] VBA7 section) would either of you see any shortcoming(s) with:

Rich (BB code):
Option Explicit


#Const Excel64 = Win64
  
#If  Excel64 Then    '(64bit OS) AND (64bit Office)
  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 LongPtr) As LongPtr
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else If[/URL]  VBA7 Then '(32 or 64 bit OS, OFFICE 2010 AND LATER - Either 32bit or 64bit Editions)
  Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As LongPtr, ByVal nIndex As Long) As Long
  Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
#Else              '(32 or 64 bit OS, OFFICE 2007 AND BEFORE - 32bit Office)
  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) As Long
#End If
 
Upvote 0
Jaafar,

I think you left out this line (just before the last #Else ) that you had previously:

Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Hi Tiredofit,

Actually Jaafar left out FindWindow() altogether as he was providing clarification for Set|GetWindow. If you look back at post # 17 you can see where to plunk FindWindow in two places.

Mark
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,199
Members
453,022
Latest member
RobertV1609

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