Python calls the ctypes method using the C function printf, pythonctypes

Source: Internet
Author: User

Python calls the ctypes method using the C function printf, pythonctypes

Import the ctypes module to the Python program and load the dynamic link library. There are three types of Dynamic Link Libraries: cdll and windows windll and oledll. The cdll load and export function uses the standard cdecl to call the standard library, while the windll load and export function conforms to the stdcall call specification (the native Convention of Win32 API) library, oledll also uses the stdcall call specification, and assumes that the function returns the Windows HRESULT error code. The error code is used to automatically throw the Python exception WindowsError when an error occurs. You can use the COM function to obtain the specific error message.

You can use cdll. msvcrt to call the MS Standard C library msvcrt. msvcrt contains most of the Standard C functions.

Let's take a look at the simple printf function.

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

In this way, you can use the printf function in the C language for output.
If you run it in IDLE, you will find that the program does not have any output results, because printf is printed to the real standard output, rather than sys. stdout. If you want to see the running result, you can run python test. py in CMD to view the result, provided that the Python environment variable has been set. Or there is a curve method that can display the output results in IDLE. Please read the curve to the end of the article.

If Py3K is used, only the starting character H is output in the console. Because Py3K uses Unicode encoding, and printf does not support this encoding, transcoding is required. Three rewrite methods can solve this problem.

# Add bfrom ctypes import * msvcrt = cdll. msvcrtstr = B "Huanhuan! "Msvcrt. printf (B" Hello % s \ n ", str) # B uses the wprintf wide character to display from ctypes import * msvcrt = cdll. msvcrtstr =" Huanhuan! "Msvcrt. wprintf (" Hello % s \ n ", str) # C transcoding to utf-8from ctypes import * msvcrt = cdll. msvcrtstr =" Huanhuan! "Result =" Hello "+ str +" \ n "result = result. encode (" UTF-8 ") msvcrt. printf (result)

Finally, we can figure out how to display the output result in the IDLE curve.

From ctypes import * msvcrt = cdll. msvcrtstr = B "Huanhuan! "S = create_string_buffer (100) # msvcrt must be long enough. sprintf (s, B 'hello % s \ n', str) print (s. value. decode ('utf-8 '))

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

All right, the above methods can solve the problem of Calling C function printf by Py3K.

What? You asked me why I had to use printf output rather than using the print provided by Python?

What is the difference between print of python and printf of c?

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

Output object to the stream file. sep specifies the delimiter and end specifies the Terminator. The parameter is converted to a string and written to the output stream. If no output content exists, the end character is output directly. The file parameter must be an object that contains the write method and is output to the standard output by default.

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

Convert and format the data according to the format string, and then output the result to the standard output device (Display) until the string ends ('\ 0.
The format string can contain the following three types of characters:

General text, which will be output directly
ASCII control characters, such as \ t and \ n, have specific meanings
Conversion character

The format is converted into a percent sign (%) and a subsequent formatted character. Generally, each % symbol must be followed by a parameter (only when the % conversion character appears will the % character be directly output ), the data type to be output must be the same as the corresponding conversion character type.

Related Article

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.