Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call SetWindowLong() in the 64-bit versions of Windows?

Tags:

In the header file WinUser.h, there is a part in which the constants of the second parameter of SetWindowLong() are defined.

// ...  #define GWL_WNDPROC         (-4) #define GWL_HINSTANCE       (-6) #define GWL_HWNDPARENT      (-8) #define GWL_STYLE           (-16) #define GWL_EXSTYLE         (-20) #define GWL_USERDATA        (-21) #define GWL_ID              (-12)  #ifdef _WIN64  #undef GWL_WNDPROC #undef GWL_HINSTANCE #undef GWL_HWNDPARENT #undef GWL_USERDATA  #endif /* _WIN64 */  #define GWLP_WNDPROC        (-4) #define GWLP_HINSTANCE      (-6) #define GWLP_HWNDPARENT     (-8) #define GWLP_USERDATA       (-21) #define GWLP_ID             (-12)  // ... 

But they are right after undefined if _WIN64 is defined; and it is defined in my 64-bit system.

As you see, there is also a GWLP_*** set of constants, but they are not documented in the page of SetWindowLong().

Why are these constants undefined in x64 systems?
What is the alternative way of calling SetWindowLong() in x64 systems?


My system:
OS: Windows 7 Ultimate x64 SP1
IDE: Visual Studio 2012 Ultimate Update 3

like image 442
hkBattousai Avatar asked Aug 12 '13 02:08

hkBattousai


People also ask

Can I use setwindowlongptr () in a 64 bit build?

These days it is recommended that you always use SetWindowLongPtr () and the GWLP_xxx constants, whether you are building for 32 or 64 bit, but in a 64 bit build you need to use the new functions and so the defines are #undefined to cause build errors that force you to fix your code. Show activity on this post.

What is setwindowlongptr?

When compiling for 32-bit Windows, SetWindowLongPtr is defined as a call to the SetWindowLong function. A handle to the window and, indirectly, the class to which the window belongs.

What is the alternative for setwindowlong -> method?

The SetWindowLongPtr API. Use this one instead of SetWindowLong to assure 64 bit compatibility. // (on any versions of Windows), so linking the method will fail at run-time. The bridge dispatches the request

How do I write 32 bit code on 64 bit Windows?

Note To write code that is compatible with both 32-bit and 64-bit versions of Windows, use SetWindowLongPtr. When compiling for 32-bit Windows, SetWindowLongPtr is defined as a call to the SetWindowLong function.


2 Answers

Some of the window data values (the ones that refer to "pointer sized" objects like the window procedure, for example) need to be 64 bit in an x64 build. The old SetWindowLong() and GetWindowLong() functions are limited to DWORD sized (32 bit) values for backwards compatibility, and Microsoft have introduced new versions, SetWindowLongPtr() and GetWindowLongPtr() that allow you to work with pointer-sized values (32 bit in a 32 bit build, and 64 bit in a 64 bit build).

These days it is recommended that you always use SetWindowLongPtr() and the GWLP_xxx constants, whether you are building for 32 or 64 bit, but in a 64 bit build you need to use the new functions and so the defines are #undefined to cause build errors that force you to fix your code.

like image 69
Jonathan Potter Avatar answered Nov 05 '22 23:11

Jonathan Potter


    As specified in <WinUser.h>     //If config is _WIN64 then use new versioned macro      #define GWLP_WNDPROC        (-4)     #define GWLP_HINSTANCE      (-6)     #define GWLP_HWNDPARENT     (-8)     #define GWLP_USERDATA       (-21)     #define GWLP_ID             (-12)      //else for _WIN32      #undef GWL_WNDPROC     #undef GWL_HINSTANCE     #undef GWL_HWNDPARENT     #undef GWL_USERDATA 
like image 43
Dattatraya Mengudale Avatar answered Nov 05 '22 21:11

Dattatraya Mengudale