Visual Basic API
Results 1 to 6 of 6

Thread: Visual Basic API

  1. #1
    Senior Member
    Join Date
    Jan 2003
    Posts
    220

    Post Visual Basic API

    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.

    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
    Step One - Declaring An API Function or Sub

    An API function must be declared before it is called. The syntax of an API function is like this.

    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.

    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!
    [gloworange]And then it happened... a door opened to a world... rushing through the phone line like heroin through an addict\'s veins, an electronic pulse is sent out, a refuge from the day-to-day incompetencies is sought... a board is found. \"This is it... this is where I belong...\" I know everyone here... even if I\'ve never met them, never talked to them, may never hear from them again... I know you all...[/gloworange]

  2. #2
    Junior Member
    Join Date
    Aug 2002
    Posts
    24

    cool

    that's cool Limpster...thanks,,it makes me got the point of API in vb

  3. #3

    RE: VB API Calls

    I have a huge book on API calls, but is there an online listing of it? At worst I can put all the API calls into a text file for those who do not have access to a reference book, but it may take a while to do.

  4. #4
    Senior Member
    Join Date
    Jan 2003
    Posts
    220
    Yes, there is a program listing all API calls at http://www.allapi.net
    [gloworange]And then it happened... a door opened to a world... rushing through the phone line like heroin through an addict\'s veins, an electronic pulse is sent out, a refuge from the day-to-day incompetencies is sought... a board is found. \"This is it... this is where I belong...\" I know everyone here... even if I\'ve never met them, never talked to them, may never hear from them again... I know you all...[/gloworange]

  5. #5
    Senior Member
    Join Date
    Oct 2003
    Posts
    234
    I have the Professional Edition of Microsoft VB and it comes with a utility to view a text file that contains all the Win32 API constants, function declarations, and types. I can't give you the utility, but Microsoft allows you to distribute the text file. I'm not sure if the Standard Edition includes it or not, so I have attached it. Even if you can't use it to aid your VB programming, it is also helpful if you are doing any windows programming in C.

    P.S. In case the file doesn't show up properly in your browser, save it to your hard drive by right-clicking the link and clicking 'Save Target As'

  6. #6
    Senior Member
    Join Date
    Jan 2003
    Posts
    220
    Nice, Thanks alot!
    [gloworange]And then it happened... a door opened to a world... rushing through the phone line like heroin through an addict\'s veins, an electronic pulse is sent out, a refuge from the day-to-day incompetencies is sought... a board is found. \"This is it... this is where I belong...\" I know everyone here... even if I\'ve never met them, never talked to them, may never hear from them again... I know you all...[/gloworange]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •