Win32API lecture 1

xiaoxiao2021-03-06  39

Lesson 1: Know the API

First, what is the API

First of all, it is necessary to tell you, what is the API. The so-called API is originally written for C and C programmers. The API said that it is a function that they are included in a dynamic connection library file attached to a DLL. In terms of standard definitions, the API is Windows 32-bit application programming interface. It is a very complex function, message and structure that makes programmers to run in Windows95 and Windows NT with different types of programming languages. Applications on the operating system. It can be said that if you have learned VC, then the API is not a problem for you. But if you have not studied VC, or you are not familiar with Windows95 structure system, then you can say that learning API will be a hard thing.

If you open the Windows System folder, you can find a lot of files named DLL. The API function contained in a DLL is not just one, dozens, or even hundreds. Can we also master it? The answer is negative: it is impossible to master. But in fact, we really don't have to master it, just grasp the API function you bring on the Windos system itself. However, a function of repetition of functions from the VB itself should also be thrown away. For example, VB's etattr command can get file properties, and setttr can set file properties. For the API, there are also corresponding functions GetFileAttributes and SetFileAttributes, and performance is similar. In this way, there is 5,600 remaining. Yes, there are a lot. However, I can dare to tell you that as long as you are familiar with 100, then your programming level is at least twice more than now. Although people say VB and Windows have close relationship, I think the API is closer to Windows. If you learn the API, the primary gain is aware of the WINDOWS architecture. This gain is from it is not easy.

What if you don't rely on the API? I can tell you, mostly the advanced programming book (of course, this is not the name of the book is advanced and advanced, but at the beginning "book content" indicated "this book" The reading object of the book is those who have a certain VB based reader). The first question is generally from the API. Therefore, you can say that you don't learn API, you will probably stay in the primary level, can't climb it up. The only way may be to save others: I am dead, come to save me, how to do this, what should I do? Of course, now I am very online, there are too many (including me, 嘻嘻) But you should understand that through this way, your hands can't make good works. This is because the lack of these knowledge your brain is not a general design concept.

Second, the API text.

Many API functions are very long. Want to see what it looks like? As follows 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 exposed to the API, I think you will definitely be scared. You may think that should not continue to learn. But don't worry, lucky is Microsoft's designers provide us with useful tools, which is the API text viewer.

With the API text viewer, we can easily find the function declarations, structural types, and constants required by the program, and then copy it to the clipboard, and finally paste the code segment of the VB program. In most cases, as long as we determine the three aspects of the function, structure and constant needed by the program, you can join them into the block by the above operations of the API text! Function. These are the most basic common sense of learning API, which is far from the huge system content of the API. In the future, we will waste energy (this is never waste)? That is: When is the function when you use, when you use what structure type, what constant is used. Third, the API function declaration.

Let us recall. 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), this line code defines a function called setFocus, this function has a parameter of a long-type data type, and passed by value ( BYVAL), the function will return a long-type data. 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" 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 can see the operation mechanism inside yourself, and it is not like the function of VB, which can be found in the online help from 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. Generate a graphic output of a Windows device 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 its source (path) in the function. As, Alias ​​in the SetFocus Lib "C: / MYDLL / User32" 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 in user32.dll is 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, which is represented 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) (Byval HWnd As long) (Byval Hwnd As long) (Byval Hwnd As Long) USER32 "Alias" setfocus "(Byval Hwnd As long) As long

It should be noted that when choosing Alias, pay attention to the case of the alias; if you do not choose Alias, the function name must pay attention to case, and cannot be changed. Of course, in many cases, this error occurs if the function declaration is copied directly from the API text visiter, so this error is very small, 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.

(Hangzhou Yuan Shuai Note: API declaration in the form must private Declare function ...)

Fourth, data type and "type security"

The data type used in the API function is basically the same as in VB. However, as a Win32's API function, there is no Integer data type. Another point is that the Boolean data type can be seen in the API function. The Variant data type is in the ANY function in the ANY function, such as Data as ANY. Although its meaning is to pass the parameters of any parameter as a parameter of the API function, there is a certain disadvantage. The reason is that this will make all types of checks to the target parameters will be turned off. This naturally brings opportunities to generate errors to various types of parameters.

In order to enforce strict type checks, and avoid the above mentioned issues, one way is to use the above mentioned to Alias ​​technology in the function. Another declaration method can be made if GetDibits for the API function. :GetDIBits following function prototype :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) modified :Public Declare As LongGetDIBits function 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 learned in the previous course, we can understand the prototype GetDibits function, the modified getDibitslong function is also good, and actually call the GetDibits original function specified by Alias. But you should see that the difference between the two is that we enforce the representation of the LPBITS parameter as a long shape in the modified function. This will make the error unit that occurred in the function call to minimize. This method is called "safe type" declaration.

The data types that are often seen in the API function are :long, string, byte, any .... (also these.)

Five, constant

For API constants, there is nothing to learn too much. Please see the following code in VB: msg = msgbox ("Hello", VBOKCANCEL) We know, VBOKCANCEL This constant is equal to 1. For the above code we can write this, without affecting the functionality of the code: MSG = msgbox ("Hello", 1), but you probably not willing to choose the latter, because this will make the code costs. This method is also taken by the API. Just the API constant must be done before things, the vb itself can't understand. Its content is still from the API text. Specific forms are as follows, etc.:

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 & initialization constants, some programs use Global, such as Global Const ABM_ACTIVATE = & H6, but I think the Public can replace it. In the past, I also used Global, but it is not used now. Use this for a while, with that, the programs cannot be consistent, and the at least looks away.

Sixth, structure

The structure is the statement of C and C languages. The custom data type is generally referred to in VB. Many friends have already known it. In the field of API, I prefer to call it structure because the API variety of structural types are not I defined (custom). In VB, the API structure is also defined by Type ....... End Type statement. For example, in the API, the definition method of point (POINT) is as follows: Public Type PointAPI X as long 'points on the coordinate value on the X coordinate (horizontal coordinates) y as long' points on the coordinate on Y coordinate (ordinate) The value end type is, as defined in the API (RECT) structure is defined as follows: Public Type Rect Left as long 'of the X coordinate TOP as long' in the upper left corner of rectangular Right AS long 'in the upper left corner of Right As long' rectangular X coordinate Bottom as long 'in the lower right corner of the lower right corner

End Type These content can also be copied from the API text visiter. The variable names in these structures can be changed casually without affecting the structure itself. That is, these member variables are virtual. For example, the POINTAPI structure can be changed to the following: Public Type PointApi MYX AS Long 'Point Coordinate Value on X Coordinate (Coordinate) MYY As LONG' Coordinate Values ​​on Y Coordinate (WLD) End Type, General Tell, there is no such necessary. The structure itself is a type of data, so the specific variable must be declared as this structure when used, in order to be truly used in the program. Structure declaration methods and other data declaration methods, such as the following statement, declare the MYPOINT as a POINTAPI structure type:

The member variable in the MyPoint As PointApi reference structure is also very simple, plus one "." After the structure name, then follow the members variables to be referenced. This is very like a reference to an object of an object in VB. For example, if we assume the value of the X variable in the MyPoint structure that has been declared to the variable Temp & the code is as follows: Temp & = MyPoint.x But special attention is that you must not think that MYPOINT in the above example is a value. . It is not a value, but the address (pointer). The value and address are completely different concepts. Structure requirements are passed to the Windows function by reference, that is, all API functions, the structure is passed by byref (byref is the default) in the Declare statement). For the conversion of the structure, you don't try to use byval, you will not be gone. Since the structural name is actually pointing to this structure (the first address of this structure), you can also send a specific structure name (see Summary, I highlight this transmission method).

Since the structure is transmitted is a pointer, the function will directly read and write the structure. This feature is well suited to load the results executed by the function in the structure.

Seven, small knot

The following procedures are given to summarize the contents of this lesson. Start VB, create a new project, add a command button, and copy the following code to the code segment, run it.

PRIVATE DECLARE FUNCTION GETCURSORPOS LIB "User32" (LPPOINT AS POINTAPI) AS Long Private Type PointApi 'Definition Point (Point) Structure X As Long' Point Coordinate Value on X Coordinate (Coordinate) Y AS Long 'Point in Y Coordinate ( Coordinate Value End TypeSub PrintCursorpos () DIM DL AS Long Dim MYPOINT AS POINTAPI DL & = GetCursorpos (MyPoint) 'Call function, get the screen mouse coordinate debug.print "x =" & str (mypoint.x) & " AND "&" y = "& str (mypoint.y) End subsprivate submanmand1_click () PrintCursorpos

End Sub

The output result is (each run may get different results, this is determined by the function call when the function is called, and the position of the mouse pointer is located in the screen) :X = 240 and y = 151

In the program, the getCursorpos function is used to get the position of the mouse pointer on the screen.

In the above example, you can find that a substantial change occurs after the function calls of the Myppint structure in parameters. This is because the structure is caused by byref.

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

New Post(0)