INF: C 6.00 -GW Switch Incompatible with Windows in Real Mode

ID Number: Q72465

3.00

WINDOWS

Summary:

The Microsoft C compiler version 6.0 has a -GW switch that is used to

generate streamlined prolog and epilog code in applications developed

for the Microsoft Windows environment. However, the prolog and epilog

code generated by -GW should not be used. Use the -Gw switch instead.

More Information:

An application that runs in real mode must store the value of the DS

register on the stack each time it makes a far function call. Calling

a far function might cause Windows to move the application's data

segment when Windows brings in the called code segment from disk. When

the data segment moves, the kernel walks the stack and updates all the

saved DS values to the new data segment location. Because the prolog

code generated by the -GW switch does not push DS on the stack, when

the kernel walks the stack, some other data is on the stack where the

stored DS register should be. When the debugging kernel walks the

stack and encounters the unanticipated data instead of the stored DS

register, it produces a "cannot discard segment" FatalExit error

message.

The Microsoft C compiler generates the following assembly language

code when the -GW switch is specified:

Prolog Epilog

------ ------

inc bp pop bp

push bp dec bp

mov bp, sp ret

To work properly in real mode, the prolog code should be modified to

resemble the following:

inc bp

push bp

mov bp, sp

push ds ; save DS value on stack for stack walking routine

The Microsoft C compiler generates the modified code when the -Gw

switch is specified.

The following information describes the situations when it is

necessary to use the -Gw switch:

Applications developed for real mode: Always specify the -Gw switch

for real mode. Because Windows can walk the stack at any time, the

Windows prolog and epilog code must be executed for all far functions,

whether or not they are exported.

Applications developed for protected (standard or enhanced) mode: The

-Gw switch is required in protected mode only during the process of

debugging an application when a meaningful stack trace is needed or if

the module contains exported functions. In an exported function, the

Windows prolog code is required to set the DS register to the correct

DGROUP value, even in protected mode. Therefore, the extended prolog

code generated by -Gw is required. However, non-exported far functions

do not require this code.

Additional reference words: 3.00