Windows System Programming
Windows System Programming
Windows System Programming
Programming using
Python
Mark Hammond
mhammond@skippinet.com.au
OReilly
Slide 2
Python programmers
Existing
Windows Programmers
OReilly
Slide 3
What is Python
Interpreted,
OReilly
Slide 4
OReilly
Slide 5
OReilly
Slide 6
OReilly
Slide 7
OReilly
Slide 8
Problems
Creates a new console window when run from a GUI.
Waits for process to terminate.
OReilly
Slide 9
family
OReilly
Slide
OReilly
Slide
OReilly
Slide
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)
OReilly
Slide
OReilly
Slide
Able
OReilly
Slide
Provides event log and performance monitor information. Clients connect using
Named Pipes
Less than 75 lines of code for all this functionality.
Still too big to present in one hit
Selected
OReilly
Slide
Overlapped IO
IO Completion Ports
Named Pipes
NT Security requirements
OReilly
Slide
Native Files (1 of 4)
win32file.CreateFile()
OReilly
Slide
Native Files (2 of 4)
Overlapped
OReilly
Slide
Native Files (3 of 4)
NT
OReilly
Slide
Native Files (4 of 4)
Full
OReilly
Slide
File Sample (1 of 2)
Overlapped
IO is used
self.overlapped = \
pywintypes.OVERLAPPED()
# create the event to be used.
self.overlapped.hEvent = \
win32event.CreateEvent(None,0,0,None)
OReilly
Slide
File Sample (2 of 2)
Create
pipeHandle
= \
win32pipe.CreateNamedPipe(pipeName,
openMode,
pipeMode,
win32pipe.PIPE_UNLIMITED_INSTANCES,
0, 0, 6000, # 6 second timeout.
sa)
...
hr = win32pipe.ConnectNamedPipe(pipeHandle,\
self.overlapped)
OReilly
Slide
Windows NT Services
Similar
OReilly
Slide
>>> win32serviceutil.StopService("Messenger")
(32, 3, 0, 0, 0, 6, 20000)
>>> win32serviceutil.StartService(...)
>>>
OReilly
Slide
Implementing Services
Simple
Simply
OReilly
Slide
to programmer
to Administrator
OReilly
Slide
OReilly
Slide
OReilly
Slide
OReilly
Slide
NT Performance Monitor
Built-in
OReilly
Slide
For example, sample the data hourly and log to a longer-term database.
Useful for reading standard information about another process.
Process
win32pdhutil
OReilly
Slide
OReilly
Slide
PerfMon Sample
Installation
Perfmon
OReilly
Slide
PipeService2_install.ini
Required for performance monitor installation
Most services dont need this!
PipeServiceClient.py
OReilly
Slide
OReilly
Slide
def
SvcStop(self):
# Before we do anything, tell the
# SCM we are starting the stop process.
self.ReportServiceStatus( \
win32service.SERVICE_STOP_PENDING)
# And set my event.
win32event.SetEvent(self.hWaitStop)
OReilly
Slide
request
win32pipe.ConnectNamedPipe(pipeHandle,\
self.overlapped)
...
# Wait for either a connection, or
# a service stop request.
waitHandles = self.hWaitStop, \
self.overlapped.hEvent
rc = win32event.WaitForMultipleObjects(\
waitHandles, 0, timeout)
OReilly
Slide
C:\Scripts> PipeService2.py \
--perfmonini=PipeService2_install.ini install
Installing service PythonPipeService to ...
Service installed
OReilly
Slide
And
OReilly
Slide
OReilly
Slide
Performance
Monitor Data.
OReilly
Slide
Summary
Python
OReilly
Slide
More Information
Python
http://www.python.org
http://www.python.org/doc
Python
http://starship.python.net/crew/mhammond
http://www.python.org/windows
Reference Manuals and Samples included with the distributions
OReilly
Slide
Hammond
mhammond@skippinet.com.au
http://starship.python.net/crew/mhammond
OReilly
Slide