How to use the C library in Python

About Python's ctypes module

1. How to use ctypes

First of all, how to import the module.

howToImport.py


from ctypes import *

Next, how to use C library dynamic linking and functions.

howToLoadLib.py


msvcrt=CDLL("C\\WINDOWS\\System32\\msvcrt.dll")
str="On Python!!"
msvcrt.printf("Hello World : %s",str)

By the way, there are several methods of dynamic linking, such as CDLL (), windll (), oledll, etc. I'm a complete beginner, so it's a bit confusing, but maybe I'll use it properly depending on the OS ...?

By the way, input functions such as gets () can also be used normally. Also, for dynamic linking,

howToLoad_2.py


msvcrt=cdll.msvcrt

But it seems that it can be done. (I wonder if this is more common ...?)

I don't know this in detail either, but maybe it can be used if it is environment-dependent and is in the standard library directory ...? You can do it normally without writing an absolute path here.

2. You can use C types with ctypes!

In Python, you generally don't have to specify a type for a variable, but in C you have to specify a type for a variable. And since there is no C type in Python, ctypes has it for you! For example

howToHensu.py


str=c_char_p("AAA")

hensu.c


char *str="AAA"

Two programs like this are probably the same. I don't know the details, but maybe what I'm doing is the same. This is just one example, char, int, short, long, double, float, void * and many more.

By the way, if you want to pass by reference in C language, use a method called byref (). For example, if you want to pass a pointer to a function argument, you can use it like myfunc (byref (pointer)). In this case pointer is just a variable.

You can also use structures and unions as well as variables.

howToStruct.py


class Test(Structure):
_fields_=[
("testInt",c_int),
("testChar",c_char),
]

You can also make a structure like this. (I will omit the union because I haven't studied enough and I don't know how to use it yet.)

reference

By the way, I bought a book called "Reverse Engineering-Binary Analysis Techniques with Python-" and started reading this story, so I wrote it. I've just started reading, so I'd like to upload it again if I read a little more! URL: Reverse Engineering-Binary Analysis Techniques with Python-

By the way, this is Qiita's first time, so I hope you can see it with warm eyes ... w I'm a complete beginner high school student, so it would be very helpful if you could point out any mistakes!

Recommended Posts

How to use the C library in Python
How to use Python Image Library in python3 series
How to use Requests (Python Library)
How to use SQLite in Python
[Python] How to import the library
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
[Python] How to use the graph creation library Altair
How to use the model learned in Lobe in Python
[Introduction to Python] How to use class in Python?
How to debug the Python standard library in Visual Studio
[python] How to use the library Matplotlib for drawing graphs
How to use the __call__ method in a Python class
How to use Google Test in C
How to use __slots__ in Python class
Notes on how to use marshmallow in the schema library
How to use regular expressions in Python
How to use is and == in Python
How to use the asterisk (*) in Python. Maybe this is all? ..
[Introduction to Python] How to use the in operator in a for statement?
[Beginner memo] How to specify the library reading path in Python
How to generate permutations in Python and C ++
How to use the graph drawing library Bokeh
Summary of how to use MNIST in Python
[Algorithm x Python] How to use the list
How to get the files in the [Python] folder
How to use tkinter with python in pyenv
Use the LibreOffice app in Python (3) Add library
python3: How to use bottle (2)
How to use the generator
[Python] How to use list 1
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
How to develop in Python
[Python] How to use input ()
How to use the decorator
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
How to retrieve the nth largest value in Python
[For beginners] How to use say command in python!
How to get the variable name itself in python
How to get the number of digits in Python
How to know the current directory in Python in Blender
How to use the Raspberry Pi relay module Python
I wanted to use the Python library from MATLAB
How to use the exists clause in Django's queryset
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
I tried to summarize how to use pandas in python
[Introduction to Udemy Python3 + Application] 30. How to use the set
How to use the Rubik's Cube solver library "kociemba"
[Python] How to output the list values in order
I want to use the R dataset in python
[Python] How to do PCA in Python
Python: How to use async with
How to use the zip function