INF: Compiler Switch Options for Windows Protected Mode Apps

ID Number: Q68801

3.00

MS-DOS

Summary:

If an application is always to be run under one of Windows' protected

modes (standard mode or 386 enhanced mode), you can greatly simplify

the function prolog and epilog code, thus producing smaller and faster

application code. The Microsoft C Compiler adds the normal function

prolog and epilog to far functions when the -Gw option is specified on

the compiler command line. However, this prolog and epilog code is

only required by functions that are exported from the application.

The standard prolog and epilog code is required for exported functions

so that the compiler will generate code to restore the DS register to

the data segment appropriate for the function and to establish a stack

frame that Windows recognizes when it inspects and modifies the stack.

This process is known as "walking" the stack and takes place whenever

memory objects are moved in real mode Windows.

In a protected mode, once the DS register is restored by an exported

function, it will not change. Thus, nonexported functions called from

within an exported function need not modify the DS register.

Similarly, code selectors never change, so Windows will not walk the

stack. This eliminates the need for any special stack frame.

Therefore, with nonexported functions in protected-mode-only

applications, the -Gw switch is not required.

More Information:

Please note that applications compiled without the -Gw switch will NOT

run in Windows real mode. In real mode, the DS register may change and

Windows may walk the stack at any time. Thus, the special stack frame

is required.

The Microsoft C Compiler version 6.00 introduces a new option switch,

-GW, that is designed to produce smaller prolog and epilog code for

nonexported functions in real mode applications. Unfortunately, using

this option switch in Microsoft C version 6.00 causes the compiler to

generate incorrect code. Microsoft has confirmed this to be a problem

in C version 6.00. We are researching this problem and will post new

information here as it becomes available.

Warning: The special stack frame created by the -Gw switch is used by

most debugging tools. Prolog code for FAR calls increments the BP

register by one when entered, making BP odd. Windows fatal exit

processing, stack trace listing in CodeView for Windows, and some

profilers depend on the odd/even characteristics of the BP register

stored in the stack frame to properly back-trace (walk) the stack.

Without a proper function prolog, functions that rely on this will not

work as documented.