If You Know You're Running in Protected Mode

In this chapter, I have tried to present guidelines for memory management that are valid in all modes in which Windows 3 can run. However, if your program is so large that it can (realistically) run only in protected mode, then you can simplify your memory management. Such a program should use GetWinFlags and terminate if it's running in real mode.

When allocating moveable global memory, you can lock the memory block immediately to obtain the pointer. Even though the block is locked, Windows can still move it in memory—and the pointer will remain valid. You don't even have to save the global memory handle. To free the block, you can use GlobalHandle to obtain the handle from the pointer. Then unlock the block and free it as normal.

You can compile with the -G2 flag to generate 286 code.