How to declare and Use API functions in VB

Source: Internet
Author: User

1. There are two methods to declare an API function in VB: if we only use an API function in a form, we can declare it in the general part of the form code:

The declared syntax is:
Private declare function...
Private declare sub .....
Private Declaration must be used here, because this API function can only be called by programs in a form.

If our program has multiple forms, and we need to use the same API function in multiple forms, we need to declare it in the module.
Add a module (Display) first ),
Then use the following syntax statement:
Public declare function ....
Public declare sub ....
The meaning of the public declaration is to use an API function as a public function or process and call it directly at any position (including all forms and modules) in a project. After the Declaration, we can use this API function in the program.

2. You can use API functions in the following ways. Take the setwindowpos function as an example:
(1) Call to ignore the function return value:
Setwindowpos form1.hwnd,-2, 0, 0, 0, 0, 3
Note that function parameters are not enclosed in parentheses.
(2) Call method call:
Call setwindowpos (form1.hwnd,-2, 0, 0, 0, 3)
Note that parentheses need to be added here, but we will not retrieve the return value of the function.
(3) Call to obtain the function return value:
Mylng = setwindowpos (form1.hwnd,-2, 0, 0, 0, 3)
In this case, brackets must be added, and a variable must be defined in advance (the type of the variable is the same as the type of the function return value) to store the return value of the API function.

Iii. Explanation of several problems:

(1) How is the lib and alias statements going on?
Generally, WIN32API functions are always included in the dynamic Connection Library DLL provided by windows or other companies. The LIB keyword in the declare statement is used to specify the DLL (dynamic connection library) file path, so that VB can find the DLL file and then use the API function. If we only list the DLL file name without specifying its complete path, VB will automatically go. find the DLL file in the directory where the EXE file is located, the current working directory, the WINDOWS \ SYSTEM directory, and the Windows directory. Therefore, if the DLL file to be used is not in the above directories, we should specify its full path.
Alias is used to specify the alias of an API function. If the called API function uses a string (the parameter contains the string type), the alias keyword is required. This is because the names of the same API function in the ANSI and Unicode character sets may be different. To ensure that no declaration error occurs, we use the alias keyword to indicate the alias of the API function, in general, on the Win9x Platform, we can add an uppercase A after the API function name as an alias.

(2) Description of common API parameter types
The most common parameters of an API function are long and long data types. For example, the handle of an API, some specific constants, and the return values of a function are values of this type; other common parameter types include integer, byte, and string.

(3) What is the use of byval in the declaration?
This is related to the parameter transfer method of VB, by default, VB transmits function parameters through address transmission, while some API functions require passing value (these two parameters are different, the former transmits a pointer, while the latter requires the true value of the parameter ). In this case, an error occurs. The solution is to add the byval keyword before the API function parameter declaration, so that VB uses the value passing method to pass the parameter.

(4) how to obtain the complete API function declaration
VB comes with the API text viewer. You can find the complete declaration of the API function and paste it into the program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.