Visual Basic & Win APIs

Tech Stuff

1/24/2005

Visual Basic & Win APIs




The Windows Application Programming Interface, or API, is a complex set of functions for interacting with Windows. In this article, Jason attempts to shed some light on this topic by explaining what an API is, and providing an example of how to access one through Visual Basic.The Windows Application Programming Interface, or API, is a complex set of functions which allow Visual Basic, C++, Java and Delphi programmers to gain access to many of the functions that windows uses internally, such as printing, thread-based resources, and the registry.

What is the Windows API?

You have probably heard of the Windows API numerous times, but you may not know exactly what it is. Basically, it is a whole set of implementation specific DLL hosted functions, which are accessible through Visual Basic, C++, Java and Delphi. The main API functions reside in three system DLL files, which are located on every computer that is using the Windows operating system.
1. User32.dll - Handles the user interface
2. Kernel32.dll - Working with files and your computers memory
3. Gdi32.dll - Graphical commands

There are many more DLLs which are accessible through the Windows Operating System, however, they are seperated into numerous other system DLL files, which we will not discuss here.

Microsoft bundled all of the API functions into system DLL's so that they were easily accessible for developers from a number of programming languages. One advantage of using the Windows API is that each function is highly optimised, and usually written in C/C++, meaning that they are a lot faster than using the standard set of functions available in Visual Basic.

It is possible to use functions from other DLL files, as long as you know the declarations to access the functions. The developer of the DLL must provide a detailed set of documentation, telling other developers how to create an API decleration for the functions contained within that DLL.

API calls can be used to perform almost any Windows task, from shutting down a computer to installing a printer. Because of the multi-threaded mature of the Windows operating system, several programs can access the Windows API simultaneously.

Windows API functions are very fast and powerful, but actually calling them up and executing them can be time consuming. I would recommend that you only use Windows API functions if the task you are trying to implement is not available as a regular Visual Basic function or statement. Of course, the task that you want to implement may not be available as a Windows API function.


Calling the API

Place a command Button on your form and set the caption to 'Show Windows Directory'. I am going to assume that the name of this command button has been left as Command1. We will be attaching code to the Click event of this button, so open up the Command1_Click() sub-routine and enter the code below:



Private Sub Command1_Click()
Dim TheResult
Dim TheWindowsDirectory As String

TheWindowsDirectory = Space(144)

'Fill 144 spaces in TheWindowsDirectory string
TheResult = GetWindowsDirectory(TheWindowsDirectory, 144)

'Get path and place it in TheResult string
If TheResult = 0 Then
     MsgBox "Cannot get the Windows Directory"
Else
     'Prepare the String for preview, and then display in a Message Box
     TheWindowsDirectory = Trim(TheWindowsDirectory)
     MsgBox "The Windows Path: " & TheWindowsDirectory
End If

End Sub


Run the program and click on the command button. A messagebox similar to the one shown below should be displayed:

This message box shows that Windows has been installed into the directory (or folder) with the path 'C:\Windows\'. Your box may vary to mine, depending on where Windows has been installed on your computer.

You may be asking yourself why an API function like this would be used. Well, if your project was built to perform a task that has a use for the Windows directory, then you are going to need to know the location of it, and using the API is the easiest way.

If you hard coded the windows directory into your application. then an error may occur, because not all people choose to install Windows to the default c:\windows directory.

There are other API functions available through the same DLL that perform a similar task to the one that we have just coded. For example, 'GetSystemDirectory' will display the full path to the Windows System Directory. In my case, the result would be 'C:\Windows\System', but it may say something different on other systems, depending on where Windows is installed.



Miles To Go