Re: How much extra window memory really gets allocated with cbWndClass?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"Dan Mitchell" <djmitchella@xxxxxxxxx> wrote in message
news:Xns97247E0009AA8djmitchellayahoocom@xxxxxxxxxxxxx
> I need to store extra window data with a window I'm creating -- in
> particular, I need to store a pointer and an int.
>
> So when creating the class, I do
>
> cls.cbWndExtra = sizeof(CFoo*) + sizeof(int);
>
> and set the data with:
>
> SetWindowLongPtr(hWnd, 0, (LONG_PTR)pFoo);
> SetWindowLongPtr(hWnd, sizeof(CFoo*), (LONG_PTR)iIndex);
>
> Then, later, in a message handler,
>
> CFoo* pFoo = (CFoo*)GetWindowLongPtr(hWnd, 0);
> int iIndex = (int)GetWindowLongPtr(hWnd, sizeof(CFoo*));
> pFoo->Bar(iIndex);
>
> and that all works fine. But in the docs for Set/GetWindowLongPtr, it
> says:
>
> "nIndex : Specifies the zero-based offset to the value to be set.
> Valid values are in the range zero through the number of bytes of
> extra window memory, minus the size of an integer."
>
> What's going on with the "minus the size of an integer" there? Do I
> need to set cbWndExtra to sizeof(CFoo*)+sizeof(int)+sizeof(int)? It
> seems to work okay as I have the code right now, but presumably there
> must be some reason they put that text in the docs..

If you have 12 extra bytes, then the int-sized addresses are

0
4
8

Thus the last valid int address is "the number of bytes of extra window
memory[12], minus the size of an integer[4]". The "minus" is simply because
the address of an int gives the starting position of the int memory block,
not the end, so the last int address has a value 4 less than the total
amount of memory.

In short, what you are doing is fine and is consistent with the docs.

> Also, looking at the various constants in winuser.h, it has
>
> #define GWL_EXSTYLE (-20)
> #define GWL_USERDATA (-21)
>
> which confuses me -- if I just needed to store a single pointer using
> GWL_USERDATA, I'd expect it to be offset by 4 from GWL_EXSTYLE, as I
> have a 32-bit value's worth of space in there, surely, so don't the
> last three bytes of that overwrite GWL_EXSTYLE?

I would guess that this code dates back to the dark ages and doesn't really
indicate what is going on. I presume that inside Get/SetWindowLong(Ptr),
there is special case processing for these values to avoid the problem you
state.

--
John Carson


.



Relevant Pages

  • Re: Shared Memory...Some Questions.....
    ... I'm by no means a shared memory expert... ... /* int ctheSharedIndex; ... shmat returns a pointer ... you are using it to store an int, you need to store the return ...
    (comp.os.linux.development.system)
  • Re: Shared Memory...Some Questions.....
    ... I'm by no means a shared memory expert... ... /* int ctheSharedIndex; ... shmat returns a pointer ... you are using it to store an int, you need to store the return ...
    (comp.os.linux.development.system)
  • How much extra window memory really gets allocated with cbWndClass?
    ... I need to store a pointer and an int. ... values are in the range zero through the number of bytes of extra window ... The docs for GetWindowLong say "nIndex Specifies the zero-based offset ...
    (microsoft.public.win32.programmer.ui)
  • Re: sorting the input
    ... Ideally, given a value of some type T, the best place to store ... is not really good enough -- an int and a float are often the ... This will print "4 4" on many machines (I have one here that does ... one "kind" of pointer, having the bits moved around (byte vs word ...
    (comp.lang.c)
  • Re: Can array[]=malloc()ed?
    ... >>You cannot store a pointer value in an `int' variable. ... CLC FAQ CLC readme: ...
    (comp.lang.c)