GetWindowLong versus GetWindowLongPtr

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

GetWindowLong versus GetWindowLongPtr

16 Oct 2017, 17:20

According to MSDN, GetWindowLongPtr would give compatibility between x64 and x32, however I'm finding that GetWindowLong gives the greater compatibility.

For the example below:
GetWindowLong - worked x64/x32
GetWindowLongPtr - worked x64/didn't work x32
Am I missing something?

Code: Select all

q:: ;window get style/extended style
WinGet, hWnd, ID, A
;GWL_STYLE := -16 ;GWL_EXSTYLE := -20
;vSfx := (A_PtrSize=8) ? "Ptr" : ""
;vSfx := "Ptr"
vSfx := ""
vWinStyle := DllCall("GetWindowLong" vSfx, Ptr,hWnd, Int,-16)
vWinExStyle := DllCall("GetWindowLong" vSfx, Ptr,hWnd, Int,-20)
MsgBox, % Format("0x{:08X} 0x{:08X}", vWinStyle, vWinExStyle)
return
A list of related dll functions:
GetClassLong / GetClassLongPtr
SetClassLong / SetClassLongPtr
GetWindowLong / GetWindowLongPtr
SetWindowLong / SetWindowLongPtr
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: GetWindowLong versus GetWindowLongPtr

16 Oct 2017, 17:29

On Windows 7 both work because pointers usually are 32-bit values. On Windows 10 pointers often exceed 32 bits. Of course, I mean 64-bit OS.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: GetWindowLong versus GetWindowLongPtr

16 Oct 2017, 17:49

GetWindowLong wrote:Note If you are retrieving a pointer or a handle, this function has been superseded by the GetWindowLongPtr function. (Pointers and handles are 32 bits on 32-bit Windows and 64 bits on 64-bit Windows.) To write code that is compatible with both 32-bit and 64-bit versions of Windows, use GetWindowLongPtr.
GetWindowLongPtr wrote:Note To write code that is compatible with both 32-bit and 64-bit versions of Windows, use GetWindowLongPtr. When compiling for 32-bit Windows, GetWindowLongPtr is defined as a call to the GetWindowLong function."
Don't forget return type: DllCall("GetWindowLong" (A_PtrSize=8 ? "Ptr" : ""), "Ptr", hWnd, "Int", -16, "PTR")

Code: Select all

GetWindowLongPtr := DllCall("GetProcAddress","PTR",DllCall("GetModuleHandle","Str","user32.dll","PTR"), "AStr", "GetWindowLong" (A_PtrSize=8 ? "Ptr" : "") "W", "PTR")
DllCall(GetWindowLongPtr, "Ptr", hWnd, "Int", -16, "PTR")
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: GetWindowLong versus GetWindowLongPtr

16 Oct 2017, 18:21

Thank you both for your responses.

Thanks HotKeyIt. Hmm I used my dll correction function to correct GetWindowLong, which has Int as a return type, but GetWindowLongPtr has Ptr as a return type. So I might have to address these (Get/Set)XXXLong(Ptr) special cases in some way.
- GetWindowLong has return type Int (aka Long).
- GetWindowLongPtr has return type Ptr (aka Long Ptr).

Btw why do you use 'PTR' sometimes and 'Ptr' other times? Cheers.

[EDIT:] A-ha, so the issue is the return type, in x64, with GetWindowLong, you get an Int, when it should be a Ptr.
- But how come you can't just do GetWindowLongPtr in x32, is the function just not in the x32 version of the dll? Does it appear in the x32 dll in newer Windows versions (I use Windows 7)?
- Also, are there any other Long/LongPtr (or similar) functions other than the ones I mentioned?
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: GetWindowLong versus GetWindowLongPtr

16 Oct 2017, 18:55

Does it appear in the x32 dll in newer Windows versions ... ?
As I see, it doesn't.
user32.dll from SysWOW64 on Windows 10:

Image

user32.dll from system32 on Windows 10:

Image
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: GetWindowLong versus GetWindowLongPtr

17 Oct 2017, 05:39

jeeswg wrote:Btw why do you use 'PTR' sometimes and 'Ptr' other times? Cheers.

[EDIT:] A-ha, so the issue is the return type, in x64, with GetWindowLong, you get an Int, when it should be a Ptr.
- But how come you can't just do GetWindowLongPtr in x32, is the function just not in the x32 version of the dll? Does it appear in the x32 dll in newer Windows versions (I use Windows 7)?
Ptr and PTR is just the same.
As MSDN says, GetWindowLongPtr is only available in 64-bit and when compiled in 32-bit GetWindowLongPtr simply calls GetWindowLong.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: GetWindowLong versus GetWindowLongPtr

17 Oct 2017, 07:49

and when compiled in 32-bit GetWindowLongPtr simply calls GetWindowLong
This is what I don't understand, if I use GetWindowLongPtr with DllCall in AHK x32, it doesn't work. And if I compiled such a script, I don't suppose it would work either.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: GetWindowLong versus GetWindowLongPtr

17 Oct 2017, 08:04

My guess is msdn assumes you compile a c(#/++/?) application rather than bundling a text file with the AHK software. :angel:
Note:
HotKeyIt wrote:

Code: Select all

DllCall("GetWindowLong" (A_PtrSize=8 ? "Ptr" : ""), "Ptr", hWnd, "Int", -16, "PTR")
Cheers :wave:
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: GetWindowLong versus GetWindowLongPtr

17 Oct 2017, 14:51

Btw is it conceivable that the Long (not the LongPtr) versions of the dll functions could be useful on x64 in some circumstances?

@Helgef: Cheers :wave:
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: GetWindowLong versus GetWindowLongPtr

17 Oct 2017, 18:51

jeeswg wrote:Btw is it conceivable that the Long (not the LongPtr) versions of the dll functions could be useful on x64 in some circumstances?
They could be used and will work vor 32-bit values but would not for 64-bit.
To avoid confusion always use LongPtr on 64-bit!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Billykid and 213 guests