Avis Junior Poster

Joined: 07 Oct 2003 Posts: 510 Location: India
|
Posted: Oct 16th, 2003 01:02 AM Post subject: What IS an API call ? |
|
|
What IS an API call ?
API is an acronym for Application Programming Interface, usually referring to the Windows API. The API is the underlying operating system software that Visual Basic hides from you with properties, methods, and events. For example, changing the Text property of a TextBox is functionally equivalent to calling the SendMessage API function with a WM_SETTEXT message. You just don't have to mess with the obscure API function since VB does it for you. However, there are some cases when VB doesn't provide a property or method for functionality that exists in the API. For example, searching for a string in a ListBox. There is no FindString method for a ListBox, but that functionality exists via the SendMessage API function with the LB_FINDSTRING message. Any VB object that has an hWnd property, or handle, can be used with the API. To use an API function, it must first be declared. That is, it must be added to the "list" of valid Visual Basic functions. This is done with the Declare keyword. There are two types of declares - public and private. A Public Declare can only be done in a module (.Bas file) - note that the Public keyword is assumed and can be left off of the declare statement.
| Code: | ' Module1.bas
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long |
If you only need this function in a UserControl or a Form, you need to add the Private keyword in front of the declare:
| Code: | ' Form1
Private Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long |
Note that declare statements must go in the (General)(Declarations) section. Once the function is declared, it becomes "part of" the Visual Basic language and can be used like any other function - with the exception that if you used the Private keyword, it can only be used in the object in which it is declared. Note also that you absolutely must use the exact same casing in theAlias text as the original function. DLL functions are case sensitive and you will receive an error when the program attempts to use a function that it cannot find. To use this API function with the ListBox object to search for a string, you need to add the message that accomplishes the search as well as the code that calls the DLL.
| Code: | Const LB_FINDSTRING = &H18F
Private Sub FindString(aStr As String)
List1.ListIndex = SendMessage(List1.hWnd, LB_FINDSTRING, _
-1, ByVal aStr)
End Sub |
_________________ Code Snippets, Tutorials, Utilities, Controls
Low cost Web Hosting
Hosting starts at as low as $4 per year!
Always follow posting guidelines
Put your VB code in [vb ] your code [ /vb] tags! |
|