Re: How much extra window memory really gets allocated with cbWndClass?
- From: "John Carson" <jcarson_n_o_sp_am_@xxxxxxxxxxxxxxx>
- Date: Wed, 7 Dec 2005 13:52:55 +1100
"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
.
- Follow-Ups:
- Re: How much extra window memory really gets allocated with cbWndClass?
- From: Dan Mitchell
- Re: How much extra window memory really gets allocated with cbWndClass?
- References:
- How much extra window memory really gets allocated with cbWndClass?
- From: Dan Mitchell
- How much extra window memory really gets allocated with cbWndClass?
- Prev by Date: How much extra window memory really gets allocated with cbWndClass?
- Next by Date: How can I detect Removable Harddisk plug in or pull out?
- Previous by thread: How much extra window memory really gets allocated with cbWndClass?
- Next by thread: Re: How much extra window memory really gets allocated with cbWndClass?
- Index(es):
Relevant Pages
|