python中win32api_如何在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 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.

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.

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)

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值