HOWTO: Convert a Linear Address to a Flat Offset on Win32s

ID: Q115080

The information in this article applies to:

  - Microsoft Win32s, versions 1.1, 1.15, 1.2, 1.3, 1.30c

SUMMARY

Win32s flat code and data selectors are not zero-based. Linear addresses retrieved through a VxD can be used in a Win32-based application running under Win32s, after one small change is made.

In addition, there are two Universal Thunk APIs that are used to convert segmented addresses to flat addresses and vice versa.

MORE INFORMATION

Linear Address to Flat Address

Win32s does not base linear addresses at 0, so that exceptions will be generated when null pointers are dereferenced. Therefore, an access violation occurs when:

1. a 16-bit DLL calls a VxD to retrieve a linear address (the VxD got

   the address by translating a physical address to a linear address)
   through DPMI function 0800h (map physical to linear).

2. the 16-bit DLL returns the address to a Win32-based application through
   the Universal Thunk.

3. the Win32-based application uses this linear address.

In order to convert a linear address (based at 0) to a flat offset, add the base to the linear address. To do this, get the offset through GetThreadSelectorEntry() with the DS or CS and then subtract that base from the linear address that was returned by the VxD.

Segmented Address to Flat Address

The following Win32s Universal Thunk APIs are used for address translation:

NOTE: In the nested function call

   UTLinearToSelectorOffset( UTSelectorOffsetToLinear( x ) );

where x is a segmented address, you may not necessarily get the original value of x back. It is by design that the sel:off pair may be different. If the memory was allocated by a 16-bit application, Win32s does not have x in its LinearAddress->selector translation tables. Therefore, when UTLinearToSelectorOffset() is called, new selectors are created.

Additional query words: gpf gp-fault

Keywords          : kbprg kbWin32s 
Version           : 1.1 1.15 1.2 1.3 1.3c
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: June 12, 1997