HOWTO: Pass Large Memory Block Through Win32s Universal Thunk

Last reviewed: June 10, 1997
Article ID: Q126708
The information in this article applies to:
  • Microsoft Win32s version 1.2, 1.30, 1.30a, 1.30c

SUMMARY

You can share memory between 32-bit code and 16-bit code under Win32s using either GlobalAlloc or passing a memory address to a Universal Thunk (UT) routine. For general information on how to share memory under Win32s, please refer to the following Knowledge Base article:

  ARTICLE ID: Q105762
  TITLE     : HOWTO: Share Memory Between 32-Bit and 16-Bit Code on Win32s

When you pass a memory address to a thunk routine, the pointer address is translated via the universal thunk (UT). However, the translated pointer is only guaranteed for 32K due to performance reasons. For more information on this limitation, please refer to the following Knowledge Base article:

  ARTICLE ID: Q100833
  TITLE     : INFO: Win32s Translated Pointers Guaranteed for 32K

This article describes ways to pass a larger memory block (including greater than 64K in size) through the Universal Thunk under Win32s.

MORE INFORMATION

GlobalAlloc()

You can call GlobalAlloc() to allocate a larger memory block on the 32-bit side of the thunk, copy the data into this memory block, send the handle to the 16-bit side, and lock the handle on the 16-bit side with GlobalLock(). With this method, you are not limited by the size of the block that can be passed across the thunk. If the memory block that you are passing is more than 64K in size, make sure to type cast the return value from GlobalLock() to a huge pointer on the 16-bit side.

VirtualAlloc()or HeapAlloc()

If you allocate the memory using VirtualAlloc(), it will be aligned on a 64K boundary, so that you can address the entire memory block. HeapAlloc() allocates large memory blocks using VirtualAlloc() as well. NOTE: You are still limited to 64K of memory, due to the selector tiling.

Allocate a selector

To use this method, get the 32-bit offset used by the Win32-based application and the selector base for the data selector returned by GetThreadSelectorEntry(), then calculate the linear address of the memory block. With this linear address, you can use AllocSelector(), SetSelectorBase(), and SetSelectorLimit() to access the memory block from the 16-bit side of the thunk.

For more information on converting the linear address to flat offset on Win32s, please refer to the following Knowledge Base article:

  ARTICLE ID: Q115080
  TITLE     : HOWTO: Convert a Linear Address to a Flat Offset on Win32s

For more information on allocating and using a selector on the 16-bit side, please refer to the following Knowledge Base article:

  ARTICLE ID: Q132005
  TITLE     : DOCERR: AllocSelector & FreeSelector Documentation Incomplete

NOTE: Sparse memory will cause problems in the general case. Make sure that the memory range has been not only reserved, but also committed.


Keywords : kbprg W32s
Version : 1.2 1.3 1.30a 1.30c
Platform : WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 10, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.