Vous êtes sur la page 1sur 9

Chapter 13

Scripting Quicktest Professional


Page 1
WINDOWS 32 API................................................................................................................... 1
W HAT IS API?........................................................................................................................ 2
D ESIGN M ODELS ............................................................................................................... 2
R ELEASE P OLICIES ............................................................................................................ 3
API E XAMPLES ................................................................................................................. 3
T HE W INDOWS API ............................................................................................................... 4
DLL (D YNAMIC L INK L IBRARY ) F ILES ................................................................................. 4
F UNDAMENTALS OF A DLL.................................................................................................... 4
B ACKGROUND ........................................................................................................................ 5
H ISTORY ................................................................................................................................ 5
W INDOWS M ESSAGES ............................................................................................................ 6
W INDOWS H ANDLES .............................................................................................................. 7
O VERVIEW OF THE W INDOWS API C OMPONENTS ................................................................
7
B ASE S ERVICES ................................................................................................................ 7
G RAPHICS D EVICE I NTERFACE .......................................................................................... 7
U SER I NTERFACE .............................................................................................................. 8
C OMMON D IALOG B OX L IBRARY ...................................................................................... 8
C OMMON C ONTROL L IBRARY ........................................................................................... 8
W INDOWS S HELL .............................................................................................................. 8
N ETWORK S ERVICES ......................................................................................................... 9
W EB API S ......................................................................................................................... 9
M ULTIMEDIA RELATED API S ............................................................................................ 9
API S FOR INTERACTION BETWEEN PROGRAMS ................................................................. 10
W INDOWS API T OOLS ......................................................................................................... 11
API V IEWER ................................................................................................................... 11
T HE E XTERN O BJECT .......................................................................................................... 12
L IMITATIONS USING THE E XTERN O BJECT ....................................................................... 12
C ONVERTING TO E XTERN .D ECLARE ................................................................................ 13
Example 1: GetPrivateProfileString Function ............................................................... 13
Example 2: GetServiceKeyName Function................................................................... 15
H IGHLIGHTS FOR W IN 32 API IN Q UICK T EST ................................................................... 17
Passing Parameters to Win32 API functions ................................................................. 18
Callback Functions....................................................................................................... 19
Interacting with a custom dll file .................................................................................. 19
T HE E XTERN O BJECT ........................................................ E RROR ! B OOKMARK NOT
DEFINED .
A PPENDIX 13.A .................................................................................................................... 19
Declare Data Types...................................................................................................... 22
Windows 32 API
This tutorial is about how to add additional functionality to your applications by
using the Windows Advanced Programming Interface.
Windows API ( A pplication P rogramming I nterfaces)is a set of predefined
Windows functions used to control the appearance and behavior of every Windows
element (from the outlook of the desktop window to the allocation of memory for a
new process). Every user action causes the execution of several or more API
Dani Vainstein
Win32API
Page 1 of 22Chapter 13
Scripting Quicktest Professional
Page 2
function telling Windows what's happened.
It is something like the native code of Windows. Other languages just act as a
shell to provide an automated and easier way to access APIs.
QuickTest supports a VBScript development language and its functionality can
be extended in two ways. The first way, is by using custom controls. These are
software components that are easily incorporated into QTP/VBS scripts. There are
custom controls for almost any task imaginable, including numerical analysis,
speech recognition, image and document support. Custom controls are called
Ocxs.
The second way, QuickTest can be extended is through the Windows Application
Programming Interface (API), using the Extern object. API functions are not
supported in VBSript.
What is API?
An application programming interface (API) is a source code interface that a
computer system or program library provides in order to support requests for
services to be made of it by a computer program.
An API differs from an Application Binary Interface in that it is specified in terms of
a programming language that can be compiled when an application is built, rather
than an explicit low level description of how data is laid out in memory.
The software that provides the functionality described by an API is said to be an
implementation of the API. The API itself is abstract, in that it specifies an
interface and does not get involved with implementation details.
An API is often a part of a software development kit (SDK).
The term API is used in two related senses:
A coherent interface consisting of several classes or several sets of related
functions or procedures.
A single entry point such as a method, function or procedure.
In general terms, an API is system software for an operating system or
environment, which consists of a standardized set of functions and procedures.
Programmers can call these functions and procedures from their programs to gain
extra functionality. Because programmers do not have to write this code
themselves, they save time. The system also provides a standard and well
documented way of working
Design Models
There are various design models for APIs. Interfaces intended for the fastest
execution often consist of sets of functions, procedures, variables and data
structures. However, other models exist as well - such as the interpreter used to
evaluate expressions in ECMAScript/JavaScript or in the abstraction layer - which
relieve the programmer from needing to know how the functions of the API relate
to the lower levels of abstraction. This makes it possible to redesign or improve
the functions within the API without breaking code that relies on it.
Some APIs, such as the ones standard to an operating system, are implemented
Dani Vainstein
Win32API
Page 2 of 22Chapter 13
Scripting Quicktest Professional
Page 3
as separate code libraries that are distributed with the operating system. Others
require software publishers to integrate the API functionality directly into the
application. This forms another distinction in the examples above. Microsoft
Windows APIs come with the operating system for anyone to use. Software for
embedded systems such as video game consoles generally falls into the
application-integrated category. While an official PlayStation API document may be
interesting to read, it is of little use without its corresponding implementation, in
the form of a separate library or software development kit.
An API that does not require royalties for access and usage is called "open".[1]
Although usually authoritative "reference implementations" exist for an API (such
as Microsoft Windows for the Win32 API), there is nothing that prevents the
creation of additional implementations. For example, most of the Win32 API can
be provided under a UNIX system using software called Wine.
Release Policies
Two general lines of API publishing policies:
Some companies zealously guard information on their APIs from general public
consumption. For example, Sony used to make its official PlayStation 2 API
available only to licensed PlayStation developers. This enabled Sony to control who
wrote PlayStation 2 games. Such control can have quality control benefits and
potential license revenue.
Some companies make their APIs freely available. For example, Microsoft makes
most of its API information public, so that software will be written for the Windows
platform.
Companies base their choice of publishing policy on maximizing benefit to them.
API Examples
The PC BIOS call interface
Single UNIX Specification (SUS)
Microsoft Win32 API
Java Platform, Enterprise Edition APIs
ASPI for SCSI device interfacing
Carbon and Cocoa for the Macintosh OS
OpenGL cross-platform API
DirectX for Microsoft Windows
Simple DirectMedia Layer (SDL)
Universal Home API
LDAP Application Program Interface
svgalib for Linux and FreeBSD
Google Maps API
Wikipedia API
Webmashup.com The Open Directory for Mashups & Web 2.0 APIs
Dani Vainstein
Win32API
Page 3 of 22Chapter 13
Scripting Quicktest Professional
Page 4
The Windows API
The API functions reside in DLLs (like User32.dll, GDI32.dll, Shell32.dll, ...) in the
Windows system directory.
The Windows API is the name given by Microsoft to the core set of application
programming interfaces available in the Microsoft Windows operating systems. It
is designed for usage by C/C++ programs and is the most direct way to interact
with a Windows system for software applications. Lower level access to a
Windows system
A S oftware D evelopment K it (SDK) is available for Windows, which provides
documentation and tools to enable developers to create software using the
Windows API and associated Windows technologies.
The Windows API is the set of functions and procedures available in Windows.
They can be used by all sorts of programmers. Mercury added the API access
interface including those working in QuickTest.
The most often required functionality of the Windows API already exists in the
VBScript basic development environment. Most of the API functions and
procedures that have not been included in VBScript itself can still be called from
the QuickTest application. The small percentage that cannot, are rarely required.
DLL (Dynamic Link Library) Files
Dynamic-link library (DLL), also known as dynamic link library (without the
hyphen), is Microsoft's implementation of the shared library concept in the
Microsoft Windows operating systems. These libraries usually have the file
extension DLL, OCX (for libraries containing ActiveX controls), or DRV (for legacy
system drivers).
D ynamic L ink L ibrary, a library of executable functions or data that can be used
by a Windows application. Typically, a DLL provides one or more particular
functions and a program accesses the functions by creating either a static or
dynamic link to the DLL. A static link remains constant during program execution
while a dynamic link is created by the program as needed. DLLs can also contain
just data. DLL files usually end with the extension .dll,.exe., drv, or .fon. A DLL
can be used by several applications at the same time. Some DLLs are provided
with the Windows operating system and available for any Windows application.
Other DLLs are written for a particular application and are loaded with the
application.
Fundamentals of a DLL
We have mentioned that a DLL is created as a project that contains at least one
source file and this source file should present an entry-point. After creating the
DLL, you will build and distribute it so other programs can use it. When building it,
you must create a library file that will accompany the DLL. This library file will be
used by other programs to import what is available in the DLL:
Dani Vainstein
Win32API
Page 4 of 22Chapter 13
Scripting Quicktest Professional
Page 5
When the import library is created, it contains information about where each
available function is included in the DLL and can locate it. When an application
needs to use a function contained in the DLL, it presents its request to the import
library. The import library checks the DLL for that function. If the function exists,
the client program can use it. If it doesn't, the library communicates this to the
application and the application presents an error.
Background
The original purpose for DLLs was saving both disk space and memory required
for applications by storing it locally on the hard drive. In a conventional non-
shared library, sections of code are simply added to the calling program. If two
programs call the same routine, that code would be duplicated. Instead, any code
which many applications share could be separated into a DLL which only exists as
a single disk file and a single instance in memory. Extensive use of DLLs allowed
early versions of Windows to work under tight memory conditions.
DLLs provide the standard benefits of shared libraries, such as modularity.
Modularity allows changes to be made to code and data in a single self-contained
DLL shared by several applications without any change to the applications
themselves. This basic form of modularity allows for relatively compact patches
and service packs for large applications, such as Microsoft Office, Microsoft
Visual Studio, and even Microsoft Windows itself.
Another benefit of the modularity is the use of generic interfaces for plug-ins. A
single interface may be developed which allows old as well as new modules to be
integrated seamlessly at run-time into pre-existing applications, without any
modification to the application itself. This concept of dynamic extensibility is taken
to the extreme with ActiveX.
With this many benefits, using DLLs also has a drawback: the DLL hell, when
several applications conflict on which version of a shared DLL library is to be used.
Such conflicts can usually be resolved by placing the different versions of the
problem DLL into the applications' folders, rather than a system-wide folder;
however, this also nullifies the savings provided by using shared DLLs. Currently,
Microsoft .NET is targeted as a solution to the problem of DLL hell by allowing
side-by-side coexistence of different versions of a same shared library. With
modern computers which have plenty of disk space and memory, it can be a
reasonable approach.
History
The Windows API has always exposed a large part of the underlying structure of
the various Windows systems for which it has been built to the programmer. This
has had the advantage of giving Windows programmers a great deal of flexibility
and power over their applications. However, it also has given Windows applications
a great deal of responsibility in handling various low-level, sometimes tedious,
Dani Vainstein
Win32API
Page 5 of 22Chapter 13
Scripting Quicktest Professional
Page 6
operations that are associated with a Graphical user interface.
A hello world program is a frequently used programming example, usually
designed to show the easiest possible application on a system that can actually do
something (i.e. print a line that says "Hello World").
Over the years, various changes and additions were made to the Windows
Operating System, and the Windows API changed and grew to reflect this. The
Windows API for Windows 1.0 supported fewer than 450 function calls, where in
modern versions of the Windows API there are thousands. However, in general,
the interface remained fairly consistent, and an old Windows 1.0 application will
still look familiar to a programmer who is used to the modern Windows API.[12]
A large emphasis has been put by Microsoft on maintaining software backwards
compatibility. To achieve this, Microsoft sometime even went as far as supporting
software that was using the API in an undocumented or even (programmatically)
illegal way.
One of the largest changes the Windows API underwent was the transition from
Win16 (shipped in Windows 3.1 and older) to Win32 (Windows NT and Windows
95 and up). While Win32 was originally introduced with Windows NT 3.1 and
Win32s allowed usage of a Win32 subset before Windows 95, it was not until
Windows 95 that many applications began being ported to Win32. To ease the
transition, in Windows 95, both for external developers and for Microsoft itself, a
complex scheme of API thinks was used that could allow 32 bit code to call into 16
bit code and (in limited cases) vice-versa. So-called flat thunks allowed 32 bit code
to call into 16 bit libraries, and the scheme was used extensively inside Windows
95 to avoid porting the whole OS to Win32 itself in one chunk. In Windows NT,
the OS was pure 32-bit (except the parts for compatibility with 16-bit applications)
and the only thunk available was generic thunks which only thunks from Win16 to
Win32 and worked in Windows 95 too. The Platform SDK shipped with a
compiler that could produce the code necessary for these thunks.
Windows Messages
Messages are the basic way Windows tells your program that some kind of input
has occurred and you must process it. A message to your form is sent when the
user clicks on a button, moves the mouse over it or types text in a textbox. All
messages are sent along with four parameters
A window handle, a message identifier and two 32-bit (Long) values. The
window handle contains the handle of the window the message is going to.
The identifier is actually the type of input occurred (click, mousemove.
Two value specify an additional information for the message (like where is the
mouse cursor when the mouse is been moved).
But, when messages are sent to you, why you dont see them, looks like someone
is stealing your mail. And before you get angry enough, let me tell you.
The theft is actually Your Application. But he does not steal your mail, but instead
read it for you and give you just the most important in a better look (with some
information hidden from time to time). This better look is the events you write
code for.
So, when the user moves the mouse over your form, Windows sends
Dani Vainstein
Win32API
Page 6 of 22Chapter 13
Scripting Quicktest Professional
Page 7
WM_MOUSEMOVE to your window, Your application get the message and its
parameters and executes the code you've entered for Button_MouseMove event.
Something else that needs to be said: You can send massages to your own window
or to another one yourself. You just call SendMessage or PostMessage
(SendMessage will cause the window to process the message immediately and
PostMessage will post it onto a queue, called message queue, after any other
messages waiting to be processed (it will return after the message is processed,
i.e. with some delay)). You must specify the window handle to send the message
to, the message and the two 32-bit values.
Windows Handles
A window handle (usually shortened to hWnd) is a unique identifier that Windows
assigns to each window created. By window in this case we are referring to
everything from command buttons and textboxes, to dialog boxes and full
windows.
The window handle is used in APIs as the sole method of identifying a window. It
is a Long (4 byte) value and you can get the handle for forms and almost all
controls.
Windows identifies every form, control, menu, button, and menu item or
whatever you can think of by its handle. When your application is run, every
control on it is assigned a handle which is used later to separate the button from
the rest of the controls. If you want to perform any operation on the button
through an API you must use this handle. Where to get it from? Well API has
provided an hWnd property for all controls that have handles in Windows.
Overview of the Windows API Components
Base Services
Provide access to the fundamental resources available to a Windows system.
Included are things like file systems, devices, processes and threads, access to the
Windows registry, and error handling. These functions reside in kernel.exe,
krnl286.exe or krnl386.exe files on 16-bit Windows, and kernel32.dll and
advapi32.dll on 32-bit Windows.
For more information:
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/winprog/winprog/base_services.asp
Graphics Device Interface
Provide the functionality for outputting graphical content to monitors, printers and
other output devices. It resides in gdi.exe on 16-bit Windows, and gdi32.dll on 32-
bit Windows.
For more information:
http://msdn.microsoft.com/library/default.asp?url=/library/en-
Dani Vainstein
Win32API
Page 7 of 22Chapter 13
Scripting Quicktest Professional
Page 8
us/winprog/winprog/graphics_device_interface.asp
User Interface
Provides the functionality to create and manage screen windows and most basic
controls, such as buttons and scrollbars, receive mouse and keyboard input, and
other functionality associated with the GUI part of Windows. This functional unit
resides in user.exe on 16-bit Windows, and user32.dll on 32-bit Windows. Since
Windows XP versions, the basic controls reside in comctl32.dll, together with the
common controls (Common Control Library).
For more information:
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/winprog/winprog/user_interface.asp
Common Dialog Box Library
Provides applications the standard dialog boxes for opening and saving files,
choosing color and font, etc. The library resides in a file called commdlg.dll on 16-
bit Windows, and comdlg32.dll on 32-bit Windows. It is grouped under the User
Interface category of the API.
For more information:
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/winui/winui/windowsuserinterface/userinput/commondialogboxlibrary.asp
Common Control Library
Gives applications access to some advanced controls provided by the operating
system. These include things like status bars, progress bars, toolbars and tabs.
The library resides in a DLL file called commctrl.dll on 16-bit Windows, and
comctl32.dll on 32-bit Windows. It is grouped under the User Interface category of
the API.
For more information:
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/winprog/winprog/common_control_library.asp
Windows Shell
Component of the Windows API allows applications to access the functionality
provided by the operating system shell, as well as change and enhance it. The
component resides in shell.dll on 16-bit Windows, and shell32.dll and later in
Windows 95 shlwapi.dll on 32-bit Windows. It is grouped under the User Interface
category of the API.
For more information:
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/winprog/winprog/windows_shell.asp and
http://msdn.microsoft.com/library/default.asp?url=/library/en-
Dani Vainstein
Win32API
Page 8 of 22Chapter 13
Scripting Quicktest Professional
Page 9
us/shellcc/platform/shell/programmersguide/shell_intro.asp
Network Services
Give access to the various networking capabilities of the operating system. Its
sub-components include NetBIOS, Winsock, NetDDE, RPC and many others.
For more information:
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/winprog/winprog/network_services.asp
Web APIs
The Internet Explorer web browser also exposes many APIs that are often used by
applications, and as such could be considered a part of the Windows API. Internet
Explorer has been an integrated component of the operating system since
Windows 98, and provides web related services to applications. The integration will
stop with Windows Vista. Specifically, it provides:
An embeddable web browser control, contained in shdocvw.dll and mshtml.dll.
The URL monikers service, held in urlmon.dll, which provides COM objects to
applications for resolving URLs. Applications can also provide their own URL
handlers for others to use.
A library for assisting with multi-language and international text support
(mlang.dll).
DirectX Transforms, a set of image filter components.
XML support (the MSXML components).
Access to the Windows Address Book.
Multimedia related APIs
Microsoft has provided the (DirectX) set of APIs as part of every Windows
installation since Windows 95 OSR2. DirectX provides a loosely related set of
multimedia and gaming services, including:
Direct3D as an alternative to OpenGL for access to 3D acceleration hardware.
DirectDraw for hardware accelerated access to the 2D framebuffer. As of
DirectX 9, this component has been deprecated in favor of Direct3D, which
provides more general high-performance graphics functionality (2D rendering,
after all, is really just a subset of 3D rendering).
DirectSound for low level hardware accelerated sound card access.
DirectInput for communication with input devices such as joysticks and
gamepads.
DirectPlay as a multiplayer gaming infrastructure. This component has been
deprecated as of DirectX 9 and Microsoft no longer recommends its use for
game development.
DirectShow which builds and runs generic multimedia pipelines. It is
Dani Vainstein
Win32API
Page 9 of 22Chapter 13
Scripting Quicktest Professional
Page 10
comparable to the GStreamer framework and is often used to render in-game
videos and build media players (Windows Media Player is based upon it).
DirectShow is no longer recommended for game development.
DirectMusic
APIs for interaction between programs
The Windows API mostly concerns itself with the interaction between the Operating
System and an application. For communication between the different Windows
applications among themselves, Microsoft has developed a series of technologies
alongside the main Windows API. This started out with Dynamic Data Exchange
(DDE), which was superseded by Object Linking

Vous aimerez peut-être aussi