Visit API Function in VB

zhaozj2021-02-17  66

The powerful API (Application Interface) function is a good programming tool for VB (Visual Basic for Windows), but the programmer using the API function may encounter such a phenomenon. After the program is running, the program is running, and the error message dialog box appears. When the system is determined, the system automatically exits the VB integrated environment. If your program has not stored, it regrets that the loss has returned to the sky. The programs after the last store are not existed. This is your general protection failure (GPF) that you use improper use of API functions.

When a GPF is wrong, you should allow Windows to close your app. In some cases you may need to exit Windows or reboot the system. The extent where the memory is destroyed. The type inconsistency in the DLL (Dynamic Link Library) function is incorrectly the main reason for the GPF. These errors can cause GPF to even make the Windows system completely crash (need to reboot the system). Let's talk about some techniques to avoid GPF.

Use an alias to provide a strong type of check is one of the effective measures to avoid GPF. In some cases, the DLL function can accept a variety of types, the loadcursor function is such an example, which is defined as follows:

HCURSOR LOADCURSOR (Hinstance, LpCursorname)

Here HCURSOR is a 16-bit handle that points to the cursor object, Hinstance is a 16-bit instance handle, lpcursorname is the name of the cursor or a 32-bit integer ID of the cursor resource. In order to support two types of LPCursorname parameters. VB is necessary to include the following two statements:

Declare Function LoadCursorlib "User" (Byval LpCursorname As String) AS Integer and

Declare function loadcursor lib "user" (Byval Hinstance As INTEGER, BYVAL LPCURSORNAME AS Long) AS Integer

However, these two declarations cannot exist in one program because Visual Basic will report repeatedly. We know, as an aification can make any parameters can be passed to the DLL function, so you can declare the following:

Declare function loadcursor lib "user" (byval lpcursorname as any) AS integer

The above statement means that Visual Basic can support a variety of DLL functions that can bring a variety of catastrophic consequences. Whenever the function is invoked by incorrect parameters, it can be triggered. A GPF, we can perform rigorous types of types and help prevent such problems.

This method is to use Alias ​​tips in the statement of functions to see the following two statements:

Declare function loadcursorbyname lib "user" alias "loadingcursor" (Byval LpCursorname As String) AS Integer and

Declare Function LoadCursorByID Lib "USER" Alias ​​"LoadCursor" (Byval hInstance As Integer, ByVal lpCursorName As Long) As IntegerLoadCursorByName do lpCursorName string parameter access DLL function LoadCursor, and access the same DLL function LoadCursorByID LoadCursor, but to do with the long integer LPCURSorname parameters, these two functions are strictly checked for the LPCursorname parameters, so that Visual Basic can identify incorrect variable types before calling the DLL function, minimizing the opportunity to cause GPF or cause system crash.

In addition, it is best to use the API function before running, and carefully check the parameters of the API function and whether the type of declaration is consistent, and whether the strict checking parameters can effectively reduce the GPF or system crash.

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

New Post(0)