GlobalUnlock Function

Declare Function GlobalUnlock Lib "kernel32.dll" (ByVal hMem As Long) As Long

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

GlobalUnlock decrements the lock count of a moveable memory block by one. The memory block is locked if its lock count is greater than zero; it is considered unlocked if its lock count is zero. A moveable memory block cannot be moved or discarded while it is locked. The function returns 1 if, after decrementing the lock count, the memory block is still locked. The function returns 0 if, after decrementing the lock count, either the memory block is now unlocked or an error occured.

hMem
A handle to the moveable memory block to decrement the lock count of.

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

Related Call: GlobalLock
Category: Memory
Back to the index.


Go to Paul Kuliniewicz's Home Page.
E-mail: rogue953@hotmail.com
This page is at http://skyscraper.fortunecity.com/transmission/45/api/g/globalunlock.html