One reason programmers use Windows API calls is to work with a lot of memory, and you can use the GlobalAlloc (allocate memory), GlobalLock (lock the memory and get a pointer to it), GlobalUnlock (unlock the memory), and GlobalFree (deallocate the memory) functions for that. We’ll take a look at the first two of these
functions in this topic, and the last two in the following topic. We’ll also see how to copy data into and out of our newly allocated memory with the MoveMemory function.
Here’s how you use GlobalAlloc to allocate memory; this function returns a non-zero handle to the memory if successful:
Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
You set the flags you want to use in wFlags, and the number of memory bytes you want in dwBytes. Here are the possible flags to use with GlobalAlloc:
GMEM_FIXED—&H0
GMEM_MOVEABLE—&H2
GMEM_NOCOMPACT—&H10
GMEM_NODISCARD—&H20
GMEM_ZEROINIT—&H40
GMEM_MODIFY—&H80
GMEM_DISCARDABLE—&H100
GMEM_NOT_BANKED—&H1000
GMEM_SHARE—&H2000
GMEM_DDESHARE—&H2000
GMEM_NOTIFY—&H4000
GMEM_LOWER—GMEM_NOT_BANK
GMEM_VALID_FLAGS—&H7F72
GMEM_INVALID_HANDLE—&H8000
To get a pointer to the memory and so put it to use, you use GlobalLock, passing it the memory
from GlobalAlloc. GlobalLock returns a non-zero pointer to the memory if successful:
Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Besides GlobalAlloc and GlobalLock, you can move data into the memory you’ve allocated with MoveMemory:
Here are what the arguments to MoveMemory mean:
• dest—Pointer to the destination buffer
• src—Pointer to the source buffer
• length—Number of bytes to move
Let’s see an example. Here, we’ll store a string of text that the user types into a text box in memory; in the next topic in this chapter, we’ll read that string back. This example will give us a good general overview of working with memory and memory buffers.
We start by setting up a 40-character-long buffer for the string to store in the form’s (General) declarations section:
Const DataLength = 40
Dim outbuffer As String * DataLength
...
We also declare the memory handle and pointer we’ll use:
Const DataLength = 40
Dim outbuffer As String * DataLength
Dim hMemory As Long
Dim hMemoryPointer As Long
Finally, we declare the functions we’ll use, GlobalAlloc, GlobalLock, and MoveMemory, as well as the memory flag we’ll use, GMEM_MOVEABLE, which means that Windows can move the memory we are using if it needs to as part of its memory-handling operations:
Const DataLength = 40
Dim outbuffer As String * DataLength
Dim hMemory As Long
Dim hMemoryPointer As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As_
Long, ByVal dwBytes As Long) As Long
rivate Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) _
As Long
Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" _
(ByVal dest As Any, ByVal src As Any, ByVal length As Long)
Const GMEM_MOVEABLE = &H2
When the user clicks a command button, Command1, we will allocate and lock the memory, and store the text string now in Text1 in it. We start by storing the text from the text box in the buffer we’ve named outbuffer:
Private Sub Command1_Click()
outbuffer = Text1.Text
...
Next, we use GlobalAlloc to allocate the memory we’ll use:
Private Sub Command1_Click()
outbuffer = Text1.Text
hMemory = GlobalAlloc(GMEM_MOVEABLE, DataLength)
Next, we pass the memory handle from GlobalAlloc to GlobalLock to get a pointer to the memory we’ve allocated:
Private Sub Command1_Click()
outbuffer = Text1.Text
hMemory = GlobalAlloc(GMEM_MOVEABLE, DataLength)
hMemoryPointer = GlobalLock(hMemory)
Finally, we copy the data from our buffer to our newly allocated memory with MoveMemory (note that because MoveMemory is a subroutine, we use the Call keyword instead of assigning a return value to a variable):
Private Sub Command1_Click()
outbuffer = Text1.Text
hMemory = GlobalAlloc(GMEM_MOVEABLE, DataLength)
hMemoryPointer = GlobalLock(hMemory)
Call MoveMemory(hMemoryPointer, outbuffer, DataLength)
End Sub
And that’s it—when the user clicks Command1, we copy the text string to our allocated memory.
We’ve stored data in allocated memory now—how do we read it back? We’ll take a look at that in the next topic.
Tuesday, March 23, 2010
Allocating Memory and Storing Data
Diposting oleh RoemahInformatika
Label: Code
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment