API function declaration

xiaoxiao2021-03-06  52

In VB, how to declare a function? I think, if you are watching this article, then you can absolutely answer this question. The following is a function that you should be familiar with:

Function setfocus (byval hwnd as long) As long

That is, this line code defines a function called setFocus. This function has a parameter of a long-type data type, and is passed (byval), and a function is returned after the function is executed.

The declaration of the API function is also very similar. For example, the setfocus function in the API is written this:

Declare function setfocus lib "user32" alias "setfocus" (Byval HWnd As Long) As long

It is a bit complicated. Yes, it is complex. But I can tell you that in addition to these parts, other parts are still the same as what you have learned before. The function of the function is also the same. Such as:

DIM DL AS Long

DL & = setFoucs (Form1.hwnd)

But it is clear. It is not like the procedure you wrote to see the operation mechanism inside, nor like VB

As with the functions of the combo, you can find it from the online help of VB. The only way is to learn, check the information other than VB.

The DECLARE statement is used to declare references to external processes in the dynamic link library (DLL) in the module level. In this regard, you can write this statement when you remember any API function declaration.

The IIB indicates a dynamic link or code resource containing the declared process or function. That is to say, it explains that the function or process from the problem.

As in the above example, the setFocus lib "user32" description function setfocus comes from the user32.dll file. The main DLL dynamic connection library files are:

User32.dll Windows Management. Generate and manage user interfaces for applications.

GDI32.DLL graphics device interface. Graphic output from Windows devices

Kernel32.dll system service. Access the computer resources of the operating system.

Note that when the DLL file is not in the Windows or System folder, you must explain it in the function (

path). Such as setfocus lib "c: / mydll / user32"

Alias ​​in the function declaration is optional. Indicates that there is another name (alias) that will be called in the dynamic link library (DLL). For example, Alias ​​"setfocus", the other name of the setFocus function is in user32.dll,

Setfocus. How can two names are the same? Of course, it can be different. In many cases, Alias ​​descriptions of the function name, that is, the last character of the alias is often character a, such as the other name of the setWindowsText function is

Setwindowstexta, is expressed as Alias ​​"setWindowstexta". This A is just a naming convention for the habit of designers, indicating that the function belongs to the ANSI version.

So, what is the use of alias? In theory, the alias provides a function method that uses another name to call the API. If you specify an alias, even though we call this function by pressing the function behind the DECLARE statement, but in the actual call of the function, it is the primary selection with an alias. For example, the following two functions (Function, ABCD) declarations are valid, they call the same setfocus function:

Declare Function setfocus lib "user32" "setfocus" (Byval Hwnd As Long) As long

Declare abcd setfocus lib "user32" Alias ​​"setfocus" (Byval HWnd As long) AS Long Need to note that when you choose Alias, you should pay attention to the case of the alias; if you don't choose Alias, the function name must pay attention to the case. And you can't change it. Of course, in many cases, since the function declaration is directly from the API

Copying in a text visiter, so this error has a lot of opportunities, but you need to know this.

Finally, remind you that the API declaration (including structure, constant) must be placed in the "General Declarations) section of the form or module.

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

New Post(0)