The Beginners Guide To API by David Greenwood

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.

PLAY A WAVE FILE (WAV)

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"

wFlags% = SND_ASYNC Or SND_NODEFAULT
            X = sndPlaySound(SoundName$, wFlags%)

End sub

CHANGE WALLPAPER

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

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

End sub

ADD FILE TO DOCUMENTS OF WINDOWS MENU BAR

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

Dim NewFile as String
            NewFile="c:\newfile.file"
            Call SHAddToRecentDocs(2,NewFile)

MAKE FORM TRANSPARENT

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()

SetWindowLong Me.hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT

End

Add your tutorials to this site Mail Me

 

Google
  Web www.gauravcreations.com

GauravCreations's Store / Home / Register / Awards / Opinions / Downloads / Query

Linux Hosting India | Linux Forum

Copyright © 2001-04 [Gaurav Creations]. All rights reserved