Introduction to the Windows API n API Application

  • Slides: 22
Download presentation
Introduction to the Windows API n API - Application Programming Interface n an API

Introduction to the Windows API n API - Application Programming Interface n an API is the software interface for things such as the OS n an API is the fundamental level of higher-level programming n in high-level programming, a program has an intermediate to execute tasks n Microsoft Word does not directly control the printing it puts a print task on the OS’s print queue (an API call)

Benefits of API Programming n n Using API calls in Visual Basic can cut

Benefits of API Programming n n Using API calls in Visual Basic can cut down on the number of dependency files and thus the size of the application package which you need to deploy to users. Sometimes now and especially in previous versions of VB API calls were the only way to do certain things like starting and running executable files to do this now use the FSO File System Objects in the Scripting Runtime Library

Benefits of API Programming n Designers of software do not have to worry about

Benefits of API Programming n Designers of software do not have to worry about the basic chores involved in every program (such as disk access, memory allocation, displaying graphics n Every time there is an improvement in the way hardware is accessed each program would have to be rewritten to take advantage of these changes. API calls are more powerful than VB methods Powerful meaning can do more / has more features n

API Drawbacks n API functions are significantly more error -prone n API functions are

API Drawbacks n API functions are significantly more error -prone n API functions are prone to fail spectacularly - shut down with a GPF n Visual Basic comes with almost no API documentation n syntactically challenging for non C++ programmers

Where is the Windows API? n C: WindowsSystem directory Win 32 API another term

Where is the Windows API? n C: WindowsSystem directory Win 32 API another term for these DLLs n user 32. dll (user interface functions) most functions we use are in here n kernel 32. dll (o. s. kernel functions) n gdi 32. dll (graphics device interface functions) n shell 32. dll (Windows shell functions).

Accessing the Microsoft Windows API n The Windows API contains thousands of functions, subs,

Accessing the Microsoft Windows API n The Windows API contains thousands of functions, subs, types, and constants that you can declare and use in your projects n These procedures are written in the C language so they must be declared before you can use them with VB n the easiest way to access the Windows API is by using the predefined declares included with Visual Basic

API Viewer Application n API Viewer is a VB Add-In. It uses Win 32

API Viewer Application n API Viewer is a VB Add-In. It uses Win 32 api. txt, located in the Winapi subdirectory of the main VB directory. You need to load this file! n The viewer allows you to search through this text file to put together a series of dependant function calls. n Sometimes you need to use functions that get values for the function you’re really interested in using

Components of the Win API n Functions - provide API functionality n Structures multiple

Components of the Win API n Functions - provide API functionality n Structures multiple individual variables passed between functions n Named Constants - numeric codes for information n Callback Functions defined completely in your program a way to process each item found belonging to the group n Messages are sent to objects to tell them to do something

Declaring the Function n Before an API function can be using in Visual Basic,

Declaring the Function n Before an API function can be using in Visual Basic, it must first be declared n The Declare statement can only appear in the (declarations) section of a form or module n If it appears in a form, the declaration must be Private n In a module, the declaration can be either Public or Private

[{Public | Private}] Declare Function function_name Lib "DLL_filename" [Alias "function_alias"] (argument_list) As data_type n

[{Public | Private}] Declare Function function_name Lib "DLL_filename" [Alias "function_alias"] (argument_list) As data_type n function_name - safest to make this the same as the"official" name n DLL_filename - name of the DLL file which stores the function. This does not include the path n function_alias almost every function which has a string as a parameter has two versions ANSI or Unicode for

[{Public | Private}] Declare Function function_name Lib "DLL_filename" [Alias "function_alias"] (argument_list) As data_type n

[{Public | Private}] Declare Function function_name Lib "DLL_filename" [Alias "function_alias"] (argument_list) As data_type n n n English speakers use ANSI version ends with the letter A Unicode version ends with the letter W argument_list same as VB data_type the return type almost always a long

[{By. Val | By. Ref}] argument_name As data_type, . . . n argument_name -

[{By. Val | By. Ref}] argument_name As data_type, . . . n argument_name - gives clue as to what the argument represents can use any name but best to use “official” name n data type specifies the size and format n Allowed Data types Byte An 8 -bit integer. Integer A 16 -bit integer. Long A 32 -bit integer. String A variable-length string.

By. Val and By. Ref n the method used to pass a parameter to

By. Val and By. Ref n the method used to pass a parameter to the API function n By. Val This method prevents the function from altering the contents n By. Ref this method passes a sort of reference to the variable itself n Strings are always passed By. Val structures are always passed By. Ref Entire Arrays are always passed By. Ref

h. DC & h. Wnd – h. Wnd Handle A unique 32 bit integer

h. DC & h. Wnd – h. Wnd Handle A unique 32 bit integer defined by the operating environment and used by a program to identify and switch to an object, such as a form or control. – h. DC device context similar in appearance to handles can be the intermediary between your program and a physical device also windows themselves are considered to be devices need h. DC to draw (use graphical methods) on an object

Pointers and Flags n pointer a 32 -bit integer variable which holds a memory

Pointers and Flags n pointer a 32 -bit integer variable which holds a memory address usually the location of some other object n VB has little support for pointers 99. 5% of the time Visual Basic handles pointer for you A flag is simply a type of named constant. The special thing about flags is that they can be combined with other related flags like (shift control alt) mask n

Using API Structures n Structures allow a function to receive or return a large

Using API Structures n Structures allow a function to receive or return a large amount of information without cluttering the argument list n Structures almost always group related information n To define a structure in Visual Basic, the Type block is used

[(Public | Private}] Type type_name member 1 As data_type 1 member 2 As data_type

[(Public | Private}] Type type_name member 1 As data_type 1 member 2 As data_type 2. . . End Type n n type_name The name of the structure member 1, member 2, . . . – The name of an individual member of the structure n data_type 1, data_type 2, . . . – The data type of a particular item in the structure

Type EXAMPLESTRUCT longvar As Long another As Long astruct As RECT End Type n

Type EXAMPLESTRUCT longvar As Long another As Long astruct As RECT End Type n To access a data member of the structure use the. (period) operator between the variable name and the member name n notice one of the members is another structure the RECT angle structure n defined a variable to use the structure n Dim ex As EXAMPLESTRUCT ex. longvar = 54 ‘ store 54 here

Rect is a predefined structure n If you want to use it you must

Rect is a predefined structure n If you want to use it you must define it like this Type Rect left As Long top As Long right As Long bottom As Long End Type n Rect a convenient way to keep the necessary coordinates of a rectangle grouped together

Using API Callback Functions n a powerful tool, giving great flexibility to some API

Using API Callback Functions n a powerful tool, giving great flexibility to some API functions n allows your program to build its own routines to handle events generated by the API functions themselves n Windows does not define any "default" callback functions n The most common examples of callback functions occur in enumeration

Enumeration n During an enumeration, the invoked API function locates all objects which fit

Enumeration n During an enumeration, the invoked API function locates all objects which fit the desired category n However, the API function does not know what to do with all the handles it finds n Callback functions typically process some data with these handles during the middle of a API function call

The Address. Of Operator n n The only specific pointer in VB It is

The Address. Of Operator n n The only specific pointer in VB It is a pointer to an address of a function defined by your program n This function must be Public and be defined in a module (not a form). n can only be used inside of the argument list of a call to a function; it cannot be used any other time