API call

Diposting oleh Admin | Saturday, July 19, 2008 | | 0 komentar »

Browsing through the list of available items in the API viewer, you can probably guess what most of the functions do, like the 'AddPrinter' function, for example. But what the heck is 'FlushConsoleInputBuffer'? It doesn't matter if you don't understand every single API call, but if you want to know what each one does, you can visit the Microsoft API resource page.

So, let's get started with writing our very first program that takes advantage of the Windows API. Start by creating a new project in Visual Basic 6. You will require a single form and one module (you can name them whatever you want, because it doesn't really matter in our example). The program that we are going to write will simply display a message box, which contains the path of the Windows directory.

Using the API Viewer, look for the function called 'GetWindowsDirectory'. When you've found it in the list, click the 'Add' button. Its source code should appear in the selected items box. Copy the source onto the Windows clipboard. In case you are unsure, here's what the source should look like:

Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long

We now have access to the source code that will call up the API function. As you can see, this API is located within the Kernal32.DLL file. With the source code copied to the Windows clipboard, return to Visual Basic and paste it in as part of the source code for the Module. It should be pasted at the top, before anything else. When your program is interpreted and executed, your application will now make a request directly to an API call. Our single API decleration is useless at the moment, however. We need to call the API function from within our program. That's what we will do right now...

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

0 komentar