Windows API and DLLs --- Part-I

Submitted on: 2/2/2015 2:12:00 AM
By: Deepanjan Datta (from psc cd)  
Level: Intermediate
User Rating: By 8 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 280
     An introduction to Windows API and DLLs!!! Part II on PSC!!! Part III coming soon


Windows API

Full form : API --- Application Programming Interface

DLL --- Dynamic Link Library

The Windows API is a collection of routines available to you, the Visual Basic programmer. In a way, these API routines are like internal functions of Visual Basic.

So many Windows API routines exist that just about anything you can do from Windows, you can do from a Visual Basic application by calling the appropriate Windows API routine.

All Windows API routines are stored in files called DLLs. Several thousand API routines are available for use.

Note : Most DLL files have '.DLL' extension.

Any program you write has access to the Windows DLLs.

Following are the three most common DLLs :

  • USER32.DLL --- Contains functions that control the Windows environment and the user's interface, such as cursors, menus, windows etc.

  • GDI32.DLL --- Contains functions that control output to the screen and other devices.

  • KERNEL32.DLL --- Contains functions that control the internal Windows hardware and software interface.

There are other DLLs such as COMDLG.DLL, MAPI32.DLL, NETAPI32.DLL, WINMM.DLL etc.

Using the 'Declare' statement

Calling Windows API routines requires a statement called 'Declare'.

The 'Declare' statement performs the following tasks :

  • Specifies where the API function is located

  • Identifies arguments needed by the API function by number and data type

  • Specifies whether or not the API function returns a value

The following format describes the subroutine procedure version of the 'Declare' statement :

Declare Sub procName Lib "libName" [Alias "alias"] [([ByVal] var1 [As dataType] [, [ByVal] var2 [As dataType]] ... [, [ByVal] varN [As dataType])]

Here are two examples :

Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA"_

(ByVal lpBuffer As String, ByVal nSize As Long) As Long

Declare Sub GetSystemInfo Lib "kernel32" (lpSystemInfo As SystemInfo)

Here is an example for calling a simple API routine:

This example sounds the speaker

Private Declare Function MessageBeep Lib "user32" (ByVal wType As Long) As Long

Private Sub cmdBeep_Click() 'You need to have a command button named cmdBeep for this example to work

Dim Beeper As Variant


End Sub

Other 2 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 Intermediate 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.