12.2 Using the WINMEM32.DLL Library

Although you could directly implement code for a flat memory model in your
Windows application, this implementation would necessarily be unique to your
application. As a result, your application might not run with future versions of Windows or with other compatible platforms.

WINMEM32.DLL supplies a standard method for implementing a flat memory model that is guaranteed to run with future versions of Windows and other compatible platforms. It gives your application access to services for allocating, reallocating, and freeing 32-bit memory objects; for translating 32-bit pointers to 16-bit pointers that can be used by Windows and MS-DOS functions; and for aliasing a data segment to a code segment so you can execute code loaded into a 32-bit segment.

Your application can load WINMEM32.DLL when Windows is running in standard or 386 enhanced mode. However, because the 32-bit registers of the 80386 or 80486 processor are available only when Windows is in 386 enhanced mode, WINMEM32.DLL is enabled only in that mode. If your application runs in standard mode, you must design your application so that it can access 16-bit memory instead of 32-bit memory. You can find out which mode Windows is running in by calling the GetWinFlags function.

WINMEM32.DLL contains eight functions that enable your application to access 32-bit memory. The following table summarizes each of these functions:

Function Description

GetWinMem32Version Returns the version number of the WINMEM32.DLL application programming interface (API).
Global16PointerAlloc Converts a 32-bit pointer to a 16-bit pointer.
Global16PointerFree Frees a pointer alias created by the Global16PointerAlloc function.
Global32Alloc Allocates a 32-bit memory object.
Global32CodeAlias Creates a code-segment alias for a 32-bit memory object, allowing code in the object to be executed.
Global32CodeAliasFree Frees a code-segment alias created by the Global32CodeAlias function.
Global32Free Frees a 32-bit memory object.
Global32Realloc Changes the size of a 32-bit memory object.

A directory listing of these functions appears later in this chapter.

Because WINMEM32.DLL is a standard Windows DLL, your application loads it as it would any other DLL. Your application should be linked so that the case of the DLL entry point names is ignored.

The WINMEM32.DLL functions use the same calling conventions as other Windows functions. The DLL entry points are external FAR PASCAL procedures. They preserve the SS, BP, DS, SI, and DI registers, and they return values in the AX register or the DX:AX register pair.