How To Convert a Linear Address to a Flat Offset on Win32s (115080)



The information in this article applies to:

  • Microsoft Win32s 1.3c
  • Microsoft Win32s 1.1
  • Microsoft Win32s 1.15
  • Microsoft Win32s 1.2
  • Microsoft Win32s 1.3

This article was previously published under Q115080

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:

  • UTSelectorOffsetToLinear (segmented address to flat address)
  • UTLinearToSelectorOffset (flat address to segmented address)
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.

Modification Type:MinorLast Reviewed:6/29/2004
Keywords:kbhowto kbprogramming KB115080