Precautions for calling Windows API in VB

zhaozj2021-02-08  214

Words of Windows API in VB Zhu Yingxi 01-4-29 11:17:21

Visual Basic (VB) As an efficient programming environment, it encapsulates part of the Windows API function, but also sacrifices some of the features of the API. When you call the API, you may cause an API programming error, which is difficult to capture or intermittent errors, and even the program crashes. To reduce API programming errors, improve the security when VB call API, you should pay attention to the following eight questions:

(1) Specify "Option Explicit"

Before programming, "Require Variable Declaration" in the VB programming environment is selected. If the item is not specified, any simple entry error may generate a "variant" variable, when calling the API, VB Mandatory conversion to this variables to avoid conflicts, so that VB will pass null values ​​for various types such as strings, long integer, integer, floating point number, causing the program to operate normally.

(2) Pay attention to the difference between VB integers and Win32 integers

In the VB environment, all INTEGERs involved are 16 bits, and once there is a 32-bit, read the information related to the Windows API function or 32-bit dynamic link libraries Or the application, especially pay attention to the analysis of the environment background, to determine the API function correctly in the design of the data type and data structure.

(3) Reduce and avoid using As Any

Although using the ANY method declaration library, the Windows API function can be allowed to accept a variety of types of parameters, but more serious is that even a small error, such as a missing type identifier or incorrectly uses the Byval keyword. It may cause system crashes or other data errors that are hard to find.

(4) Pay attention to check parameter type

In the API error, except for errors caused by omissions byval keywords, approximately 50% is because there is an incorrect parameter type in the statement. In the Win32 environment, both 8-bit, 16-bit, or 32-bit numerical variables are passed in 32-bit, and if used, it is difficult to find out. If the declared parameter type is different, VB is treated as a Variant to the API function, and the "Error DLL Call Specification" message will appear.

(5) Do not forget BYVAL to ensure the integrity of the function declaration

BYVAL is "Value" call, when the parameter passes, the pointer to the DLL is not passed to the parameter variable itself, but a copy of the transfer parameter value is passed to the DLL. For example, when passing string parameters, the interface between VB and DLL supports two types of strings. If the BYVAL keyword is not used, VB will pass the function pointer to the DLL to an OLE2.0 string (ie BSTR data type). And the Windows API function often does not support this type of data, resulting in errors. After using the BYVAL keyword, VB converts the string to the "empty termination" string of the C language format and is used correctly by the API.

(6) Recheck the function name

In the Win16 environment, the name of the API function does not require case sensitive, and in the Win32 environment, there is this requirement. When you can't find a function that is not found in a DLL function, it is necessary to check the function name, whether the function of the management string is missing the A and W prefixes.

(7) Pre-initialize the string to avoid conflict

If the API function requires a pointer to the buffer to load data from it, while the string variable is passed, the string length should be initialized first. Because the API cannot know the length of the string - API default has sufficient length. There is no initialization string, the buffer assigned to the string may be insufficient, the API function will be able to rewrite repeatedly at the end of the buffer, and the content behind the memory string will be rewritten. The program is expressed as a sudden end or intermittent error. (8) Track check parameters, return types and return values

VB has an immediate mode and single-step debugging function, using this advantage, ensuring that the type of function declaration is clear (API does not return Variant type), by tracking and checking the source and type of parameters, can exclude error delivery of parameters. Many API functions have a return result, indicating whether it is successful. To test the return result, use the LastDLLLERROR method of the VB's ERR object to check this, call the error, retrieve the results of the API function getLastError, to modify the declaration, to correct the purpose of the API function.

转载请注明原文地址:https://www.9cbs.com/read-2948.html

New Post(0)