SoFunction
Updated on 2024-11-19

Python calls to ctypes using the C function printf

Importing the ctypes module into a Python program loads a dynamic link library. There are three kinds of dynamic link libraries: cdll, and windll and oledll for windows. cdll loads libraries that export functions using the standard cdecl calling specification, while windll loads libraries that export functions conforming to the stdcall calling specification (a native convention of the Win32 API), and oledll also uses the stdcall calling specification. and assumes that the function returns a Windows HRESULT error code. The error code is used to automatically throw WindowsError, a Python exception, when something goes wrong, and you can use COM functions to get specific error information.

The MS standard C library msvcrt can be called, msvcrt contains most of the standard C functions.

Here's a look at the simple printf function.

from ctypes import *
msvcrt = 
str = "Huanhuan!"
("Hello %s\n", str)

This allows you to use the printf function in C for output.
If you run it in IDLE you will see that the program does not produce any output, this is because printf is printing to real standard output instead. If you want to see the results, you can run python in CMD to see the results, provided you have set the Python environment variables. Or there is a curve method to display the output in IDLE, please curve read to the end of the article.

If you are using Py3K, you will see in the console that only the beginning character H is output. This is because Py3K uses Unicode encoding, which printf does not support, so it needs to be transcoded. Three rewriting methods can be organized to solve this problem.

# A to byte type Precede the string with b
from ctypes import *
msvcrt = 
str = b"Huanhuan!"
(b"Hello %s\n", str)

# B Wide character display using wprintf
from ctypes import *
msvcrt = 
str = "Huanhuan!"
("Hello %s\n", str)

# C converted to utf-8
from ctypes import *
msvcrt = 
str = "Huanhuan!"
result = "Hello " + str + "\n"
result = ("utf-8")
(result)

Finally to take care of displaying the output in IDLE on a curve.

from ctypes import *
msvcrt = 
str = b"Huanhuan!"
s = create_string_buffer(100)  # Must be long enough
(s, b'Hello %s\n', str)
print(('utf-8'))

First use the sprintf function to output the result to the s variable, and then use Python's own print method to output s's value.

Well, with the above various methods you can solve the problem of Py3K calling C function printf.

What? You're asking me why I went to all this trouble to have to use printf output instead of just using Python's own print?

What is the difference between python's print and c's printf?

print([object, ...], *, sep=' ', end='\n', file=, flush=False)

Outputs an object to a stream file, sep specifies the separator, end specifies the terminator. Parameters are converted to strings to be written to the output stream, if there is no output content, the end terminator is output directly. file parameter must be an object containing the write method, the default output is output to the standard output.

int printf( char * format, ... );

Converts and formats the data according to the parameter format string, then outputs the result to a standard output device (monitor) until the end of the string ('\0') appears.
Parameter format The string can contain the following three character types:

For normal text, the output will be directly
ASCII control characters such as \t, \n, etc. have specific meanings.
Format Conversion Characters

Format conversions are composed of a percentage sign (%) and the formatting characters that follow it. In general, each % symbol must be followed by a parameter to correspond to it (only when the %% conversion character will be directly output % characters), and want to output the data type must be the same as its corresponding conversion character type.