|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Here are what the arguments to MoveMemory mean:
To allocate memory, we used GlobalAlloc to get a handle to a memory area; to use that memory, we used GlobalLock to get a pointer to the memory area. To unlock memory, you pass a pointer to that memory to GlobalUnlock: Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long To free memory, you pass a memory handle to GlobalFree: Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long Lets see this at work. Here, we add the declarations of the functions well use to the program we developed in the previous topic, as well as a buffer, inbuffer, to store the data we read:
Const DataLength = 40
Const GMEM_MOVEABLE = &H2
Dim outbuffer As String * DataLength
Dim inbuffer As String * DataLength
Dim memHandle As Long
Dim memPointer As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long,_
ByVal dwBytes As Long) As Long
Private 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)
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) _
As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) _
As Long
Then, when the user clicks Command2, we can use the memory pointer we created in the program in the previous topic, hMemoryPointer, with MoveMemory, to copy the string to the buffer:
Private Sub Command2_Click()
Call MoveMemory(inbuffer, hMemoryPointer, DataLength)
...
Now we can move the string to Text2 from inbuffer:
Private Sub Command2_Click()
Call MoveMemory(inbuffer, hMemoryPointer, DataLength)
Text2.Text = inbuffer
...
Finally, we unlock and free the memory weve used:
Private Sub Command2_Click()
Call MoveMemory(inbuffer, hMemoryPointer, DataLength)
Text2.Text = inbuffer
GlobalUnlock (hMemoryPointer)
GlobalFree (hMemory)
End Sub
Thats itwhen you run the program, as shown in Figure 23.10, and click the Store Text In Memory button, the text in the top text box, Text1, is stored in memory. When you click the Read Text From Memory button, the string is read back and displayed in the bottom text box, Text2, as you see in Figure 23.10. Now were using memory with the Windows API.
The code for this example is located in the winmemory folder on this books accompanying CD-ROM. Making A Window TopmostYou can use SetWindowPos to make a window topmost, which means itll always stay on top of other windows:
Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal _
hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal _
cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Heres what the arguments to this function mean:
Lets see an example. Here, well size a window to 100 x 100 pixels and make it a topmost window. We start by declaring SetWindowPos in our program, as well as the constants well use, SWP_SHOWWINDOW, SWP_DRAWFRAME, and HWND_TOPMOST:
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, _
ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long,
ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Const HWND_TOPMOST = -1
Const SWP_SHOWWINDOW = &H40
Const SWP_DRAWFRAME = &H20
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |