GlobalAlloc Function

Declare Function GlobalAlloc Lib "kernel32.dll" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long

Platforms: Win 32s, Win 95/98, Win NT

GlobalAlloc allocates a series of bytes in the computer's global memory. The memory can be used for any purpose necessary. The function's return value, if successful, depends on the flags specified in wFlags. It will either be a pointer to the block of allocated memory or a handle to the block of allocated memory. Although they both identify the same thing, they will most likely not be equal and cannot be used interchangably! If the function fails, a value of 0 will be returned. Note that Windows will not allocate a memory block starting at base address 0.

wFlags
One or more of the following flags specifying how to allocate the block of memory:
GHND = &H40
Same as combining GMEM_MOVEABLE with GMEM_ZEROINIT.
GMEM_DDESHARE = &H2000
Optimize the allocated memory for use in DDE conversations.
GMEM_DISCARDABLE = &H100
Allocate discardable memory. (Cannot be combined with GMEM_FIXED.)
GMEM_FIXED = &H0
Allocate fixed memory. The function's return value is a pointer to the beginning of the memory block. (Cannot be combined with GMEM_DISCARDABLE or GMEM_MOVEABLE.)
GMEM_MOVEABLE = &H2
Allocate moveable memory. The memory block's lock count is initialized at 0 (unlocked). The function's return value is a handle to the beginning of the memory block. (Cannot be combined with GMEM_FIXED.)
GMEM_NOCOMPACT = &H10
Do not compact any memory or discard any discardable memory to allocate the requested block.
GMEM_NODISCARD = &H20
Do not discard any discardable memory to allocate the requested block.
GMEM_SHARE = &H2000
Same as GMEM_DDESHARE.
GMEM_ZEROINIT = &H40
Initialize the contents of the memory block to 0.
GPTR = &H42
Same as combining GMEM_FIXED with GMEM_ZEROINIT.
dwBytes
The number of bytes to allocate; i.e., the size of the memory block to allocate.

Example:

' Use a block of memory as an intermediary step to copy
' the contents of array s() to array t().  Yes, you could copy them directly,
' but this demonstrates a few different memory functions.
Dim s(0 To 255) As Integer, t(0 To 255) As Integer  ' arrays to copy from/to
Dim c As Integer, retval As Long  ' counter variable & return value
Dim hMem As Long, pMem As Long  ' handle and pointer to memory block

' Initialize the source array s()'s data
For c = 0 To 255
  s(c) = 2 * c  ' each element equals double its index
Next c

' Allocate a moveable block of memory (returns a handle) (Integer type = 2 bytes)
hMem = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, 256 * 2)
' Lock the memory block, returning a pointer to it
pMem = GlobalLock(hMem)
' Copy the entire contents of s() to the memory block
' Note that pMem is ByVal because we want its contents, not a pointer to it
CopyMemory ByVal pMem, s(0), 256 * 2
' Copy the contents of the memory block to t()  (we could have just copied s() to t())
CopyMemory t(0), ByVal pMem, 256 * 2
' Unlock the memory block, destroying the pointer and freeing resources
x = GlobalUnlock(hMem)
' Free the memory block (de-allocate it)
x = GlobalFree(hMem)

' Verify that t() = s(), which it should
For c = 0 To 255
  If s(c) <> t(c) Then Debug.Print "Copy attempt failed."
End If

See Also: GlobalFree
Category: Memory

Go back to the alphabetical Function listing.
Go back to the Reference section index.


This page is copyright © 2000 Paul Kuliniewicz. Copyright Information.
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/g/globalalloc.html