2.5.2 Use of Window-Specific Data

OLE follows the object-oriented model in that server applications contain documents, and in turn, documents contain objects. Isolating object, document, and server data is easier if you structure your application to have a window that contains document windows, and document windows that contain object windows. Each kind of window should maintains a private data structure visible only to that window. Not only does this simplify data management, but it also reduces the need of global variables to track the number of objects or open files, and possibly variable-length arrays of data structures.

For each server application, each document, and each object window, register the window class with sizeof(HANDLE) in the cbWndExtra field of WNDCLASS and define a single data structure to contain the data specific to that class. Allocate memory for this structure when the window receives a WM_CREATE message and free that memory on WM_DESTROY. Store the handle to this memory in the window's extra bytes. Then, whenever the window procedure is entered, it can simply retrieve this handle with GetWindowWord and have access to the entire data structure.