[comp.windows.ms.programmer] Borland C++, Dialog Boxes and "this" passing

whisd@sersun1.essex.ac.uk (Whiteside S D B) (04/27/91)

C++ and Windows

I am attempting to extend the class library for Windows suggested by Borland in their "Language Express" magazine of Winter 1991 (!!!).

They reserve extra bytes in their Windows Class structure (WNDCLASS) to hold the "this" pointer to the current Window object.

When Windows calls the Class' message procedure, it grabs this pointer via the window handle and uses the -> operator to access members of the correct object.

This neatly modularizes the code for a window class and its instances.

However there are some problems using similar treatment with dialog boxes. Using dialog boxes one typically does not have access to the window class Windows uses internally to manage the dialog box. Therefore one cannot allocate the extra bytes required to hold the "this" pointer without which the message handle cannot function.

I thought of one solution: you could use DialogBoxParam() to send "this" to the WM_INITDIALOG part of the message function and store it in a static STACK member of the dialog class. Then each message call could TOP the stack to find which "this" it needs to reference. Since there can be only one (modal) dialog in operation at one time, this would be sufficient. For multiple modals perhaps a DICTIONARY structure with the pairs <this,window handle> could be used instead.

This solution is pretty messy. It's not half as neat as the window class one Borland suggest. It needs extra code and extra memory (for the stack). Can anyone suggest a more elegant and simpler way of getting the right "this" to the message function at the right time?

Simon Whiteside
Essex University UK