Struct, struct Member
In the C language world, a program is composed of a struct plus algorithm. Therefore, the struct type is indispensable in the interaction with Win32 APIs. Classes defined in Python cannot be directly used in C functions. They must be inherited by the structure classes in ctypes and filled in fields according to their format. The following example defines the simplest struct that represents the coordinate struct of a point on the screen, as shown below:
# Windows application # developer: Cai junsheng (QQ: 9073204) 2014-8-14from ctypes import * class POINT (Structure): _ fields _ = [("x", c_int ), ("y", c_int)] # point = POINT (10, 20) print (point. x, point. y) point = POINT (y = 5) print (point. x, point. y)
In this example, a class POINT is defined to inherit the Structure class in the ctypes library, and the member list _ fields _ in the class POINT is assigned a value, each struct member is represented by a tuples of two elements ("x", c_int )). The first field of the tuples indicates the name of the field, and the second field indicates the type of the field. In this way, we can describe the struct defined in C language. You can also use the defined struct In the struct, as shown in the following example:
class FILETIME(ctypes.Structure): _fields_ = [("dwLowDateTime", DWORD), ("dwHighDateTime", DWORD)]...class WIN32_FIND_DATAA(ctypes.Structure): _fields_ = [("dwFileAttributes", DWORD), ("ftCreationTime", FILETIME), ("ftLastAccessTime", FILETIME), ("ftLastWriteTime", FILETIME), ("nFileSizeHigh", DWORD), ("nFileSizeLow", DWORD), ("dwReserved0", DWORD), ("dwReserved1", DWORD), ("cFileName", CHAR * MAX_PATH), ("cAlternateFileName", CHAR * 14)]
In the WIN32_FIND_DATAA struct, the custom struct FILETIME is referenced as the type declaration. Define the struct for data exchange with the C interface in Python. The most special thing is to inherit the class ctypes. Structure. Otherwise, you must remember this point.
C language struct Definition
The second struct student defines a student struct.
The first is to use typedef to redefine the struct type name of struct student as student. That is to say, struct student and student indicate that the same thing is a type identifier, such as typedef int zhengshu; that is, you rename the integer int to zhengshu. The following definitions: int I; and zhengshu I; are equivalent.
C language struct Definition
The second struct student defines a student struct.
The first is to use typedef to redefine the struct type name of struct student as student. That is to say, struct student and student indicate that the same thing is a type identifier, such as typedef int zhengshu; that is, you rename the integer int to zhengshu. The following definitions: int I; and zhengshu I; are equivalent.