-
SetWindowLong
Hi,
I'm having a bit of a trouble understand what this function does. I think it changes the attributes of a window class but I am not sure.
Also I'm a bit confused what this does "(long)windowMessageText". Is the text being converted into a long?
Code:
HWND hwnd;
LPCWSTR windowMessageText2 = L"wow";
SetWindowLong(hwnd, GWLP_USERDATA, (long)windowMessageText);
-
You know how classes work? They have normal member variables that are specific to each instance, and they have static members, shared between all instances.
Well, the windows of Windows have the same thing, but with the Set/GetWindow/ClassShort/Long/LongPtr access method. Basically, the GetWindow* family retrieves instance members, SetWindow* sets them, GetClass* retrieves static members, SetClass* sets them. Each family has three members: GetWindowShort (or GetWindowWord, I can't remember; the thing is hardly used anyway) and the others work on 16-bit values, GetWindowLong and the others work on 32-bit values, and GetWindowLongPtr works on LONG_PTR values, which are the size of a pointer on the platform: 32 bits on 32-bit systems, 64 on 64-bit systems.
So, when putting handles, pointers etc. in the window or class data, you should use the LongPtr variant to be Win64-compatible. But note that the SDK that comes with Dev-C++ is too old to contain these variants.
Thus, the code becomes
Code:
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)windowMessageText)
As for what that last part means: consider that the prototype for SetWindowLongPtr says that the last argument has the type LONG_PTR. But the value you want to store has the type const unsigned short *. (That's what LPCWSTR is an alias for.) LONG_PTR is an alias for some integer type (long on 32-bit systems, __int64 or long long on 64-bit systems). You can't pass a pointer where an integer is expected. Unless you tell the compiler to just pretend that the bit pattern of the pointer is really an integer, which is exactly what the cast does.
-
Thanks for explanation.
Is casting in a way like converting an integer into a long value?
-
Yes, that's one cast. Casting is generally used to refer to all changes of types that do not involve explicit construction of the new value from the old (i.e. parsing a string into an integer is not a cast).