Thursday, December 11, 2008

Encouraging the use of APIs

Encouraging the use of APIs

What is the API

The Application Programming Interface (API) is an interface to the many routines made available through Dynamic Link Libraries (DLLs). As you already know, Windows ships with hundreds of DLLs, and within these DLLs lie thousands of functions and procedures. Many of these routines are used by Windows and reused by other applications. Your applications can, and will, use some of these routines.

Using APIs, Windows manages memory, processes graphics, plays music, processes mouse events, accepts keyboard input and many other things. Each of these tasks requires several subtasks to be completed successfully. Using the API, you can make your application mimic or , even better, reuse the functionality available in Windows.

There are several reasons why to use Windows API. One of the reasons is it will be because Visual Basic doesn’t provide enough tools to accomplish a specific task. For Example, Visual Basic cannot easily play a wave file without another tool, such as the multimedia control or the API. There are some things Visual Basic can do, but the API can do it faster. For Example is rendering graphics on the screen. The reason is that the API Contains better memory management routines and time-proven algorithms that are optimized for performance.

Another reason to use the API is when you have limitations on the number of files that can be sent or shipped as a part of the product. For example, you could use the Common Dialog control in your project and ship its accompanying ActiveX control (OCX) file with your set up, however there are many instances where you would not want to ship the control with your project. An alternative would be use the API in your code and compile them directly into your executable.

Additional Functionality
API provides significant additional functionality that is lacking in Visual Basic. As mentioned earlier, it would be difficult to play a simple wave file without the help of another tool but in this situation, sndPlaySound API will be better.

Performance Gains
The API can provide many speed improvements for your development efforts. The examples will be discussed later.

Low Overhead
As you read this Knowledge base and develop your applications, you will see circumstances where it makes sense to use the API rather than the trusty control.

Static versus Dynamic Linking
Before using the APIs you should understand what is happening behind the scenes. Understanding, how Visual Basic works with APIs will help you better utilize the API in your development efforts.

Static Linking
Static libraries consist of .OBJ files that contain object code. Libraries are generally loaded in their entirety. Static libraries were created that contained several routines you could include in your application during the linking process. For Example, Library called Grapics.obj could have many graphics routines, but your application only requires one to clear the screen. When your application is linked, all functions in Graphics.obj are actually included in your executable file. As a result , your executable code becomes larger , and if you wanted to change the functionality you would need to re-link your application with new object library.

////image missing///

Dynamic Linking
Visual Basic provides extensibility through a process known as dynamic linking, which calls a routine within another program while your application is running.

///image missing///

If your application utilizes a function from DLL, your application imports that function at run-time when it is actually required. Although dynamic linking makes your code run slower than statically-linked code but provide lot more benefits. Your applications can be upgraded easily when new versions of DLLs arrive. Since the DLLs are designed to be backward compatible, you would not need to re-compile and distribute your application because one library was upgraded.

How to Find the Right API
APIs are generally stored within DLLs. There are several DLLs common to all 32-bit Windows platforms. These include Kernel32.DLL, User32.DLL, and GDI32.DLL.

Kernel32.DLL is responsible for handling most of the operating system’s functionality, including memory management, device I/O & most Windows environment functions. APIs such as GlobalAlloc, Global Lock, GlobalUnlock and Global Free allow you to allocate and lock memory for your applications. Other APIs, such as GetWindowsDirectory and GetSystemInfo gives information about your operating environment.

User32.DLL is responsible for GUI and window management. For example, User32.DLL exposes the MessageBox and MessageBoxEx APIs. These APIs display a message box just like the one created with Visual Basic’s MsgBox command. APIs like CreateWindow, GetForegroundWindow and GetFocus shows the basic window management functions. It also exposes APIs that allow to retrieves information from input devices. For Example, the GetKeyboardState API retrieves information about which keys are pressed on the keyboard.

GDI32.DLL is the library that handles most of the basic graphic functions. GDI stands for Graphics Device Interface & it handles graphics devices such as monitors, and printers.

The most effective method for selecting the right API is to start with the API that satisfies the basic requirements of the task you wish to accomplish. Once you know the right API, careful scrutiny of its arguments will determine if other APIs are needed to satisfy the required arguments.

You insert the API declaration into your project like:

Private Declare Function GetVersionEx Lib “kernel32” Alias “GetVersionExA” _ (lpVersionInformation As OSVERSIONINFO) As Long

Using the API Viewer
The first step is to load the API Viewer. Once it is started, load the appropriate API file into the viewer, as follows:
From the Add-Ins menu, select API Viewer.
In the API Viewer, select Load Text File…….from the File menu, or Load Database File…if you previously converted the file to a database.
Select the Winapi folder.

Within the Winapi folder you will see three API declaration files.
APILoad.txt
MAPI32.txt
Win32API.txt.

The API Type selector is used to narrow your list of choices. You can select from
Constants,
Declares and
Types.

Selecting Constants lists all the Constants declared in the file. The Declares option lists all the API declarations in the available Items list.
Finally, selecting Types will list all the data structures, or Types, as they are known in Visual Basic, in the Available Items list.

1 comment:

  1. Canadian gamers can select the payment option that works best for them from an extended record of choices. Bonuses aren’t as widespread at Ruby Fortune as they're at other on-line casinos. Ruby Fortune does supply a welcome bonus for brand spanking new|for model new} customers although. In the higher proper corner, click on on the green Deposit button and then select Withdrawal. You can solely withdraw as soon as} you have met the necessities, so when you make 룰렛 your first deposit, they will seem.

    ReplyDelete