The Beginners Guide To API

Submitted on: 1/7/2015 2:44:00 PM
By: Dave Greenwood (from psc cd)  
Level: Beginner
User Rating: By 28 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0, VB Script, ASP (Active Server Pages)
Views: 1308
     This article teaches you the basics of Windows API by giving you a walk through of Declaring API Functions from Start to Finish & uses real examples of useful code you can use in your Projects!


The Beginners Guide To API

What is Windows API

It is Windows Application Programming Interface. This basically means that Windows has built in functions that programmers can use. These are built into its DLL files. (Dynamic Link Library)

So What can these functions do for me (you might ask) ?

These pre-built functions allow your program to do stuff without you actually have to program them.

Example: You want your VB program to Restart Windows, instead of your program communicating directly to the various bits & pieces to restart your computer. All you have to do is run the pre-built function that Windows has kindly made for you. This would be what you would type if you have VB4 32, or higher.

In your module

Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long

If you wanted your computer to shutdown after you press Command1 then type this In your Form under

Sub Command1_Click ()

X = ExitWindowsEx (15, 0)

End Sub


Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long

Now to Explain what the above means

Private Declare Function ExitWindowsEx tells VB to Declare a Private Function called "ExitWindowsEx".

The Lib "user32" part tells VB that the function ExitWindowsEx is in the Library (DLL file) called "user32".

The final part tells VB to expect the variables that the ExitWindowsEx function uses.

(ByVal uFlags As Long, ByVal dwReserved As Long) As Long

The ByVal means pass this variable by value instead of by reference.

The As Long tells VB what data type the information is.

You can find more about data types in your VB help files.

Now you should know what each part of the Declaration means so now we go on to what does

X = ExitWindowsEx (15, 0)

For VB to run a function it needs to know where to put the data it returns from the function. The X = tells VB to put the response from ExitWindowsEx into the variable X.

What's the point of X =

If the function runs or fails it will give you back a response number so you know what it has done.

For example if the function fails it might give you back a number other than 1 so you can write some code to tell the user this.

If x <> 1 Then MsgBox "Restart has Failed"


Now you should know what everything in the Declaration above means. You are now ready to start using API calls in your own VB projects.

To get you started I have included some useful API calls you might want to use that I've found on Planet Source Code.


Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Public Const SND_SYNC = &H0

Public Const SND_ASYNC = &H1
Public Const SND_NODEFAULT = &H2
Public Const SND_MEMORY = &H4
Public Const SND_LOOP = &H8
Public Const SND_NOSTOP = &H10

Sub Command1_Click ()

SoundName$ = File 'file you want to play example "C:\windows\kerchunk.wav"

X = sndPlaySound(SoundName$, wFlags%)

End sub


Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As Any, ByVal fuWinIni As Long) As Long


Sub Command1_Click ()
Dim strBitmapImage As String
strBitmapImage = "c:\windows\straw.bmp"
x = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, strBitmapImage, 0)

End sub


Declare Sub SHAddToRecentDocs Lib "shell32.dll" (ByVal uFlags As Long, ByVal pv As String)

Dim NewFile as String
Call SHAddToRecentDocs(2,NewFile)


Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long,ByVal dwNewLong As Long) As Long
Public Const GWL_EXSTYLE = (-20)
Public Const WS_EX_TRANSPARENT = &H20&

Private Sub Form_Load()



Any Problems email me at

Other 4 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.