Search Tools Links Login

The Beginners Guide To API


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!

Original Author: Dave Greenwood

Code

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


Privatesize="2"> Declaresize="2"> Functionsize="2"> ExitWindowsEx size="2">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


----------------


Privatesize="2"> Declaresize="2"> Functionsize="2"> ExitWindowsEx size="2">Lib "user32"
(ByVal uFlags As Long, ByVal dwReserved As Long) As Long


Now to Explain what the above means


Privatesize="2"> Declaresize="2"> Functionsize="2"> ExitWindowsEx tells VB to Declare a Private Function
called "ExitWindowsEx".


The size="2">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 WAVEFILE (WAV)


Declare color="#000080" size="2">Function
sndPlaySound
Libsize="2"> "winmm.dll" size="2">Alias "sndPlaySoundA"
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long


Public color="#000080" size="2">Const SND_SYNC =
&H0


  Public color="#000080">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:windowskerchunk.wav"


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

End sub


CHANGE WALLPAPER


Declare color="#000080" size="2">Function
SystemParametersInfo
Libsize="2"> "user32" size="2">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 color="#000080">String
strBitmapImage = "c:windowsstraw.bmp"
x = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, strBitmapImage, 0)

End sub


ADD FILE TO DOCUMENTS OF WINDOWS MENU BAR


Declare color="#000080" size="2">Sub
SHAddToRecentDocs
Libsize="2"> "shell32.dll" (ByVal uFlags As Long, ByVal pv
As String)


Dim NewFile as color="#000080">String
NewFile="c: ewfile.file"
Call SHAddToRecentDocs(2,NewFile)

MAKE FORM TRANSPARENT


Declare Function SetWindowLong color="#000080">Lib "user32" color="#000080">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


Any Problems email me at href="mailto:DSG@hotbot.com">DSG@hotbot.comsize="2">


About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 90 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.