473,378 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

About wmi

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>
Jul 13 '08 #1
25 2557
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
Jul 13 '08 #2
On 7ÔÂ13ÈÕ, ÏÂÎç10ʱ26·Ö, Larry Bates <larry.ba...@websafe.com`wrote:
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 (authorof WMI
interface) monitors that list religously (thanks Tim).

-Larry- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -
I cannot find comp.python.windows.What's the URL?
Jul 14 '08 #3
patrol wrote:
On 7ÔÂ13ÈÕ, ÏÂÎç10ʱ26·Ö, Larry Bates <larry.ba...@websafe.com`wrote:
>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- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -

I cannot find comp.python.windows.What's the URL?
http://news.gmane.org/gmane.comp.pyt...ws/cutoff=7565

-Larry
Jul 14 '08 #4
On 7ÔÂ14ÈÕ, ÏÂÎç12ʱ29·Ö, Larry Bates <larry.ba...@websafe.com`wrote:
patrol wrote:
On 7ÔÂ13ÈÕ, ÏÂÎç10ʱ26·Ö, Larry Bates <larry.ba....@websafe.com`wrote:
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- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
- ÏÔʾÒýÓõÄÎÄ×Ö -
I cannot find comp.python.windows.What's the URL?

http://news.gmane.org/gmane.comp.pyt...ws/cutoff=7565

-Larry- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -
Thanks
Jul 14 '08 #5
Larry Bates wrote:
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)
[... snip code ...]
You should probably post this to comp.python.windows. Tim Golden
(author of WMI interface) monitors that list religously (thanks Tim).

Actually, I follow this one pretty much, too. I've just been a bit busy
these last few days. And still am, so this answer will be short :)
1) I'm not sure if WMI can be forced to close down system processes,
but if it can it's probably by means of specifying one or more
privileges when you connect. Try looking in the WMI newsgroups
for a more general (non-Python) answer to this and I'll happily
explain how to apply it in a Python context.

2) I can't quite see from this traceback where the problem
arises. Have you snipped the traceback at all, or was that
all there was? Can you narrow the thing down to a short
snippet of code which I'm likely to be able to run independently,
please?

Sorry for the haste.

TJG
Jul 14 '08 #6
Hi,
1) I'm not sure if WMI can be forced to close down system processes,
but if it can it's probably by means of specifying one or more
privileges when you connect. Try looking in the WMI newsgroups
for a more general (non-Python) answer to this and I'll happily
explain how to apply it in a Python context.
I use VBS to kill these processes,the VBS cannot kill these either.
2) I can't quite see from this traceback where the problem
arises. Have you snipped the traceback at all, or was that
all there was? Can you narrow the thing down to a short
snippet of code which I'm likely to be able to run independently,
please?
import wmi
from time import sleep

c = wmi.WMI ()
process_watcher = c.Win32_Process.watch_for("creation")
while True:
new_process = process_watcher()
if new_process.Caption == 'notepad.exe':
print "start killing.."
sleep(5)
result = new_process.terminate()
print "killed"
We must start the notepad.exe manually, then (1) kill the notepad.exe
by this code.
(2)we kill the notepad.exe before this code manually. Both will result
in errors.
Jul 15 '08 #7

Situation (1):
result = new_process.terminate()
TypeError: 'int' object is not callable

Situation (2):
result = new_process.terminate()
File "C:\Python25\lib\wmi.py", line 494, in __getattr__
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)

BTW, My windows' languange is Chinese.
Jul 15 '08 #8
patrol wrote:
Situation (2):
result = new_process.terminate()
File "C:\Python25\lib\wmi.py", line 494, in __getattr__
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)

BTW, My windows' languange is Chinese.

Well that looks embarrassingly like a complete lack
of unicode-awareness in the wmi module. Would you
mind trying this version:

http://timgolden.me.uk/wmi-project/wmi.py

which is a copy of the svn trunk to see if that
improves the UnicodeDecode error, please? I'll
try to get an install of a non-English edition of
Windows but, as you might imagine, I normally run
the UK version so don't hit these kind of issue
myself.

TJG
Jul 15 '08 #9
patrol wrote:
Situation (1):
result = new_process.terminate()
TypeError: 'int' object is not callable
I'm not sure exactly what's causing that
particular effect, but I would suggest that
you call the method as .Terminate (note the
initial capital). On my box, calling .terminate
simply raises an AttributeError as expected,
but I do remember encountering the situation
you're describing in some situation which
now escapes me.

At any rate, try using:

result, = new_process.Terminate ()

and note that the return value is a tuple,
not a single number. The code will work either
way, but in your case "result" will be a tuple
of length one; in mine, result will be a number.

TJG
Jul 15 '08 #10
http://timgolden.me.uk/wmi-project/wmi.py

It cannot work either.
which is a copy of the svn trunk to see if that
improves the UnicodeDecode error, please? I'll
try to get an install of a non-English edition of
Windows but, as you might imagine, I normally run
the UK version so don't hit these kind of issue
myself.
I expect you can test successfully at a non-English edition

Jul 16 '08 #11

At any rate, try using:

result, = new_process.Terminate ()
Windows is sometime case insensitive,but the python is case sensitive.
I also encountered this kind of problems.

Thanks for Tim's help.

Patrol
Jul 16 '08 #12
patrol wrote:
>http://timgolden.me.uk/wmi-project/wmi.py

It cannot work either.
Oh well. It was only a quick fix! I'll try
to get some kind of non-ASCII edition of Windows
to test against. As I understand it, the situation
is that some WMI exception (ie coming from the
underlying WMI/COM subsystem) results in an error
message which contains non-ASCII characters.

Just so I'm not chasing red herrings, could you
paste the output from the following code, please?

<code>
import wmi # use the version linked above

c = wmi.WMI ("non-existent computer")

#
# Should give a traceback here for the DCOM
# error, not a UnicodeDecodeError.
#
</code>

Thanks

TJG
Jul 16 '08 #13
On 7ÔÂ16ÈÕ, ÏÂÎç3ʱ29·Ö, Tim Golden <m...@timgolden..me.ukwrote:
patrol wrote:
>http://timgolden.me.uk/wmi-project/wmi.py
It cannot work either.

Oh well. It was only a quick fix! I'll try
to get some kind of non-ASCII edition of Windows
to test against. As I understand it, the situation
is that some WMI exception (ie coming from the
underlying WMI/COM subsystem) results in an error
message which contains non-ASCII characters.

Just so I'm not chasing red herrings, could you
paste the output from the following code, please?

<code>
import wmi # use the version linked above

c = wmi.WMI ("non-existent computer")

#
# Should give a traceback here for the DCOM
# error, not a UnicodeDecodeError.
#
</code>

Thanks

TJG
The errors are in the following:

Traceback (most recent call last):
File "D:\My Documents\code\python\wmi\test.py", line 5, in <module>
c = wmi.WMI ("non-existent computer")
File "C:\Python25\lib\wmi.py", line 1199, in connect
handle_com_error (error_info)
File "C:\Python25\lib\wmi.py", line 184, in handle_com_error
exception_string = [u"%s - %s" % (hex (hresult_code),
hresult_name)]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position
4: ordinal not in range(128)

Patrol
Jul 16 '08 #14
patrol wrote:
The errors are in the following:

Traceback (most recent call last):
File "D:\My Documents\code\python\wmi\test.py", line 5, in <module>
c = wmi.WMI ("non-existent computer")
File "C:\Python25\lib\wmi.py", line 1199, in connect
handle_com_error (error_info)
File "C:\Python25\lib\wmi.py", line 184, in handle_com_error
exception_string = [u"%s - %s" % (hex (hresult_code),
hresult_name)]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position
4: ordinal not in range(128)
OK, I'm trying to set up a Virtual PC so I can install
a non-English XP. But would you mind running the
following code for me, please, so I can get a handle
on what's coming back:

<code>
import pythoncom
import win32com.client

try:
win32com.client.GetObject ("winmgmts://blahblah")
except pythoncom.com_error, info:
for i in info:
print repr (i)

</code>

Thanks
TJG
Jul 16 '08 #15
On 7ÔÂ16ÈÕ, ÏÂÎç10ʱ39·Ö, Tim Golden <m...@timgolden.me.ukwrote:
patrol wrote:
The errors are in the following:
Traceback (most recent call last):
File "D:\My Documents\code\python\wmi\test.py", line 5, in <module>
c = wmi.WMI ("non-existent computer")
File "C:\Python25\lib\wmi.py", line 1199, in connect
handle_com_error (error_info)
File "C:\Python25\lib\wmi.py", line 184, in handle_com_error
exception_string = [u"%s - %s" % (hex (hresult_code),
hresult_name)]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position
4: ordinal not in range(128)

OK, I'm trying to set up a Virtual PC so I can install
a non-English XP. But would you mind running the
following code for me, please, so I can get a handle
on what's coming back:

<code>
import pythoncom
import win32com.client

try:
win32com.client.GetObject ("winmgmts://blahblah")
except pythoncom.com_error, info:
for i in info:
print repr (i)

</code>

Thanks
TJG
-2147023174
'RPC \xb7\xfe\xce\xf1\xc6\xf7\xb2\xbb\xbf\xc9\xd3\xc3\x a1\xa3'
None
None

----------------------------------------------------------------------
import pythoncom
import win32com.client
try:
win32com.client.GetObject ("winmgmts://blahblah")
except pythoncom.com_error, info:
for i in info:
print i

-2147023174
RPC ·þÎñÆ÷²»¿ÉÓá£
None
None
-------------------------------------------------------------------------
>>a="RPC ·þÎñÆ÷²»¿ÉÓá£"
a
'RPC \xb7\xfe\xce\xf1\xc6\xf7\xb2\xbb\xbf\xc9\xd3\xc3\x a1\xa3'
>>>
-------------------------------------------------------------------------
Patrol
Jul 16 '08 #16
patrol wrote:
-2147023174
'RPC \xb7\xfe\xce\xf1\xc6\xf7\xb2\xbb\xbf\xc9\xd3\xc3\x a1\xa3'
None
None

----------------------------------------------------------------------
import pythoncom
import win32com.client
try:
win32com.client.GetObject ("winmgmts://blahblah")
except pythoncom.com_error, info:
for i in info:
print i

-2147023174
RPC ·þÎñÆ÷²»¿ÉÓá£
None
None
-------------------------------------------------------------------------
>>>a="RPC ·þÎñÆ÷²»¿ÉÓá£"
a
'RPC \xb7\xfe\xce\xf1\xc6\xf7\xb2\xbb\xbf\xc9\xd3\xc3\x a1\xa3'
-------------------------------------------------------------------------
Patrol
Brilliant. Thanks, Patrol. So the error message comes back
encoded. Can you confirm what your console encoding is,
please? The following script should confirm:

<code>
import os, sys

print sys.stdout.encoding
os.system ("chcp")

</code>

TJG
Jul 16 '08 #17
Assuming that the error comes back in the sys.stdout encoding, the following version *should* work ok. I still haven't got a non-English set up to test it on, but it certainly does return a Unicode error message.

http://timgolden.me.uk/wmi-project/wmi.py

The usual test case, if you wouldn't mind:

<code>
import wmi

wmi.WMI ("non-existent computer")

</code>

should give a (language-specific) error message, not an UnicodeDecodeError

TJG
Jul 16 '08 #18
On 7ÔÂ16ÈÕ, ÏÂÎç11ʱ59·Ö, Tim Golden <m...@timgolden.me.ukwrote:
patrol wrote:
-2147023174
'RPC \xb7\xfe\xce\xf1\xc6\xf7\xb2\xbb\xbf\xc9\xd3\xc3\x a1\xa3'
None
None
----------------------------------------------------------------------
import pythoncom
import win32com.client
try:
win32com.client.GetObject ("winmgmts://blahblah")
except pythoncom.com_error, info:
for i in info:
print i
-2147023174
RPC ·þÎñÆ÷²»¿ÉÓá£
None
None
-------------------------------------------------------------------------
>>a="RPC ·þÎñÆ÷²»¿ÉÓá£"
a
'RPC \xb7\xfe\xce\xf1\xc6\xf7\xb2\xbb\xbf\xc9\xd3\xc3\x a1\xa3'
-------------------------------------------------------------------------
Patrol

Brilliant. Thanks, Patrol. So the error message comes back
encoded. Can you confirm what your console encoding is,
please? The following script should confirm:

<code>
import os, sys

print sys.stdout.encoding
os.system ("chcp")

</code>

TJG- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -
>>import os,sys
print sys.stdout.encoding
cp936
>>os.system("chcp")
»î¶¯µÄ´úÂëÒ³: 936
0
>>sys.getdefaultencoding()
'ascii'
Jul 16 '08 #19
On 7ÔÂ17ÈÕ, ÉÏÎç12ʱ16·Ö, Tim Golden <m...@timgolden.me.ukwrote:
Assuming that the error comes back in the sys.stdout encoding, the following version *should* work ok. I still haven't got a non-English set up to test it on, but it certainly does return a Unicode error message.

http://timgolden.me.uk/wmi-project/wmi.py

The usual test case, if you wouldn't mind:

<code>
import wmi

wmi.WMI ("non-existent computer")

</code>

should give a (language-specific) error message, not an UnicodeDecodeError

TJG
--------------------------------------------------------------------------------------
>>import wmi
wmi.WMI('non-existent computer')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python25\lib\wmi.py", line 1199, in connect
handle_com_error (error_info)
File "C:\Python25\lib\wmi.py", line 184, in handle_com_error
exception_string = [u"%s - %s" % (hex (hresult_code),
hresult_name)]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position
4: ordinal
not in range(128)
--------------------------------------------------------------------------------------
yup,error_info contains the Chinese encoded string. All of the Simple
Chinese Windows use the CP936.Every Chinese word utilizes two
bytes.Maybe you can fix this bug by modifying handle_com_error.

Patrol
Jul 16 '08 #20
patrol wrote:
>>>import wmi
wmi.WMI('non-existent computer')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python25\lib\wmi.py", line 1199, in connect
handle_com_error (error_info)
File "C:\Python25\lib\wmi.py", line 184, in handle_com_error
exception_string = [u"%s - %s" % (hex (hresult_code),
hresult_name)]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position
4: ordinal
not in range(128)
--------------------------------------------------------------------------------------
yup,error_info contains the Chinese encoded string. All of the Simple
Chinese Windows use the CP936.Every Chinese word utilizes two
bytes.Maybe you can fix this bug by modifying handle_com_error.

Well, that's what I've done in that latest version.
Only I naively assumed
that I could use sys.stdout.encoding to determine
the encoding. I'll have to try harder.

TJG
Jul 16 '08 #21
patrol wrote:
On 7ÔÂ17ÈÕ, ÉÏÎç12ʱ16·Ö, Tim Golden <m...@timgolden.me.ukwrote:
>Assuming that the error comes back in the sys.stdout encoding, the following version *should* work ok. I still haven't got a non-English set up to test it on, but it certainly does return a Unicode error message.

http://timgolden.me.uk/wmi-project/wmi.py

The usual test case, if you wouldn't mind:

<code>
import wmi

wmi.WMI ("non-existent computer")

</code>

should give a (language-specific) error message, not an UnicodeDecodeError

TJG
--------------------------------------------------------------------------------------
>>>import wmi
wmi.WMI('non-existent computer')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python25\lib\wmi.py", line 1199, in connect
handle_com_error (error_info)
File "C:\Python25\lib\wmi.py", line 184, in handle_com_error
exception_string = [u"%s - %s" % (hex (hresult_code),
hresult_name)]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position
4: ordinal
not in range(128)
--------------------------------------------------------------------------------------
yup,error_info contains the Chinese encoded string. All of the Simple
Chinese Windows use the CP936.Every Chinese word utilizes two
bytes.Maybe you can fix this bug by modifying handle_com_error.
Can you confirm that that last bit of
code was run with the version of wmi.py
currently at:

http://timgolden.me.uk/wmi-project/wmi.py

That version should already be decoding the
string correctly.

TJG
Jul 16 '08 #22
On 7ÔÂ17ÈÕ, ÉÏÎç3ʱ20·Ö, Tim Golden <m...@timgolden..me.ukwrote:
patrol wrote:
On 7ÔÂ17ÈÕ, ÉÏÎç12ʱ16·Ö, Tim Golden <m...@timgolden.me.ukwrote:
Assuming that the error comes back in the sys.stdout encoding, the following version *should* work ok. I still haven't got a non-English set up to test it on, but it certainly does return a Unicode error message.
>http://timgolden.me.uk/wmi-project/wmi.py
The usual test case, if you wouldn't mind:
<code>
import wmi
wmi.WMI ("non-existent computer")
</code>
should give a (language-specific) error message, not an UnicodeDecodeError
TJG
--------------------------------------------------------------------------------------
>>import wmi
wmi.WMI('non-existent computer')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python25\lib\wmi.py", line 1199, in connect
handle_com_error (error_info)
File "C:\Python25\lib\wmi.py", line 184, in handle_com_error
exception_string = [u"%s - %s" % (hex (hresult_code),
hresult_name)]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position
4: ordinal
not in range(128)
--------------------------------------------------------------------------------------
yup,error_info contains the Chinese encoded string. All of the Simple
Chinese Windows use the CP936.Every Chinese word utilizes two
bytes.Maybe you can fix this bug by modifying handle_com_error.

Can you confirm that that last bit of
code was run with the version of wmi.py
currently at:

http://timgolden.me.uk/wmi-project/wmi.py

That version should already be decoding the
string correctly.

TJG
----------------------------------------------------------------------------------------
>>import wmi
wmi.WMI('non-existent computer')
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
wmi.WMI('non-existent computer')
File "C:\Python25\lib\wmi.py", line 1199, in connect
handle_com_error (error_info)
File "C:\Python25\lib\wmi.py", line 189, in handle_com_error
raise x_wmi, u"\n".join (exception_string)
x_wmi: <unprintable x_wmi object>
------------------------------------------------------------------------------------------
# -*- coding:utf-8 -*-
import wmi
from time import sleep

c = wmi.WMI ()
process_watcher = c.Win32_Process.watch_for("creation")
while True:
new_process = process_watcher()
if new_process.Caption == 'notepad.exe':
print "start killing.."
sleep(5)
result = new_process.Terminate()
print "killed"
When I kill the notepad.exe manually,it also results in errors.
Traceback (most recent call last):
File "C:\Documents and Settings\patrol\×ÀÃæ\test.py", line 12, in
<module>
result = new_process.Terminate()
File "C:\Python25\lib\wmi.py", line 396, in __call__
handle_com_error (error_info)
File "C:\Python25\lib\wmi.py", line 188, in handle_com_error
exception_string.append (u" %s - %s" % (hex (scode),
(error_description or
u"").decode (sys.stdout.encoding).strip ()))
UnicodeEncodeError: 'ascii' codec can't encode characters in position
0-2: ordin
al not in range(128)
-----------------------------------------------------------------------------------------------
Patrol
Jul 17 '08 #23
I will try to modify the wmi.py ,however I'm a novice.It will take a
long time. You can give it up temporarily. If you don't mind ,can you
tell me where needs modifying and how? Just unicode? Or Other?
Jul 17 '08 #24
patrol wrote:
I will try to modify the wmi.py ,however I'm a novice.It will take a
long time. You can give it up temporarily. If you don't mind ,can you
tell me where needs modifying and how? Just unicode? Or Other?
OK. Thanks for your patience on this one, Patrol. What I propose
to do is to dig into the pywin32 sources to determine what's going
on when the error messages are fetched. Then I'll be better placed
to decide what to do when they come out. On the surface, the current
module should be handling things correctly; but I've obviously missed
an encode/decode somewhere.

If I can think of an (even hackish) workaround for you in the meantime,
I'll let you know. Until then...

TJG

Jul 17 '08 #25
On 7ÔÂ17ÈÕ, ÏÂÎç4ʱ22·Ö, Tim Golden <m...@timgolden..me.ukwrote:
patrol wrote:
I will try to modify the wmi.py ,however I'm a novice.It will take a
long time. You can give it up temporarily. If you don't mind ,can you
tell me where needs modifying and how? Just unicode? Or Other?

OK. Thanks for your patience on this one, Patrol. What I propose
to do is to dig into the pywin32 sources to determine what's going
on when the error messages are fetched. Then I'll be better placed
to decide what to do when they come out. On the surface, the current
module should be handling things correctly; but I've obviously missed
an encode/decode somewhere.

If I can think of an (even hackish) workaround for you in the meantime,
I'll let you know. Until then...

TJG
Thanks for Tim's help.

Patrol
Jul 18 '08 #26

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
8
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
77
by: nospam | last post by:
Reasons for a 3-tier achitecture for the WEB? (NOTE: I said, WEB, NOT WINDOWS. DON'T shoot your mouth off if you don't understand the difference.) I hear only one reason and that's to switch a...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
5
by: eScrewDotCom | last post by:
www.eScrew.com eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
0
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
7
by: Edward Yang | last post by:
A few days ago I started a thread "I think C# is forcing us to write more (redundant) code" and got many replies (more than what I had expected). But after reading all the replies I think my...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.