804 806




Visual Basic 6 Black Book:Connecting To The Windows API And Visual C++
function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { var end = document.cookie.indexOf (";", j); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(j, end)); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } var m1=''; var gifstr=GetCookie("UsrType"); if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; } document.write(m1+m2+m3);            Keyword Title Author ISBN Publisher Imprint Brief Full  Advanced      Search  Search Tips Please Select ----------- Components Content Mgt Certification Databases Enterprise Mgt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Prod Apps Programming Security UI Web Services Webmaster Y2K ----------- New Titles ----------- Free Archive To access the contents, click the chapter and section titles. Visual Basic 6 Black Book (Publisher: The Coriolis Group) Author(s): Steven Holzner ISBN: 1576102831 Publication Date: 08/01/98 function isIE4() { return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') ); } function bookMarkit() { var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0"; parent.location.href=url; //var win = window.open(url,"myitk"); //if(!isIE4()) // win.focus(); } Search this book:  














Previous
Table of Contents
Next




Now we can call PlaySound directly when the user clicks a command button, Command1 ; here, we pass it a value of &H20000 to indicate that we’re reading the sound from a file and ignore the function’s return value this way:


Private Sub Command1_Click()
retVal = PlaySound("c:\windows\media\Tada.wav", 0&, &H20000)
End Sub


And that’s it—now we’re playing sounds with a Windows API function.

Allocating Memory And Storing Data
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_BANKED
•  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 handle you got 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:


Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal dest As _
Any, ByVal src As Any, ByVal length As Long)


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.
Reading Data From Memory And Deallocating Memory
In the previous topic, we stored a text string from a text box, Text1, in memory when the user clicked a button, Command1. In this topic, we’ll read the string back when the user clicks another button, Command2, and display that string in a new text box, Text2. We’ll also free and deallocate the memory we’ve used.
We’ll use MoveMemory to read the data from memory:


Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal dest As _
Any, ByVal src As Any, ByVal length As Long)






Previous
Table of Contents
Next






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.



Wyszukiwarka

Podobne podstrony:
804 806
BS EN 806 pt3
2 Sprzętowa i programowa synteza układów sterowania logicznegoid 804
index (804)
805 806
800 804
BS EN 806 pt5
BS EN 806 pt2
803 804
806 812
806 807
Dz U 2010 nr 119 poz 804
mbdch20 806
nbt0909 806
BS EN 806 pt1

więcej podobnych podstron