本文介绍了如何在 Python 中使用 Win32 API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Python 中使用 win32 API?最好和最简单的方法是什么?
你能提供一些例子吗?

How can I use win32 API in Python?What is the best and easiest way to do it?
Can you please provide some examples?

推荐答案

PyWin32 是要走的路——但如何使用它呢?一种方法是从您遇到的具体问题开始并尝试解决它.PyWin32 为 Win32 API 函数提供了很多绑定,你真的必须先选择一个特定的目标.

PyWin32 is the way to go - but how to use it? One approach is to begin with a concrete problem you're having and attempting to solve it. PyWin32 provides bindings for the Win32 API functions for which there are many, and you really have to pick a specific goal first.

在我的 Python 2.5 安装(Windows 上的 ActiveState)中,win32 包有一个 Demos 文件夹,其中包含库各个部分的示例代码.

In my Python 2.5 installation (ActiveState on Windows) the win32 package has a Demos folder packed with sample code of various parts of the library.

例如,这里是 CopyFileEx.py:

For example, here's CopyFileEx.py:

import win32file, win32api
import os


def ProgressRoutine(TotalFileSize, TotalBytesTransferred, StreamSize, StreamBytesTransferred,
    StreamNumber, CallbackReason, SourceFile, DestinationFile, Data):
    print Data
    print TotalFileSize, TotalBytesTransferred, StreamSize, StreamBytesTransferred, StreamNumber, CallbackReason, SourceFile, DestinationFile
    ##if TotalBytesTransferred > 100000:
    ##    return win32file.PROGRESS_STOP
    return win32file.PROGRESS_CONTINUE

temp_dir=win32api.GetTempPath()
fsrc=win32api.GetTempFileName(temp_dir,'cfe')[0]
fdst=win32api.GetTempFileName(temp_dir,'cfe')[0]
print fsrc, fdst

f=open(fsrc,'w')
f.write('xxxxxxxxxxxxxxxx\n'*32768)
f.close()
## add a couple of extra data streams
f=open(fsrc+':stream_y','w')
f.write('yyyyyyyyyyyyyyyy\n'*32768)
f.close()
f=open(fsrc+':stream_z','w')
f.write('zzzzzzzzzzzzzzzz\n'*32768)
f.close()

operation_desc='Copying '+fsrc+' to '+fdst
win32file.CopyFileEx(fsrc, fdst, ProgressRoutine, operation_desc, False,   win32file.COPY_FILE_RESTARTABLE)

它展示了如何将 CopyFileEx 函数与其他一些函数(例如 GetTempPath 和 GetTempFileName)一起使用.从这个例子中,你可以对如何使用这个库有一个一般的感觉".

It shows how to use the CopyFileEx function with a few others (such as GetTempPath and GetTempFileName). From this example you can get a "general feel" of how to work with this library.

这篇关于如何在 Python 中使用 Win32 API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:38
查看更多