Microsoft KB Archive/25064

From BetaArchive Wiki

INF: Implementing Linked Lists with Handles in Windows ID Number: Q25064

2.00 3.00 WINDOWS


The standard method when using C in a non-Windows environment is to use pointers to reference the next link in the chain. Using pointers in the Windows environment requires that the LMEM_FIXED flag be used with LocalAlloc() so heap compaction will not affect the internal pointer references. Problems will occur using this technique as members of the chain are added and deleted.

More Information:

Implementing a heap compaction to free space will be difficult (or impossible). Therefore, handles should be used instead of pointers to reference successive list members in the Windows environment. Handles will allow the allocated heap data to be moved; no LMEM_FIXED is needed. When data in the chain must be referenced, LocalLock() is called using the member’s handle. To traverse the linked list, do the following:

  1. Given an initial handle to the chain link, call LocalLock().
  2. Use the address returned by LocalLock() to access the link’s data and get the handle of the next link in the chain.
  3. Call LocalUnlock() for the current link.
  4. If not at the end of the chain, go to step 1 using a new handle.