Important alert: (current site time 7/16/2013 3:35:48 AM EDT)
 

article

The Beginners Guide To API

Email
Submitted on: 7/5/2000 1:15:13 PM
By: Dave Greenwood 
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: 50571
(About the author)
 
     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!

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				

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.

PLAY A WAVEFILE (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

Any Problems email me at DSG@hotbot.com


Other 6 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

7/5/2000 1:43:20 PMMoto Stud

This tutorial rocks. Easily the best tutorial i've seen.
(If this comment was disrespectful, please report it.)

 
7/5/2000 11:50:47 PMstealth

very good tutorial
(If this comment was disrespectful, please report it.)

 
7/7/2000 11:58:10 AMM@

Good work. It is nice to see tutorials that don't jump straight over the reader's heads. Very well written. Keep up the good work!
(If this comment was disrespectful, please report it.)

 
7/27/2000 8:46:17 AMMat

Thank you, David
(If this comment was disrespectful, please report it.)

 
8/4/2000 2:18:51 PMCygnus

I have several books that I use for API calls. Two of which are suppose to be beginners guides and one is suppose to be the "all knowing" API reference. It's funny because with each reference I usually have to struggle because the authors do not take a common sense approach in their thought process to let you know fundamentally what the heck the code your using is doing and why.

In each case the authors neglect to interject the simple common sence approach you have in your tutorial.

Good Job
(If this comment was disrespectful, please report it.)

 
8/8/2000 6:07:36 PMMarc

where did you pick the (15,0) of Exitwindows(15,0) ???
(If this comment was disrespectful, please report it.)

 
8/9/2000 5:49:12 PMGreg Webster

Hello, I was just wondering if anyone could sugest a vb book on api for me?
thanks
(If this comment was disrespectful, please report it.)

 
8/10/2000 11:39:00 AMopello

do a search at amazon.com for "Dan Appleman" (he is an author, and has written several excellent books).
(If this comment was disrespectful, please report it.)

 
8/13/2000 1:32:08 AMRajaraman

Quite good. I see your effort to make others understand what it all means. keep it up.
(If this comment was disrespectful, please report it.)

 
8/13/2000 7:51:28 AMSTKO

Very helpful, by the way from where I can get the full details of Windows known dlls? (on the web)
(If this comment was disrespectful, please report it.)

 
8/13/2000 5:24:26 PMRoger Willcocks

There is also a program available called API Guide which has 500+ API declares, each with examples, listed by category, etc. I've found it really good. Just do a search for it at http://www.codehound.com/
(If this comment was disrespectful, please report it.)

 
8/16/2000 6:32:37 PMJotaf98

This may not sound good because I haven't read your article yet, but I have this dll that I know that has at least 1 function to extract files from an MPQ archive (it's name is Storm.dll and it comes with Starcraft; it's a compression technology like zip but it's a lot faster, and since the author says that anyone can use it, I'd like to use it in my game too). I have another program that is a manager for these archives, so all I need is that specific API, nothing more. Is it possible to get it? I saw a VB program called MPQ Viewer using it (made by Simply Red Software, e-mail smims@hotmail.com), but I can't seem to find a web page and no one replies to my e-mails, so I can't figure out how to do it!
(If this comment was disrespectful, please report it.)

 
8/18/2000 7:59:14 AMsandy

Thanks for the helpful hints, but one thing is missing - where do you get the values for the Public Constants that you declare?
(If this comment was disrespectful, please report it.)

 
9/11/2000 11:02:29 PMnsr

gee...tahnx..
(If this comment was disrespectful, please report it.)

 
11/25/2000 12:37:02 PMgaurav

I have read your tutorial at planetsourcecode.com
It has been very nicely written
May I please request your permission to have it put up on my site www.gauravcreations.com with full credits to you ofcourse


(If this comment was disrespectful, please report it.)

 
6/10/2001 8:52:27 PMcmor34

This article doesn't apply to VBScript or ASP, as indicated in section 'Compatibility'. You can't declare API calls in VBScript or ASP.
(If this comment was disrespectful, please report it.)

 
7/28/2001 11:23:23 AM§inister Minister

Hey thx, even i understud it :)
(If this comment was disrespectful, please report it.)

 
9/5/2001 6:01:47 PMTSmoov

i am writing a program in VB6, where in the application you have a form, and in that form you have text fields. I am trying to write a program that will let me email or FTP these fields to a web server or someones email account. if anyone has any ideas please email me at bigboi68@yahoo.com Thanks
(If this comment was disrespectful, please report it.)

 
10/14/2001 9:23:43 AMWengWasHere

Just want to ask something about FindWindow...how will you know the classname and the WindowName if you're just a beginner...say if you want to find the handle of a Notepad? any help please?
(If this comment was disrespectful, please report it.)

 
11/26/2001 9:49:56 PMHari

It's very helpful, It's great.
(If this comment was disrespectful, please report it.)

 
2/6/2002 7:48:16 AMScott

Total Beginner to API
Where did the 15 come from for the exit windows API, and whats the deal with the public constants, whats wFlags% = SND_ASYNC Or SND_NODEFAULT!?
Not Beginner enuf for me....
(If this comment was disrespectful, please report it.)

 
3/4/2002 9:52:23 AMPhish

WengWasHere,

To Find the Window's Class or Handle you would need to use an API Spy. Which is a program that gives you a windows Feilds. I suggest Do§ API Spy.
(If this comment was disrespectful, please report it.)

 
4/30/2002 9:24:40 PMTheOne

the sound tutorial dosent work!!!
it says variable not defined, then it
highlights:
SoundName$ = "C:\ATI\COPHIT.WAV"
????????????????
GRRRRRRRRRR
(If this comment was disrespectful, please report it.)

 
5/17/2002 11:45:13 PMhexsmit

An error keeps popping up taht says that constants are not allowed as public members of object modules
someone please tell me what im doing wrong

(If this comment was disrespectful, please report it.)

 
10/3/2003 6:50:12 PMJatopian

How do u know that a certain function is in a DLL? Do u just have to GUESS?!
(If this comment was disrespectful, please report it.)

 
11/18/2003 9:27:55 AM

how do you know the return value for
the shut down api is (15, 0)
(If this comment was disrespectful, please report it.)

 
6/24/2005 4:25:13 AM

Hi I am really wanting to learn to program. I have decided to choose VB to do this. Does anyone know where I can learn the very very basics of VB.
(If this comment was disrespectful, please report it.)

 
6/24/2005 4:30:53 AM

Hi I am really interseted in learning to program. I have chosen VB to do this but I need to learn obviously. Does anyone know what site I can go to to learn the fundemental basics.
(If this comment was disrespectful, please report it.)

 

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.