?????
1. 使用 os 模塊調(diào)用外部程序
>>> import os
>>> os.system('notepad.exe')
0
>>> os.system('notepad D:\\Python\\test.txt')
????? 也可使用 os 模塊的 popen() 方法來打開外部程序,就不會出現(xiàn)命令提示符窗口。
>>> os.popen(r'C:\Windows\notepad.exe')
<os._wrap_close object at 0x032DEEC8>
2. 使用 win32api 模塊調(diào)用 ShellExecute() 函數(shù)來啟動外部程序
>>> import win32api
>>> win32api.ShellExecute(0, 'open', 'notepad.exe', '', '',0) # 0 表示在后臺運(yùn)行程序
42
>>> win32api.ShellExecute(0, 'open', 'notepad.exe', '', '',1)# 1 表示在前臺運(yùn)行程序
42
>>> win32api.ShellExecute(0, 'open', 'notepad.exe', 'D:\\Python\\test.txt', '',1) # 傳遞參數(shù)打開文件
42
>>> win32api.ShellExecute(0, 'open', 'www.python.org', '', '',0) # 打開網(wǎng)址
42
>>> win32api.ShellExecute(0, 'open', 'D:\\Python\\test.txt', '', '',1) # 相對于雙擊文件
3. 通過創(chuàng)建進(jìn)程來啟動外部程序
>>> import win32process
>>> handle = win32process.CreateProcess(r'C:\Windows\notepad.exe', '', None, None, 0, win32process.CREATE_NO_WINDOW,None,None,win32process.STARTUPINFO()) # 打開記事本程序
>>> win32process.TerminateProcess(handle[0], 0) # 關(guān)閉剛才打開的程序
>>> handle = win32process.CreateProcess(r'C:\Windows\notepad.exe', '', None, None, 0, win32process.CREATE_NO_WINDOW,None,None,win32process.STARTUPINFO())
>>> import win32event
>>> win32event.WaitForSingleObject(handle[0], -1) # 需要手動關(guān)閉記事本
4 通過 ctypes 來調(diào)用動態(tài)鏈接庫代碼
????? ctypes 是 Python 處理動態(tài)鏈接庫的標(biāo)準(zhǔn)擴(kuò)展模塊,運(yùn)行在 Python 中調(diào)用動態(tài)鏈接庫或共享庫中的代碼。
????? 使用 Windows 動態(tài)鏈接庫 user32.dll 中的 MessageBoxA() 函數(shù)來顯示對話框。
>>> import ctypes
>>> ctypes.windll.user32.MessageBoxA(0, str.encode('Hello world!'), str.encode('Python ctypes'), 0)
????? 調(diào)用標(biāo)準(zhǔn) C 函數(shù)庫 msvcrt 中的 printf() 函數(shù)。
>>> mascrt = ctypes.cdll.LoadLibrary('msvcrt')
>>> printf = mascrt.wprintf
>>> printf('Hello world!')
????? ctypes 提供了與 C 語言兼容的數(shù)據(jù)類型,但在 Python 中使用 C 語言的結(jié)構(gòu)體時(shí)需要用類來改寫。

718C3DDC47FA315EFF7561135338F91C.jpg
????? 枚舉進(jìn)程列表。
from ctypes.wintypes import *
from ctypes import *
import collections
kernel32 = windll.kernel32
class tagPROCESSENTRY32(Structure):
_fields_ = [('dwSize', DWORD),
('cntUsage', DWORD),
('th32ProcessID', DWORD),
('th32DefaultHeapID', POINTER(ULONG)),
('th32ModuleID', DWORD),
('cntThreads', DWORD),
('th32ParentProcessID',DWORD),
('pcPriClassBase', LONG),
('dwFlags', DWORD),
('szExeFile', c_char * 260)]
def enumProcess():
hsnapshot = kernel32.CreateToolhelp32Snapshot(15, 0)
fProcessEntry32 = tagPROCESSENTRY32()
processClass = collections.namedtuple("processInfo", "processName processID")
processSet = []
if hsnapshot:
fProcessEntry32.dwSize = sizeof(fProcessEntry32)
listloop = kernel32.Process32First(hsnapshot, byref(fProcessEntry32))
while listloop:
processName = (fProcessEntry32.szExeFile)
processID = fProcessEntry32.th32ProcessID
processSet.append(processClass(processName, processID))
listloop = kernel32.Process32Next(hsnapshot, byref(fProcessEntry32))
return processSet
for i in enumProcess():
print(i.processName, i.processID)
????? 調(diào)用 Windows 底層 API 函數(shù)來創(chuàng)建窗口。
import win32gui
from win32con import *
def WndProc(hwnd, msg, wParam, lParam):
if msg == WM_PAINT:
hdc, ps = win32gui.BeginPaint(hwnd)
rect = win32gui.GetClientRect(hwnd)
win32gui.DrawText(hdc, 'GUI Python', len('GUI Python'), rect,
DT_SINGLELINE|DT_CENTER|DT_VCENTER)
win32gui.EndPaint(hwnd, ps)
if msg == WM_DESTROY:
win32gui.PostQuitMessage(0)
return win32gui.DefWindowProc(hwnd, msg, wParam, lParam)
wc = win32gui.WNDCLASS()
wc.hbrBackground = COLOR_BTNFACE + 1
wc.hCursor = win32gui.LoadCursor(0, IDC_ARROW)
wc.hIcon = win32gui.LoadIcon(0, IDI_APPLICATION)
wc.lpszClassName = 'Python on Windows'
wc.lpfnWndProc = WndProc
reg = win32gui.RegisterClass(wc)
hwnd = win32gui.CreateWindow(reg, 'Python', WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0, None)
win32gui.ShowWindow(hwnd, SW_SHOWNORMAL)
win32gui.UpdateWindow(hwnd)
win32gui.PumpMessages()