About wmi

Larry Bates larry.bates at websafe.com`
Sun Jul 13 16:26:02 CEST 2008


patrol wrote:
> I want to prevent some process from running. The code is in the
> following. I  encounter some unexpected troubles.
> Probelm1: This program cannot terminate "scrcons.exe" and
> "FNPLicensingService.exe",which are system processes.
> Problem2:After a while, this program will abort by error
>   File "C:\Python25\lib\wmi.py", line 397, in __call__
> handle_com_error (error_info)
>   File "C:\Python25\lib\wmi.py", line 190, in handle_com_error raise
> x_wmi, "\n".join (exception_string)
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position
> 14: ordinal not in range(128)
> 
> 
> ----------------------------------------------------
> code----------------------------------------------------------------------------------
> # -*- coding:utf-8 -*-
> import pythoncom
> import wmi
> import threading
> import time
> from xml.dom.minidom import parse, parseString
> 
> class Info (threading.Thread):
> 	def __init__ (self):
> 		threading.Thread.__init__ (self)
> 	def run (self):
> 		print 'In Another Thread...'
> 		pythoncom.CoInitialize ()
> 		dom1 = parse('processTerminateList.xml')
> 		config_element = dom1.getElementsByTagName("processTerminateList")
> [0]
> 		servers = config_element.getElementsByTagName("processName")
> 		try:
> 			c = wmi.WMI ()
> 			for process in c.Win32_Process ():
> 				for server in servers:
> 					if process.name == getText(server.childNodes):
> 						process.Terminate()
> 						print process.name
> 			process_watcher = c.Win32_Process.watch_for("creation")
> 			while True:
> 				new_process = process_watcher()
> 				name =  new_process.Caption
> 				print name
> 				for server in servers:
> 					if name == getText(server.childNodes):
> 						new_process.Terminate()
> 		finally:
> 			pythoncom.CoUninitialize ()
> def getText(nodelist):
> 	rc = ""
> 	for node in nodelist:
> 		if node.nodeType == node.TEXT_NODE:
> 			rc = rc + node.data
> 	return rc
> 
> if __name__ == '__main__':
> 	Info().start()
> ------------------------------------------------------
> processTerminateList.xml-----------------------------------------------------------------------
> <?xml version="1.0" encoding="utf-8"?>
> <processTerminateList>
>     <processName>scrcons.exe</processName>
>     <processName>TXPlatform.exe</processName>
>     <processName>mdm.exe</processName>
>     <processName>FNPLicensingService.exe</processName>
>     <processName>notepad.exe</processName>
>     <processName>uedit32.exe</processName>
> </processTerminateList>

You should probably post this to comp.python.windows.  Tim Golden (author of WMI 
interface) monitors that list religously (thanks Tim).

-Larry



More information about the Python-list mailing list