SAMPLE: CEdit Derived Class That Uses the Global Heap

Last reviewed: March 2, 1998
Article ID: Q118308
1.00 1.50 WINDOWS kbprg kbfile

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with:

        - Microsoft Visual C++ for Windows, version 1.0 and 1.5
    

SUMMARY

The GEDIT sample demonstrates how to create a CEdit derived class that uses a globally allocated memory segment instead of the application's local heap, thereby increasing the maximum contents of the edit control to the full 64K.

To obtain the GEDIT sample, download GEDIT.EXE, a self-extracting file, from the Microsoft Software Library (MSL) on the following services:

  • Microsoft Download Service (MSDL)

          Dial (206) 936-6735 to connect to MSDL
          Download GEDIT.EXE (size: 35331 bytes) 
    
  • Internet (anonymous FTP)

          ftp ftp.microsoft.com
          Change to the SOFTLIB\MSLFILES directory
          Get GEDIT.EXE (size: 35331 bytes) 
    

GEDIT.ZIP can also be found in library 8 of the MSMFC forum as GEDITMP.ZIP

MORE INFORMATION

Under Windows, edit controls use the application's local heap to store their text data. Unfortunately this can greatly limit their maximum capacity of 64K if the application uses a large portion of the local heap. In order to be able to utilize the entire 64K capability of the edit control it must have its own segment.

Windows edit controls determine the local heap of the application by using the instance handle of the application. The instance handle of an application is just the data segment address (segment only). Therefore the edit control just uses the instance handle directly as the segment selector for its memory allocations.

In order to get the edit control to have its own segment, PreCreateWindow must be overridden in a CEdit derived class. In this overridden member function GlobalAlloc is called to allocate a portion of memory from the global heap (the size is not important since the edit control will grow the segment as necessary). The handle returned from the GlobalAlloc is locked and the segment portion of the pointer is stored as the hInstance member of the CREATESTRUCT passed into the overridden PreCreateWindow. A LocalInit must also be called to set up the newly allocated memory as a heap.

Once the hInstance variable contains the segment selector of this new segment the CEdit derived class will allocate memory from this segment instead of the application's local heap.


Additional reference words: kbinf 1.00 1.50 2.00 2.50 gedit globedit
KBCategory: kbprg kbfile
KBSubcategory: MfcMisc
Keywords : kb16bitonly MfcMisc kbfile kbprg
Technology : kbMfc
Version : 1.00 1.50
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 2, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.