API is the most advanced part of Programming in VB. To most people its hard to understand. Basically API is just functions that are inside .DLL(Dynamic Link Libraries) that are shipped with windows. API is also widely used in C/C++. To execute an API function you must do 3 things.
Step One - Declaring An API Function or SubQuote:
1. Delcare the API function or sub
2. Declare Any constants relating to that API call
3. Call API function/sub and fill in required parameters
An API function must be declared before it is called. The syntax of an API function is like this.
Quote:
Declare Function ExitWindowsEx Lib "user32" Alias "ExitWindowsEx" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
In the case of this declartion. The API is a function, that when called, will shutdown windows.
Parts of the Declaration
Declare Function
This is used to tell VB that a function is being declared. Note that this may also be "Declare Sub" depending on the API call your using. This is a function because it returns a True or False Value (1 or 0) used to check if the action was completed sucsessfully or not. It would need to be a sub if it was returning data such as a Window handle.
=======================================
ExitWindowsEx
This is the name of the API call being used. This tells the system which function to pull out of the DLL file.
=======================================
Lib "user32"
This is the .DLL file that the function resides in. In this example, ExitWindowsEx Is located in user32.dll. The reason the .dll is not included in this declartion is because this is a well known DLL. Other such DLL's are "kernel32.dll" and "gdi32.dll." Usually the full file name is included. Note that you must also supply the full filename if its not located in the system PATH.
=======================================
Alias "ExitWindowsEx"
This is just another name you can refer to the function by incase it becomes incompatible, which some may. Not all functions or subs have alias'
The following text located inside of the parentheses are called parameters. These are the data you feed to the function or sub to help it execute properly.
=======================================
ByVal uFlags As Long
Most varibales in VB are passes by what is called reference. This means that the value of the varible can be changed. The word ByVal here means that the varible is passed By Value, or that a copy of it is passed and not the original. This is so that the value of the original varible cannot be changed. You will find that most parameters in VB API calls are passed "ByVal." uFlags is the name of the parameter to be passed. This is what is called a constant. All constants must be declared before they are used. To shutdown windows you would use the constant "EWX_SHUTDOWN". To declare it you would use this statement shown in step two.
=======================================
ByVal dwReserved As Long
This is just a reserved value, it will always equal 0.
=======================================
This final "As Long" Is just what type of data the function will return.
++++++++++++++++++++++++++++++++++++++++++++++++
Step Two - Declaring Constant
The only constant found in this API call is EWX_SHUTDOWN. It is declared like this.
Const EWX_SHUTDOWN = 1
The value of EWX_SHUTDOWN is 1. But you would plug in the constant name, not the value. An example of the finished API call is shown below. "As long" is just what type the constant value must be.
++++++++++++++++++++++++++++++++++++++++++++++++
Step Three - Call API function/sub and fill in required parameters
The final step is to execute the API function. To call API functions you just type the name followed by the completed parameters. Here is an example.
++++++++++++++++++++++++++++++++++++++++++++++++Quote:
ExitWindowsEx(EWX_SHUTDOWN, 0)
Here is the API Function ExitWindowsEx in use. When called, the computer will shutdown.
Note: Becareful when using Any API. Sometimes they may cause unexpected and undesirible results.
'Declare your constant
Const EWX_SHUTDOWN = 1
'Declare the API function
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
'Begin Form Load Procedure
Private Sub Form_Load()
'Call the API function
ExitWindowsEx(EWX_SHUTDOWN, 0)
End Sub
++++++++++++++++++++++++++++++++++++++++++++++++
I hope this helped you to understand how API works. I know I had A hard time with it at first but its really easy once you work with it. Its near impossible to memorize every API function and sub so here are some tools I reccomend that can help. The first one, APIGUIDE is extremley helpful. It lists most API calls and has explantions and examples. The second, APIViewer lists most of the constants and API calls. Get the tools here http://www.allapi.net
Good luck!