Differences between cdll and windll and cdllwindll

Source: Internet
Author: User

Differences between cdll and windll and cdllwindll

To call a dynamic connection library written in C language, Python must not only be compatible with the calling habits of C interfaces, but also the data types of C languages. Fortunately, the ctypes library has already done these two tasks, 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

The code in this line is very concise. This kind of beauty is because the ctypes library has done a lot of work behind it. For example, windll is actually a complicated object. In the ctypes library, it provides three objects that are easy to load dynamic connection libraries: cdll, windll, and oledll. By accessing the attributes of these three objects, you can call the function of the dynamic Connection Library. Cdll is mainly used to load the C language call method (cdecl), windll is mainly used to load the WIN32 call method (stdcall), and oledll uses the WIN32 call method (stdcall) the returned value is the HRESULT value returned in Windows. If you have never learned programming before, you certainly cannot distinguish between cdecl and stdcall. Even if you have learned programming, you may not know it if you have never written a call between different databases. This is because all these details have been hidden in the current IDE development environment. However, such details cannot be ignored in cross-language calls. Then you may ask why these two call methods are not the same dynamic Connection Library? This is a good question. To answer this question, we should start from the time when C language was invented. In 1970s, the American Dennis Ritchie invented the C language and used the C language to write UNIX. As a result, he became the father of the C language and the father of the UNIX operating system. The UNIX operating system is very efficient and easy to modify, thanks to the C language. With the promotion of UNIX operating systems, C language has become a popular language. To make UNIX highly efficient, the design of C language should focus on efficient design. The Design of function calls reflects this point. When calling a function in C language, multiple parameters need to be transferred. These parameters can be transmitted through registers or stacks. Then you may ask why not only register is used? Because function call has many parameters, for example, up to five. At that time, there were very few CPU registers, which could not meet this requirement. Unlike the current CPU of ARM or MIPS, there are many registers and as many as 13. In this case, it is basically possible to use registers to transmit parameters. In the current environment, C language compilers were designed to pass function call parameters in stack mode. This not only solved the problem of fewer registers, but also solved another problem, is the number of parameters that can be passed dynamically. The above only solves the number problem, and another problem occurs, that is, the order of the parameters in the stack. This is like a physical education teacher in a school asking a class of students to queue up and choose from height to height, or from height to height. On the issue of stack entry, the C language also faces two options: one is the same as the code writing order from left to right, and the other is from right to left. Considering the problem of dynamic parameters, the designers of the C language adopt the right-to-left inbound stack method. This method has two advantages: first, when the function is running, the default mode is from left to right, which means that the direction of the outbound stack should be the top element of the stack, which can improve the running efficiency. Second, the function parameters are not scheduled, the required parameters appear in the runtime analysis string. Each time a parameter appears, the stack is displayed, which is consistent with the running analysis sequence. For example, the following function declaration:

Printf (const char *,...);

The calling method varies depending on the order in which the stack can be viewed. In C, the stack is written from right to left, and in PASCAL, the stack is written from left to right. In the ctypes library, cdll, windll, and oledll support the parameter sequence from right to left.

Next, another problem arises. Since the parameters are passed in the stack mode, this will happen. When the stack parameters are not used, who will clear them, restore stack status. In this case, there are two options for Compiler designers: one is intended for callers to clear, and the other is intended for callers to clear. The two methods have no difference in performance, but they only arrange to clear the code in different places. Cdll is the method of clearing stacks by callers, while windll and oledll are cleared by callers. This is the difference between them. Therefore, when calling a dynamic Connection Library in Python, you must be clear about the call method used by each function. Otherwise, the program will have problems and the program will die directly if it is heavy. Differences between cdll and windll are as follows:

 

 


Why does dlldecryption () in a python script fail to be used in windows in linux?

E: \ Script \ python \ encrypt_test.dll is there an encryption function in it, look for the person who writes this dll, or you can check the export name by yourself.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.