杰瑞科技汇

Python如何调用Windows API?

Of course! "Python Windows API" is a broad topic, but it's a powerful way to automate and interact with the Windows operating system. Here’s a comprehensive guide covering the main approaches, libraries, and practical examples.

Python如何调用Windows API?-图1
(图片来源网络,侵删)

What is the Windows API?

The Windows API (Application Programming Interface) is a set of thousands of functions, messages, structures, and interfaces provided by the Windows operating system. It's the "C-language" interface that all native Windows applications (like Notepad, Chrome, etc.) use to perform tasks, such as:

  • Creating windows and dialogs
  • Handling user input (keyboard, mouse)
  • Accessing the file system
  • Managing processes and services
  • Interacting with the registry
  • Sending network requests

Why Use Python for the Windows API?

While the API is designed for C/C++, using Python offers significant advantages:

  • Rapid Development: Python's syntax is clean and concise, allowing you to build complex scripts much faster than in C++.
  • Powerful Libraries: Python has excellent libraries for data manipulation, networking, and more, which you can combine with Windows API calls.
  • Automation: It's the perfect tool for system administration, automating repetitive tasks, and building "glue" scripts that control other Windows applications.

The Main Approaches to Access the Windows API from Python

There are three primary ways to interact with the Windows API from Python, each with its own pros and cons.

Approach Description Pros Cons Best For
ctypes Python's built-in foreign function library. It allows you to call functions in DLLs directly. - Built-in: No installation needed.
- Full control: Access to almost any API function.
- Lightweight.
- Verbose: Requires manual definition of constants, structures, and function signatures.
- Error-prone: Easy to make mistakes with data types.
Quick scripts, learning the API, or when you can't install external libraries.
pywin32 A mature, comprehensive library that wraps many parts of the Windows API in Pythonic objects. - Easy to use: High-level, object-oriented interface.
- Well-documented: Extensive documentation and examples.
- Stable: Has been around for decades.
- Requires installation: pip install pywin32.
- Can be heavy: Installs a lot of modules you might not need.
Most general-purpose Windows automation tasks, GUI interaction, COM object handling.
pywin32 + win32com A specific part of pywin32 for interacting with COM (Component Object Model) applications. - Powerful automation: Can control Office, Outlook, Internet Explorer, etc.
- High-level: Abstracts away complex API calls.
- Limited to COM-aware apps.
- Can be tricky to debug.
Automating tasks in Microsoft Office (Word, Excel, Outlook), or other COM-based applications.

Approach 1: ctypes (The Built-in Power Tool)

ctypes is the most direct way to call the API. You load a DLL (like user32.dll or kernel32.dll), get a function from it, and call it.

Python如何调用Windows API?-图2
(图片来源网络,侵删)

Example: Getting the Windows Version

This classic example demonstrates how to call GetVersionExW from the kernel32.dll to get detailed OS version information.

import ctypes
from ctypes import wintypes
# Define the structure that the API function will fill in.
# We must match the C structure definition.
class OSVERSIONINFOEXW(ctypes.Structure):
    _fields_ = [
        ('dwOSVersionInfoSize', wintypes.DWORD),
        ('dwMajorVersion', wintypes.DWORD),
        ('dwMinorVersion', wintypes.DWORD),
        ('dwBuildNumber', wintypes.DWORD),
        ('dwPlatformId', wintypes.DWORD),
        ('szCSDVersion', wintypes.WCHAR * 128),
        ('wServicePackMajor', wintypes.WORD),
        ('wServicePackMinor', wintypes.WORD),
        ('wSuiteMask', wintypes.WORD),
        ('wProductType', wintypes.BYTE),
        ('wReserved', wintypes.BYTE),
    ]
# Load the DLL and get the function handle
try:
    kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
    GetVersionExW = kernel32.GetVersionExW
    GetVersionExW.argtypes = [ctypes.POINTER(OSVERSIONINFOEXW)]
    GetVersionExW.restype = wintypes.BOOL
except AttributeError:
    print("GetVersionExW function not found in kernel32.dll")
    exit()
# Create an instance of the structure and set its size
os_version_info = OSVERSIONINFOEXW()
os_version_info.dwOSVersionInfoSize = ctypes.sizeof(os_version_info)
# Call the function
if not GetVersionExW(ctypes.byref(os_version_info)):
    # If it fails, get the error code
    error_code = ctypes.get_last_error()
    print(f"Error calling GetVersionExW: {error_code}")
    exit()
# Print the results
print(f"Windows Version: {os_version_info.dwMajorVersion}.{os_version_info.dwMinorVersion}")
print(f"Build Number: {os_version_info.dwBuildNumber}")
print(f"CSD Version: {os_version_info.szCSDVersion.decode('utf-16-le')}")

Approach 2: pywin32 (The Easiest and Most Common)

pywin32 provides Python classes and functions that map directly to Windows API concepts. It's much more readable than ctypes.

Installation

pip install pywin32

Example: Listing All Top-Level Windows

This script uses win32gui to list the title and handle of every visible top-level window on your desktop.

import win32gui
import win32con
def enum_windows_proc(hwnd, lParam):
    """
    This callback function is called for each window found by EnumWindows.
    """
    # Check if the window is visible and has a title
    if win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowText(hwnd):
        # Get the window title
        window_title = win32gui.GetWindowText(hwnd)
        # Get the window class name
        class_name = win32gui.GetClassName(hwnd)
        print(f"Handle: {hwnd}, Title: '{window_title}', Class: '{class_name}'")
    # Return True to continue enumeration
    return True
print("Enumerating all visible windows...")
# EnumWindows calls the enum_windows_proc function for each top-level window.
win32gui.EnumWindows(enum_windows_proc, None)
print("Done.")

Example: Creating a Simple Message Box

This shows how to call a high-level function that doesn't require manual structure definitions.

Python如何调用Windows API?-图3
(图片来源网络,侵删)
import win32api
import win32con
# Display a message box
result = win32api.MessageBox(
    0,
    "Hello from Python via the Windows API!",
    "Python pywin32 Example",
    win32con.MB_YESNO | win32con.MB_ICONQUESTION
)
if result == win32con.IDYES:
    print("User clicked Yes.")
else:
    print("User clicked No.")

Approach 3: win32com for Automating Office

This is incredibly useful for tasks like creating a report, sending an email, or manipulating a spreadsheet.

Example: Creating an Excel Workbook and Saving It

import win32com.client
# Create a COM object for Excel
excel = win32com.client.Dispatch("Excel.Application")
# Make Excel visible (optional)
excel.Visible = True
# Add a new workbook
workbook = excel.Workbooks.Add()
# Get the active sheet
sheet = workbook.ActiveSheet
# Write some data to cells
sheet.Cells(1, 1).Value = "Hello"
sheet.Cells(1, 2).Value = "World"
sheet.Cells(2, 1).Value = "From Python"
sheet.Cells(2, 2).Value = "via COM!"
# Save the workbook
# The '2' parameter means the file format is an Excel Workbook (.xlsx)
workbook.SaveAs("C:\\Temp\\python_excel_test.xlsx", FileFormat=2)
# Close the workbook
workbook.Close()
# Quit Excel
excel.Quit()
print("Excel file created successfully.")

Important Considerations and Best Practices

  1. Permissions: Many API functions, especially those dealing with system services or other processes, require Administrator privileges. Your script might fail if you run it from a standard user account.

  2. Error Handling: Windows API calls can fail for many reasons (file not found, access denied, invalid parameters, etc.). Always check the return value of API functions. ctypes provides get_last_error(), and pywin32 functions often raise exceptions on failure.

  3. Documentation: The ultimate source of truth for the Windows API is the official Microsoft Docs. When you use pywin32, the documentation on its GitHub repo is also invaluable.

  4. Modern Alternatives: For some tasks, consider modern Python libraries that are often easier to use:

    • File System: Use the built-in pathlib or os modules instead of CreateFile/ReadFile API calls.
    • Networking: Use the requests library instead of raw Winsock API calls.
    • System Information: Use libraries like psutil for process and system monitoring.

Summary and Where to Start

  • For simple scripts and learning: Start with ctypes. It forces you to understand the underlying API calls.
  • For 90% of automation tasks: Use pywin32. It's the sweet spot between power and ease of use. The win32gui, win32process, and win32api modules are your friends.
  • For automating Microsoft Office or other COM apps: You absolutely need pywin32 and its win32com module. It's the industry standard for this.
分享:
扫描分享到社交APP
上一篇
下一篇