使用Python进行Windows系统编程(Windows System Programming Using Python)

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
– Just often not quite how we would like!
os.system()
import os os.system(“notepad C:\\autoexec.bat”)
Problems – Creates a new console window when run from a GUI. – Waits for process to terminate.
O’Reilly Python Conference Aug 24, 1999 Windows System Programming using Python Slide 12
Better Process Control (2 of 2)

win32api.ShellExecute()
– Typically opens “documents” - eg, execute “foo.doc”, and (typically) Word will open. – Finer control over the new process. – Can also execute arbitrary executables - not limited to documents. – For example, to print a specific document: win32api.ShellExecute(0, "print", \ "MyDocument.doc", None, "", 1)
Windows System Programming using Python
Mark Hammond mhammond@skippinet.com.au
OReilly Open Source Python Conference August 1999, Monterey, CA
About this Presentation
Байду номын сангаас
Official releases can be found at
http://www.python.org/windows
Extensions home is at
http://starship.python.net/crew/mhammond
Windows System Programming using Python Slide 6
O’Reilly Python Conference Aug 24, 1999
Windows System Programming using Python
Slide 11
Better Process Control (1 of )
win32api module provides some highlevel, Windows specific functions. win32api.WinExec()
os.execv("c:\\Winnt\\notepad.exe", \ ("c:\\autoexec.bat",) )
– Does clobber your existing process - the call to os.execv() never returns!
O’Reilly Python Conference Aug 24, 1999

– Very similar to os.system(), but overcomes limitations. – >>> import win32api – >>> win32api.WinExec("notepad") – Optional parameter allows you to specify the Window’s initial state (eg, minimized)
O’Reilly Python Conference Aug 24, 1999
Windows System Programming using Python
Slide 2
Who this talk is for?

Existing Python programmers
– Even those without any Windows experience should follow this without problem.
Although
there is not enough time to explain the relevant Windows APIs

Existing Windows Programmers
– Even without Python experience, you should immediately see the similarities between your existing language.

O’Reilly Python Conference Aug 24, 1999 Windows System Programming using Python Slide 5
Python Windows Extensions

Includes:
– Pythonwin: MFC based GUI environment and IDE/Debugger – win32com: Interfaces Python and COM – win32 Extensions: Interfaces to native Win32 API.
Windows System Programming using Python Slide 4
O’Reilly Python Conference Aug 24, 1999
Python and Windows
Each Python version comes with an installer package for Windows. Standard Python port contains all crossplatform Python features, but very few Windows specific features. Python for Windows extensions contains many useful Windows extensions for Python. http://www.python.org/windows

Most content taken directly from upcoming book for O’Reilly Python Programming on Win32 By Mark Hammond and Andy Robinson

http://www.ora.com/catalog/pythonwin32/
O’Reilly Python Conference Aug 24, 1999 Windows System Programming using Python Slide 8
Portable Process Control (1 of 3)


Standard Python functions all work
O’Reilly Python Conference Aug 24, 1999
Windows System Programming using Python
Slide 3
What is Python
Interpreted, dynamic high-level language Obviously open source
O’Reilly Python Conference Aug 24, 1999
System Level Programming?
For this talk, we define system level programming as working with low-level features of Windows Files, Pipes, Processes, Threads, Services, Event Log and so forth. Python and similar languages really not suitable for device-driver type development, and other more system-like Systems Programming!
O’Reilly Python Conference Aug 24, 1999
Windows System Programming using Python
Slide 13
Ultimate Process Control (1 of 2)
win32process module exposes the low level Win32 API. Full support for CreateProcess, CreateProcessAsUser, CreateThread etc. Full support for Windows Handles
Windows System Programming using Python
Slide 10
Portable Process Control (3 of 3)

os.popen()
>>> file = os.popen("echo Hello") >>> file.read() 'Hello\012' – Works fine from Windows NT console programs, but fails miserably from a GUI! – win32pipe module in the Win32 extensions provides a working replacement.

– Files can be passed as stdin/out/err – Process and thread handles are waitable using the win32event module.
O’Reilly Python Conference Aug 24, 1999 Windows System Programming using Python Slide 14

– Many advanced Windows features are not exposed using these interfaces. – Standard implementation of some of the standard library functions leaves a little to be desired in a Windows environment.

– Hence we are here.
Often used in a similar problem domain to Perl/Tcl Proponents consider readability and maintainability a big strong-point


http://www.python.org
O’Reilly Python Conference Aug 24, 1999 Windows System Programming using Python Slide 9
Portable Process Control (2 of 3)

os.execv family
– Doesn’t search system path, and doesn’t parse command lines

O’Reilly Python Conference Aug 24, 1999 Windows System Programming using Python Slide 7
Why not just use Python?
Python has excellent native support for files, processes, threads etc. These features are typically limited to those defined by ANSI C.
相关文档
最新文档