python call the operating system library functions

Call the operating system library functions

We know that the use of software written in C language on a python interpreter essence, the operating system? It is also the operating system is essentially a software, whether it is Windows, linux, macOS comes with a lot of shared libraries, then we can use python to call.

from ctypes import *
import platform

# 判断当前的操作系统平台。
# Windows平台返回"Windows",linux平台返回"Linux",macOS平台返回"Darwin"
system = platform.system()

# 不同的平台共享库不同
if system == "Windows":
    libc = cdll.msvcrt
elif system == "Linux":
    libc = CDLL("libc.so.6")
elif system == "Darwin":
    libc = CDLL("libc.dylib")
else:
    print("不支持的平台,程序结束")
    import sys
    sys.exit(0)


# 调用对应的函数,比如printf,注意里面需要传入字节
libc.printf(b"my name is %s, age is %d\n", b"van", 37)  # my name is van, age is 37

# 如果包含汉字就不能使用b""这种形式了,因为这种形式只适用于ascii字符,我们需要手动encode成utf-8
libc.printf("姓名: %s, 年龄: %d\n".encode("utf-8"), "古明地觉".encode("utf-8"), 17)  # 姓名: 古明地觉, 年龄: 17

We are on top of Windows calls, even if the code to get the linux can also be performed normally.

As for the Mac, I do not have Mac, not demonstrated.

Of course, there is also support other functions, we are here to Windows as an example:

from ctypes import *

libc = cdll.msvcrt

# 创建一个大小为10的buffer
s = create_string_buffer(10)
# strcpy表示将字符串进行拷贝
libc.strcpy(s, c_char_p(b"hello satori"))
# 由于buffer只有10个字节大小,所以无法完全拷贝
print(s.value)  # b'hello sato'


# 创建unicode buffer
s = create_unicode_buffer(10)
libc.strcpy(s, c_wchar_p("我也觉得很变态啊"))
print(s.value)  # 我也觉得很变态啊

# 比如puts函数
libc.puts(b"hello world")  # hello world

Other functions in the Windows system

For Windows, we can also call other functions, but this is no longer the way up through cdll.msvcrt. In Windows there is a user32 above such a thing, we look at:

from ctypes import *

# 我们通过cdll.user32本质上还是加载了Windows上的一个共享库
# 这个库给我们提供了很多方便的功能
win = cdll.user32

# 比如查看屏幕的分辨率
print(win.GetSystemMetrics(0))  # 1920
print(win.GetSystemMetrics(1))  # 1080

We can also use it to open MessageBoxA:

We can see that we can easily call the Windows api by windll.user32, which specific api can go online to find, searchwin32 api

About other modules for Windows

In addition to ctypes, there are several modules designed to operate win32 services, win32gui, win32con, win32api, win32com, win32process. Direct pip install pywin32 can, or pip install pypiwin32.

The form is displayed and hidden form

import win32gui
import win32con

# 首先查找窗体,这里查找qq。需要传入 窗口类名 窗口标题名,至于这个怎么获取可以使用spy工具查看
qq = win32gui.FindWindow("TXGuifoundation", "QQ")
# 然后让窗体显示出来
win32gui.ShowWindow(qq, win32con.SW_SHOW)

# 还可以隐藏
win32gui.ShowWindow(qq, win32con.SW_HIDE)

Control the position and size of the form

import win32gui
import win32con

qq = win32gui.FindWindow("TXGuiFoundation", "QQ")

# 主要要接收如下参数
# 参数一:控制的窗体
# 参数二:大致方位:HWND_TOPMOST,位于上方
# 参数三:位置x
# 参数四:位置y
# 参数五:长度
# 参数六:宽度
# 参数七:比较固定,就是让窗体一直显示
win32gui.SetWindowPos(qq, win32con.HWND_TOPMOST, 100, 100, 300, 300, win32con.SWP_SHOWWINDOW)

Then we can also let the window run around full screen

import win32gui
import win32con
import random

qqWin = win32gui.FindWindow("TXGuiFoundation", "QQ")

# 将位置变成随机数
while True:
    x = random.randint(1, 800)
    y = random.randint(1, 400)
    win32gui.SetWindowPos(qqWin, win32con.HWND_TOPMOST, x, y, 300, 300, win32con.SWP_SHOWWINDOW)

Voice playback

import win32com.client
# 直接调用操作系统的语音接口
speaker = win32com.client.Dispatch("SAPI.SpVoice")
# 输入你想要说的话,前提是操作系统语音助手要认识。一般中文和英文是没有问题的
speaker.Speak("他能秒我,他能秒杀我?他要是能把我秒了,我当场······")

python modules in win32 api is very large, almost the entire windows can be operated service provided, win32 module is the equivalent of a Windows service into a package interface. But these services, or call these specific services can be doing, you can go to their own research, here stop here. After all, it could have been combined with the introduction between python and static languages, so I feel this article seems a bit excessive, but it is always good to know more about.

Guess you like

Origin www.cnblogs.com/traditional/p/12239420.html