Thread: SetWindowLong

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    53

    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);

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    53
    Thanks for explanation.

    Is casting in a way like converting an integer into a long value?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to change window style at runtime?
    By Mr. Bitmap in forum Windows Programming
    Replies: 5
    Last Post: 06-09-2002, 04:49 PM
  2. Problems using SetWindowLong and GWL_USERDATA
    By xds4lx in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2002, 12:47 AM