article,teaches,basics,Windows,APIby,giving,w
Quick Search for:  in language:    
article,teaches,basics,Windows,APIby,giving,w
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
RentACoder Stats

 Code:  lines
 Jobs: 0 postings

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for RentACoder.
Wrapping Scrolling Text
By Paranoid_Androi d on 7/2


Create A Dummy File
By AML on 7/2


Click here to see a screenshot of this code!Captionbar manipulation!
By Peter Hebels on 7/2

(Screen Shot)

A Game Of War
By Co0nest on 7/2


Click here to see a screenshot of this code!KeyGen Example
By Bengie|NET on 7/2

(Screen Shot)

Click here to see a screenshot of this code!OpenBrowser v1.9
By Orlando Jerez on 7/2

(Screen Shot)

SendMessageBySt ring() Example
By Jaime Muscatelli on 7/2


Click here to see a screenshot of this code!FirstSunday
By Jan Paul Penning on 7/2

(Screen Shot)

Click here to see a screenshot of this code!Ikonz v1.0
By Gaurav Creations on 7/2

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

The Beginners Guide To API

Print
Email
 

Submitted on: 7/5/2000 1:15:13 PM
By: Dave Greenwood 
Level: Beginner
User Rating: By 27 Users
Compatibility:

Users have accessed this article 26725 times.
 
(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 langauges 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 notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
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 PM:Moto Stud
This tutorial rocks. Easily the best tutorial i've seen.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/5/2000 11:50:47 PM:stealth
very good tutorial
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/7/2000 11:58:10 AM:M@
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!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/27/2000 8:46:17 AM:Mat
Thank you, David
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/4/2000 2:18:51 PM:Cygnus
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/8/2000 6:07:36 PM:Marc
where did you pick the (15,0) of Exitwindows(15,0) ???
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/9/2000 5:49:12 PM:Greg Webster
Hello, I was just wondering if anyone could sugest a vb book on api for me? thanks
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/10/2000 11:39:00 AM:opello
do a search at amazon.com for "Dan Appleman" (he is an author, and has written several excellent books).
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/13/2000 1:32:08 AM:Rajaraman
Quite good. I see your effort to make others understand what it all means. keep it up.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/13/2000 7:51:28 AM:STKO
Very helpful, by the way from where I can get the full details of Windows known dlls? (on the web)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/13/2000 5:24:26 PM:Roger 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/
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/16/2000 6:32:37 PM:Jotaf98
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!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/18/2000 7:59:14 AM:sandy
Thanks for the helpful hints, but one thing is missing - where do you get the values for the Public Constants that you declare?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/11/2000 11:02:29 PM:nsr
gee...tahnx..
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/25/2000 12:37:02 PM:gaurav
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/10/2001 8:52:27 PM:cmor34
This article doesn't apply to VBScript or ASP, as indicated in section 'Compatibility'. You can't declare API calls in VBScript or ASP.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/28/2001 11:23:23 AM:§inister Minister
Hey thx, even i understud it :)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/5/2001 6:01:47 PM:TSmoov
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/14/2001 9:23:43 AM:WengWasHere
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?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/26/2001 9:49:56 PM:Hari
It's very helpful, It's great.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/6/2002 7:48:16 AM:Scott
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....
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/4/2002 9:52:23 AM:Phish
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.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/30/2002 9:24:40 PM:TheOne
the sound tutorial dosent work!!! it says variable not defined, then it highlights: SoundName$ = "C:\ATI\COPHIT.WAV" ???????????????? G RRRRRRRRRR
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/17/2002 11:45:13 PM:hexsmit
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | RentACoder Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.