Accessing the Windows API in Visual Basic - Your first API call
(Page 4 of 6 )
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, located at
http://www.microsoft.com/api/.
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 LongWe 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...
Next: Calling the API >>
More Visual Basic Articles
More By Jason Brimblecombe