Multiple Instance Large Model Windows MFC Applications

ID Number: Q85736

1.00 | 1.00

MS-DOS | WINDOWS

Summary:

In the small and medium memory models, all objects based on the

Microsoft Foundation Class (MFC) library supplied with Microsoft C/C++

version 7.0 must be placed in the near heap. This imposes a limit on

the number of objects that can be allocated because the near heap is

typically less than 64K in size. The MFC library does not allow a

small or medium model application to create a far instance of any of

the standard classes from the MFC library. To create far instances of

classes based on the MFC library, the compact or large memory model

must be used.

This article discusses some of the issues regarding large memory model

Microsoft Windows applications built with the MFC library.

More Information:

Member functions of classes are called with an implicit "this"

pointer, which points to the data for the class. By default, the size

of the this pointer is determined by the ambient memory model. For

small and medium models, the this pointer is near. For large, compact,

and huge models, the this pointer is far. For example, consider the

following line of code in a medium memory model application:

lpMyString = new FAR CString;

The pointer to the new object will be a far pointer. However, the

member functions of the class CString in the medium memory model MFC

library expect a near this pointer. As a result, the compiler will

generate a C2662 error stating that it cannot convert the this

pointer from far to near. For more information on the this pointer

and how it is affected by different memory models, please refer to the

online help for the __far keyword and to pages 244-246 and page 400 of

the "Microsoft C/C++ C++ Language Reference" manual supplied with

Microsoft C/C++ version 7.0.

An application must use the compact or large memory model to allocate

far instances of MFC derived classes. To use these memory models, the

appropriate libraries must be built. Please see the README.TXT file in

the MFC\SRC directory for instructions on building different memory

model versions of the MFC library.

Compact and large memory model Windows applications raise special

issues. Windows can run multiple instances of an application only if

the application has a single data segment (DGROUP). Multiple instances

of a compact or large memory model application can be run only if all

of the application's static and global data is located in DGROUP. To

help enable this, build the MFC libraries and applications by using

the /Gx compiler switch.

By default, in the compact and large memory models, the compiler

allocates initialized data items as near if they are smaller than or

equal in size to the threshold value set by the /Gt option. The /Gx

switch extends this initialized data allocation rule to data that is

uninitialized and data that is marked as extern. The /Gx option does

not affect the size of pointers. Pointers remain far by default. For

more information on the /Gt and /Gx compiler switches, please refer to

the online help for the command-line options for the C/C++ compiler

and to page 522 and pages 523-524 of the "Microsoft C/C++ Environment

and Tools" manual supplied with Microsoft C/C++ version 7.0.

When building the MFC libraries, use the OPT=/Gx option on the NMAKE

command line. Instances of classes created using new can then be

allocated outside of DGROUP, overcoming the 64K limit.

The command line to build a large model Windows version of the MFC

library with the /Gx option is:

NMAKE MODEL=L TARGET=W OPT=/Gx

Note: The /Gx option does not work for static or global objects. These

objects must be explicitly declared as NEAR in order for them to be

placed in DGROUP. For example, in the HELLO.CPP sample MFC Windows

application, the following code used to create the application object

CTheApp theApp;

should be changed to the following:

CTheApp NEAR theApp;

Additional reference words: 7.00 Win SDK WinSDK