VB:What Is The Windows API
From GPWiki
The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server. However, the GPWiki forums are still active! Come say hello. Literally, API means "Application Programming Interface." Not too descriptive by any stretch. The Windows API is actually a collection of pre-existing functions inherent in Windows that you can call upon to do jobs for you within the Windows environment. These functions are stored within the multitude of DLL's that come with Windows. To access these exotic functions, we first have to declare them in our code. The syntax, however, looks a little ugly. Here's an example:
Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long
Lets have a look at this. First, we see that this is a declare statement, this may be unfamiliar to some of you. The windows help files state that a declare is "used at module level to declare references to external procedures in a dynamic-link library (DLL)." That's exactly what we're doing here, we're referencing a procedure (actually a function in this case) inside the DLL called winmm.dll. This is the windows multimedia DLL, and within it resides a function called sndPlaySound which allows us to play wave files. The word Lib indicates that "a DLL or code resource contains the procedure being declared" and the word Alias indicates that "the procedure being called has another name in the DLL." The rest of the declare statement is simply the arguments passed to the procedure. NOTE: If you intend to declare an API call inside an object (such as a form) you must include the prefix Private before the statement. Ok, so now we have the function declared, and we're ready to use it in code. We now treat it the same as we would any procedure we created ourselves. For a specific example on how to use an API call from within code, see the tutorial on Playing WAV Sound Files. Where did this magic little piece of code come from? It can be found within the Winapi directory of your VB installation's root folder. Within the Win32api.txt file you'll see an incomprehensible list of procedures and constants that make up the API. If you already know what you're looking for, then this file may be useful to you, but generally it's best to read a book about the API or look at web sites in order to find the useful portions. API calls handling things such as wave output, cursor display, timing loops and more are explained in other tutorials available here. |


