Python wants to invoke the C - language Dynamic Connection library, not only to be compatible with the C interface's calling habits, but also to be compatible with the C language data type. Fortunately , the ctypes Library has done both of these things, so it is very convenient to call the dynamic Connection library. In the Hello World Program, this line of code is written as follows:
MessageBox = Windll.user32.MessageBoxW
From the simplicity of this line of code, it is very graceful. This beauty is due tocTYPESThe library has done a lot of work behind the scenes, such asWindllis actually a more complex object. In thecTYPESLibrary, it provides three objects that are easy to load in a dynamic connection library:Cdll,Windlland theOledll. By accessing the properties of these three objects, you can invoke the function of the dynamic Connection library. WhichCdllprimarily used to loadClanguage Invocation Method (cdecl),Windllprimarily used to loadWIN32Invocation Method (stdcall), whileOledllUseWIN32Invocation Method (stdcall) and the return value isWindowsback in theHRESULTvalues. If you haven't studied programming before, there's no way to differentiatecdecland thestdcall, even if you have learned to program, you may not know if you have not written a call across different libraries. Because at the momentIDEin the development environment, all of these details have been hidden. But when it comes to cross-lingual calls, you can't ignore this detail. Then you may ask why these two call methods are not the same dynamic connection library? It's a good question to ask. To answer this question, you have to inventClanguage at that time. In the -age, Americans Dennis Ritchie inventedClanguage, and useClanguage WritingUNIX, and thus he becameCthe father of language andUNIXthe parent of the operating system. BecauseUNIXThe operating system is very efficient and easy to modify, thanks to the use ofClanguage to write. WithUNIXthe promotion of the operating system,Clanguage has become a popular language. Want to makeUNIXbecome efficient, thenCIn the design of language, we should focus on efficient design. This is reflected in the design of the function call. In theCwhen calling a function of a language, you need to transfer multiple parameters. The transfer of these parameters can be transmitted via registers or stacks. Then you might ask why not just use registers as a way of doing it? Because the parameters of a function call are much more, such as reaching5A. And at that timeCPUThe register is very small and does not meet this requirement. Not like the currentARMorMIPSof theCPU, more registers, up to -a lot of them. It is basically possible to solve the problem by using the registers to transmit the parameters. Under the circumstances of the time, the designCthe compiler of the language is to pass the parameters of the function call in the way of the stack, so it can not only solve the problem of less register, but also solve another problem, that is, the number of parameters can be passed dynamically. The above only solves the problem of the number, there is another problem, that is, the order of the parameters into the stack problem. This is like a school sports teacher called a class of students to line up, the first row is from high to short, or from short to high choice. On the issue of the stack,CThe language also faces two choices, one in the same order as the code, from left to right and the other from right to left. After considering the problem of dynamic parameters,Clanguage Designers adopt a right-to-left approach, which has two advantages: first, when the function is run, the default is from left to right, which means that the direction of the stack should take precedence over the elements of the top of the stack, which can improve the efficiency of the operation; Each occurrence of a parameter pops up the stack once, consistent with the order in which the analysis was run. For example, the following function declaration:
printf (const char *,...);
The order in which the stacks are visible is different in the way they are called. In C , the right-to-left way into the stack, in the PASCAL language is left-to-right into the stack order. cdll,windll and oledll in the cTYPES library. is the parameter order that supports right-to-left stacking.
Then came the other problem, since the parameters are used in the way of the stack to pass, then this will happen, when the stack parameters are not used, who will clear, restore the state of the stack. In this case, there are two more options in the compiler's designer: one that tends to be cleared by the caller, and one that tends to be erased by the caller. There is no difference in performance between the two ways, just arranging the cleared code in a different location. Cdll is the way to use the stack cleared by the caller, and Windll and the Oledll is cleared with the callee. This is the difference between them. Therefore, when calling dynamic connection libraries in Python, it is important to understand the invocation method used by each function, otherwise the program will be in trouble and the weight will die directly. the differences between Cdll and windll are as follows: