776 779




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




Handling C/C++ And Windows Data Types
Notice also that the PlaySound arguments that use Windows C/C++ Hungarian prefix notation (for instance, the prefix of the variable name lpszName, lpsz, means this variable is a long pointer to a zero-terminated string). You’ll find a list of Hungarian notation prefixes in Table 23.1, which will let you unravel what the variable types in Windows API calls really are. The arguments to PlaySound are also passed with the ByVal keyword. What does this mean?
Table 23.1 Windows C/C++ Hungarian notation.
Prefix
Meaning

a
array

b
bool (int)

by
unsigned char (byte)

c
char

cb
count of bytes

cr
color reference value

cx, cy
short (count of x, y length)

dw
unsigned long (dword)

fn
function

h
handle

I
integer

m_
data member of a class

n
short or int

np
near pointer

p
pointer

l
long

lp
long pointer

s
string

sz
string terminated with a zero

tm
text metric

w
unsigned int (word)

x, y
short (x or y coordinate)


The standard Windows calling convention is actually the Pascal calling convention, which is not the same as the Visual Basic calling convention. When you pass a variable to a procedure in Visual Basic, Visual Basic usually passes a reference to the variable, and the called procedure then uses that reference to read (and possibly write) the value in the passed variable. That process is called passing arguments by reference. On the other hand, when you pass variables to the Windows API, you should often pass the variable’s value directly, not a reference to the variable. That process is called passing arguments by value. You specify which way to pass variables using the ByRef and ByVal keywords.
As you can see, there are a few interface issues that we have to face when connecting to the Windows API. Take a look at the first argument in PlaySound: lpszName. This is a long C/C++ pointer to a zero-terminated string buffer— how do we construct one of those in Visual Basic, which doesn’t even use pointers? It turns out that all we have to do is to pass a standard Visual Basic string for this argument, as we’ve done when we call PlaySound in Visual Basic:


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


In fact, you can handle the C/C++ data types needed when you use the Windows API by using Visual Basic data types, as shown in Table 23.2. It may look complex, but don’t worry—we’ll get practical experience passing variables using the conversions in Table 23.2 in the examples throughout this chapter. It’s easier than you think—for example, when you need to pass a long integer variable to the Windows API, you just use a Visual Basic long variable, passing it by value.

Table 23.2 C/C++ and Visual Basic variable types.
C/C++ Type
Passed As

handle
ByVal Long

int
ByVal Integer

long
ByVal Long

lpint
ByRef Integer

lplong
ByRef Long

lpstr
ByVal String

lpsz
ByVal String

lpvoid
ByRef Any

Sometimes, a Windows API procedure takes an argument of a Windows-defined type. For example, the MoveToEx function, which moves the current drawing position (we’ll see this function in this chapter), takes an argument of type POINTAPI:


Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal x _
As Long, ByVal y As Long, lpPoint As POINTAPI) As Long


You’ll find the declarations of types like POINTAPI in the win32api.txt file, and you can copy them and put them in a module in your own program (data types need to be declared in a module):


Type POINTAPI
x As Long
y As Long
End Type


Now you’re free to use that type in your Visual Basic programs like this:



Dim ptPoint As POINTAPI

ptPoint.x = 0
ptPoint.y = 0

lngRetVal = MoveToEx(intHandle, intX, intY, ptPoint)


In this way, you can handle the data types and data structures needed by Windows procedures. We’ll get more experience with this throughout the chapter.

Now that we’ve gotten an overview of how to connect to the Windows API and Visual C++ through dynamic link libraries, the next question is, what’s in those DLLs for us to use?

NOTE:  Depending on your version of Windows, you may have to use longs for handles instead of integers. If you get overflows when assigning handles to integers, switch to longs.

What’s Available In The Windows API?
You’ll find the procedures, constants, and types used in the Windows API in the file win32api.txt. You can open that file in a text editor like the Windows WordPad and copy and paste the declarations you need into your Visual Basic program, as we’ll do in this chapter. However, the win32api.txt file only includes the raw declarations for procedures, constants, and types, but it doesn’t tell you what all the variables mean. To find reference information on the Windows API set, refer to the Microsoft Win32 Software Development Kit (SDK), which, depending on your version of Visual Basic, may be included on the Microsoft Developer Network Library CD.

You can also use the Visual Basic API Viewer add-in tool to work with win32api.txt. This tool appears in Visual Basic’s Add-Ins menu (if it doesn’t appear in your Add-Ins menu, you can add the API Viewer to Visual Basic with the Add-Ins menu’s Add-In Manager item). You can open win32api.txt in the API Viewer, as shown in Figure 23.1.

Figure 23.1  The Visual Basic API Viewer.
The API Viewer makes it easy to work with the procedures in the Windows API and add them to your Visual Basic program. To add a declaration to your program, just select the procedure you want to add, set its declaration to Public or Private with the option buttons in the API Viewer, click the Add button to add it to the Selected Items box, and click the Insert button to add those items to your program.



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