Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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)

2 Benefits of API Programming 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. n 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

3 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. n API calls are more powerful than VB methods Powerful meaning can do more / has more features

4 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

5 Where is the Windows API? n C:\Windows\System directory Win32 API another term for these DLLs n user32.dll (user interface functions) most functions we use are in here n kernel32.dll (o.s. kernel functions) n gdi32.dll (graphics device interface functions) n shell32.dll (Windows shell functions).

6 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

7 API Viewer Application n API Viewer is a VB Add-In. It uses Win32api.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

8 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

9 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

10 [{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

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

12 [{ByVal | ByRef}] 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.

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

14 hDC & hWnd –hWnd 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. –hDC 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 hDC to draw (use graphical methods) on an object

15 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 n 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

16 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

17 [(Public | Private}] Type type_name member1 As data_type1 member2 As data_type2... End Type n type_name The name of the structure n member1, member2,... –The name of an individual member of the structure n data_type1, data_type2,... –The data type of a particular item in the structure

18 Type EXAMPLESTRUCT longvar As Long another As Long astruct As RECT End TypeRECT 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 Dim ex As EXAMPLESTRUCT ex.longvar = 54 ‘ store 54 here

19 Rect is a predefined structure 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

20 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

21 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

22 The AddressOf Operator n The only specific pointer in VB n 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


Download ppt "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."

Similar presentations


Ads by Google