What is API
First, it is necessary to explain to you what APIs are. The so-called API was originally written for C and C ++ programmers. An API is a function that is included in a dynamic connection library file attached with a DLL name. In terms of standard definition, APIs are windows 32-bit application programming interfaces and are a series of complex functions, messages, and structures, it allows programmers to compile applications running on Windows 95 and Windows NT operating systems in different programming languages. It can be said that if you have learned VC before, then the API is not a problem for you. However, if you have never learned VC or are not familiar with the Windows 95 architecture, it is very hard to learn API.
If you open the windows system folder, you can find many files with the name DLL attached to them. A dll contains more than one API function, dozens, or even hundreds. Can we all master it? The answer is no: it is impossible to grasp. But in fact, we really don't have to master it all. We only need to master the built-in API functions of the ipvs system. However, you should also discard functions that are repeated with the functions of VB itself. For example, VB
The etattr command of can get the file attributes, and setattr can set the file attributes. There are also corresponding functions for the API.
Getfileattributes and setfileattributes have similar performance. In this case, there are 5 and 600 remaining items. Yes, there are also many. However, I can tell you that as long as you are familiar with 100, your programming level is at least twice higher than today. Although VB is closely related to windows, I think the API is closer
Windows. If you have learned the API, your first achievement is your understanding of the Windows architecture. This harvest is not easy.
What if you don't rely on APIs? I can tell you that most of them are advanced programming books (of course, this is not the name of a book, rather, in the first book content, the book reads those books whose objects are readers with a certain VB basis. The first question raised is generally from API. Therefore, if you do not learn APIs, you will probably stay at the initial level and cannot climb up. The only way may be to ask someone for help: I am dying. Come and help me. What should I do? Bored? Of course, there are too many good people on the Internet (including me), but you should understand that through this channel, you cannot produce good works in your hands. This is because your mind lacks this knowledge. Reagan cannot be an overall design concept.
API text explorer [Return]
Many API functions are very long and long. What do you want to see? The following is an example of the API ddeclienttransaction function:
Declare function ddeclienttransaction lib "USER32" (pdata as byte, byval cbdata as long, byval hconv as long, byval hszitem as long, byval wfmt as long, byval wtype as long, byval dwtimeout as long, pdwresult as long) as long
Wow! So long? If you have never been familiar with APIs, I think you will be scared. You may consider whether to continue learning. But don't worry. Fortunately, Microsoft designers provide us with useful tools. This is the API
Text viewer.
Through the API text viewer, we can easily find the function declaration, structure type, and constant required by the program, copy it to the clipboard, and then paste it into the code segment of the VB program. In most cases, as long as we determine the functions, structures, and constants required by the program, you can add them to the program segment through the above operations on the API text browser, so that these functions can be used in the program. These are the most basic common sense questions for learning APIs, and they do not occupy the huge system content of APIs. Where will we waste our energy in the future? That is:
When to use what function, when to use what structure type, and when to use what constant.
API function declaration [Return]
Let's look back. In VB, how do I declare functions? I think if you are reading this article, you will be able to answer this question. The following is the function declaration you should be familiar:
Function setfocus (byval hwnd as long) as long
That is, this line of code defines a function named setfocus. This function has a long data type parameter and is passed by value (byval). After the function is executed, a long data is returned.
The declaration of API functions is similar. For example, the setfocus function in API is written as follows:
Declare function setfocus lib "USER32" alias "setfocus" (byval hwnd as long) as long
A little complicated. Yes, it's complicated. But I can tell you that the other parts are the same as what you learned before. The same is true for function calls in programs. For example:
Dim DL as long
DL & = setfoucs (form1.hwnd)
However, it is clear. It does not show the running mechanism as you write your own program, nor does it look like VB
The built-in functions can be found from the online help of VB. The only method is to learn and query information other than VB.
The declare statement is used to declare references to external processes in the dynamic link library (DLL) at the module level. You only need to remember that this statement is required for any API function declaration.
IIB indicates the dynamic link library or code resource that contains the declared process or function. That is to say, it indicates the question of where a function or process comes from.
In the preceding example, setfocus lib "USER32" indicates that the setfocus function is from the user32.dll file. The main DLL dynamic connection library files include:
User32.dll for Windows Management. User interfaces for generating and managing applications.
Gdi32.dll graphical device interface. Generate graphical output for Windows devices
Kernel32.dll system service. Access the computer resources of the operating system.
Note: When the DLL file is not in the windows or system folder, you must describe its source in the function (
Path ). For example, setfocus lib "C: \ mydll \ USER32"
Alias in function declaration is optional. Indicates that the called process has another name (alias) in the dynamic link library (DLL ). For example, alias "setfocus" indicates that another name of the setfocus function in user32.dll is,
Setfocus. Why are the two names the same? Of course, it can also be different. In many cases, the function name specified by alias, that is, the last character of the alias is often Character A. For example, another name of the setwindowstext function is
Setwindowstexta, Which is alias "setwindowstexta ". This a is just a naming convention used by designers to indicate that the function belongs to the ANSI version.
So what is the purpose of an alias? Theoretically, an alias provides a function method to call an API with another name. If you specify an alias, though we call this function based on the function following the declare statement, the alias is the first choice in actual function calling. For example, the following two functions (function, ABCD) are valid and 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
Note that when using alias, you should pay attention to the Case sensitivity of the alias. If you do not use alias, the function name must be case sensitive and cannot be changed. Of course, in many cases, the function declaration is directly from the API
Because this error is copied from the text browser, there are very few opportunities for such errors, but you need to know this.
Finally, we remind you that the API Declaration (including the structure and constant) must be placed in the "general declarations" section of the form or module.
Data Type and "type security" [Return]
The data types used in API functions are basically the same as those in VB. However, an integer does not exist in the Win32 API function.
Data type. In addition, the boolean data type cannot be seen in API functions. Variant data types appear in the form of any in API functions, such as data as any. Although its meaning is to allow any parameter type to be passed as a parameter of this API function, this method has some disadvantages. The reason is that this will disable all type checks for the target parameter. This will naturally lead to errors for various types of parameter calls.
To enforce strict type checks and avoid the problems mentioned above, one way is to use the alias technology mentioned above in functions. For example, you can declare the API function getdibits. As follows:
Prototype of the getdibits function:
Public declare function getdibits lib "GDI32" alias "getdibits" (byval ahdc as long, byval hbitmap as long, byval nstartscan as long, byval nnumscans as long, lpbits as any, lpbi as bitmapinfo, byval wusage as long) as long
Transformation of the getdibits function:
Public declare function getdibitslong lib "GDI32" alias "getdibits" (byval ahdc as long, byval hbitmap as long, byval nstartscan as long, byval nnumscans as long, lpbits as long, lpbi as bitmapinfo, byval wusage as long) as long
Based on the previous knowledge in this course, we can know whether the prototype getdibits function or the getdibitslong function is modified. Actually, the original getdibits function specified by alias will be called. However, you should see that the difference between the two is that we force the lpbits parameter to be set to long in the transformed function. This will minimize the chance of errors in function calls. This method is called the "Security type" statement.
Data Types often seen in API functions include long, String, byte, any... (that's all .)
Constant [Return]
There is nothing special about API constants. See the following code in VB:
MSG = msgbox ("hello", vbokcancel)
We know that the value of the constant vbokcancel is equal to 1. The above code can be written in this way without affecting the functions of the Code:
MSG = msgbox ("hello", 1)
But you probably don't want to choose the next one, because it will make it hard to understand the code. This method is also adopted by the API. However, the API constant must be initialized before the event. VB itself cannot be understood. Its content still comes from
Text browser. The specific form is as follows:
Public const abm_activate = & h6
Public const right_ctrl_pressed = & h4
Public const rpc_e_server_died = & h80010007
Private const rpc_s_call_failed_dne = 1727 &
In constant initialization, some programs use global, such as global const abm_activate = & H6, But I think public can replace it completely. I used global in the past, but it is not very useful now. I will use this for a while, and I will use it for a while. The consistency between various programs cannot be maintained. At least it looks awkward.
Structure [Return]
The structure is the statement in C and C ++ languages. In VB, it is generally called a custom data type. Presumably many of my friends already know it. In the API field, I prefer to call it a structure because the various API structure types are not defined by me (
.
In VB, the API structure is also defined by the type... end type statement. For example, in an API, the point structure is defined as follows:
Public type pointapi
Coordinate value of X as long at X coordinate (x coordinate)
Y as long 'coordinate value at Y coordinate (Y coordinate)
End type
For example, the rect structure in API is defined as follows:
Public type rect
Left as long 'x coordinate in the upper left corner of the rectangle
Top as long 'y coordinate in the upper left corner of the rectangle
Right as long 'x coordinate in the lower right corner of the rectangle
Bottom as long 'y coordinate end type in the lower right corner of the rectangle
These contents can also be copied from the API text browser. The variable names in these structures can be modified without affecting the structure itself. That is to say, these member variables are all virtual. For example, the pointapi structure can be changed to the following:
Public type pointapi
The coordinate value of myx as long at the X coordinate (abscissa ).
The coordinate value of myy as long at Y coordinate (Y coordinate ).
End type
However, in general, this is not necessary. The structure itself is a data type. Therefore, you must declare the specific variable as the structure type in use to truly use the structure in the program. The structure declaration method is the same as other data declaration methods. For example, the following statement declares the change mypoint as the pointapi structure type:
Mypoint as pointapi
It is also very simple to reference the member variables in the structure. Add a "." After the structure name, and then write the member variables to be referenced. This is like referencing a property of an object in VB. For example, if we assign the value of the X variable in the mypoint structure declared above to the variable temp &
The Code is as follows:
Temp & = mypoint. x
However, you should never think that the mypoint in the above example is a value. It is not a value, but an address (
Pointer ). Value and address are completely different concepts. The structure must be passed to Windows functions by reference, that is, all APIs
In the function, the structure is passed by byref (in the declare statement, byref is the default type ). Do not try to use byval for Structure Transfer. You will get nothing. Because the structure name is actually a pointer to this structure (the first address of this structure), you can transmit a specific structure name (see the Summary, I highlighted this transfer method in red ).
Since the structure transmits a pointer, the function will directly perform read and write operations on the structure. This feature is suitable for loading the results of function execution in the structure.
Small knot [Return]
The following procedures are provided to summarize the content learned in this lesson. Start VB, create a project, add a command button, copy the following code to the code snippet, and run it.
Private declare function getcursorpos lib "USER32" (lppoint as pointapi) as long
Private type pointapi 'defines the point structure
Coordinate value of X as long at X coordinate (x coordinate)
Y as long 'coordinate value at Y coordinate (Y coordinate)
End type
Sub printcursorpos ()
Dim DL as long
Dim mypoint as pointapi
DL & = getcursorpos (mypoint) 'calls the function to obtain the mouse coordinates on the screen.
Debug. Print "x =" & STR (mypoint. X) & "and" & "Y =" & STR (mypoint. Y)
End sub
Private sub commandementclick ()
Printcursorpos
End sub
Output result:
X = 240 and Y = 151
In the program, the getcursorpos function is used to obtain the position of the mouse pointer on the screen.
In the above example, you can find that the content of the myppint structure passed by the parameter changes substantially after the function call. This is because the structure is passed by byref.