About wmi

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • patrol

    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.ex e" and
    "FNPLicensingSe rvice.exe",whic h are system processes.
    Problem2:After a while, this program will abort by error
    File "C:\Python25\li b\wmi.py", line 397, in __call__
    handle_com_erro r (error_info)
    File "C:\Python25\li b\wmi.py", line 190, in handle_com_erro r raise
    x_wmi, "\n".join (exception_stri ng)
    UnicodeDecodeEr ror: '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.Thre ad):
    def __init__ (self):
    threading.Threa d.__init__ (self)
    def run (self):
    print 'In Another Thread...'
    pythoncom.CoIni tialize ()
    dom1 = parse('processT erminateList.xm l')
    config_element = dom1.getElement sByTagName("pro cessTerminateLi st")
    [0]
    servers = config_element. getElementsByTa gName("processN ame")
    try:
    c = wmi.WMI ()
    for process in c.Win32_Process ():
    for server in servers:
    if process.name == getText(server. childNodes):
    process.Termina te()
    print process.name
    process_watcher = c.Win32_Process .watch_for("cre ation")
    while True:
    new_process = process_watcher ()
    name = new_process.Cap tion
    print name
    for server in servers:
    if name == getText(server. childNodes):
    new_process.Ter minate()
    finally:
    pythoncom.CoUni nitialize ()
    def getText(nodelis t):
    rc = ""
    for node in nodelist:
    if node.nodeType == node.TEXT_NODE:
    rc = rc + node.data
    return rc

    if __name__ == '__main__':
    Info().start()
    ------------------------------------------------------
    processTerminat eList.xml-----------------------------------------------------------------------
    <?xml version="1.0" encoding="utf-8"?>
    <processTermina teList>
    <processName>sc rcons.exe</processName>
    <processName>TX Platform.exe</processName>
    <processName>md m.exe</processName>
    <processName>FN PLicensingServi ce.exe</processName>
    <processName>no tepad.exe</processName>
    <processName>ue dit32.exe</processName>
    </processTerminat eList>
  • Larry Bates

    #2
    Re: About wmi

    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.ex e" and
    "FNPLicensingSe rvice.exe",whic h are system processes.
    Problem2:After a while, this program will abort by error
    File "C:\Python25\li b\wmi.py", line 397, in __call__
    handle_com_erro r (error_info)
    File "C:\Python25\li b\wmi.py", line 190, in handle_com_erro r raise
    x_wmi, "\n".join (exception_stri ng)
    UnicodeDecodeEr ror: '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.Thre ad):
    def __init__ (self):
    threading.Threa d.__init__ (self)
    def run (self):
    print 'In Another Thread...'
    pythoncom.CoIni tialize ()
    dom1 = parse('processT erminateList.xm l')
    config_element = dom1.getElement sByTagName("pro cessTerminateLi st")
    [0]
    servers = config_element. getElementsByTa gName("processN ame")
    try:
    c = wmi.WMI ()
    for process in c.Win32_Process ():
    for server in servers:
    if process.name == getText(server. childNodes):
    process.Termina te()
    print process.name
    process_watcher = c.Win32_Process .watch_for("cre ation")
    while True:
    new_process = process_watcher ()
    name = new_process.Cap tion
    print name
    for server in servers:
    if name == getText(server. childNodes):
    new_process.Ter minate()
    finally:
    pythoncom.CoUni nitialize ()
    def getText(nodelis t):
    rc = ""
    for node in nodelist:
    if node.nodeType == node.TEXT_NODE:
    rc = rc + node.data
    return rc
    >
    if __name__ == '__main__':
    Info().start()
    ------------------------------------------------------
    processTerminat eList.xml-----------------------------------------------------------------------
    <?xml version="1.0" encoding="utf-8"?>
    <processTermina teList>
    <processName>sc rcons.exe</processName>
    <processName>TX Platform.exe</processName>
    <processName>md m.exe</processName>
    <processName>FN PLicensingServi ce.exe</processName>
    <processName>no tepad.exe</processName>
    <processName>ue dit32.exe</processName>
    </processTerminat eList>
    You should probably post this to comp.python.win dows. Tim Golden (author of WMI
    interface) monitors that list religously (thanks Tim).

    -Larry

    Comment

    • patrol

      #3
      Re: About wmi

      On 7ÔÂ13ÈÕ, ÏÂÎç10ʱ26·Ö, Larry Bates <larry.ba...@we bsafe.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.ex e" and
      "FNPLicensingSe rvice.exe",whic h are system processes.
      Problem2:After a while, this program will abort by error
      File "C:\Python25\li b\wmi.py", line 397, in __call__
      handle_com_erro r (error_info)
      File "C:\Python25\li b\wmi.py", line 190, in handle_com_erro r raise
      x_wmi, "\n".join (exception_stri ng)
      UnicodeDecodeEr ror: '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.Thre ad):
      def __init__ (self):
      threading.Threa d.__init__ (self)
      def run (self):
      print 'In Another Thread...'
      pythoncom.CoIni tialize ()
      dom1 = parse('processT erminateList.xm l')
      config_element = dom1.getElement sByTagName("pro cessTerminateLi st")
      [0]
      servers = config_element. getElementsByTa gName("processN ame")
      try:
      c = wmi.WMI ()
      for process in c.Win32_Process ():
      for server in servers:
      if process.name == getText(server. childNodes):
      process.Termina te()
      print process.name
      process_watcher = c.Win32_Process .watch_for("cre ation")
      while True:
      new_process = process_watcher ()
      name = new_process.Cap tion
      print name
      for server in servers:
      if name == getText(server. childNodes):
      new_process.Ter minate()
      finally:
      pythoncom.CoUni nitialize ()
      def getText(nodelis t):
      rc = ""
      for node in nodelist:
      if node.nodeType == node.TEXT_NODE:
      rc = rc + node.data
      return rc
      >
      if __name__ == '__main__':
      Info().start()
      ------------------------------------------------------
      processTerminat eList.xml------------------------------------------------------------------------
      <?xml version="1.0" encoding="utf-8"?>
      <processTermina teList>
      <processName>sc rcons.exe</processName>
      <processName>TX Platform.exe</processName>
      <processName>md m.exe</processName>
      <processName>FN PLicensingServi ce.exe</processName>
      <processName>no tepad.exe</processName>
      <processName>ue dit32.exe</processName>
      </processTerminat eList>
      >
      You should probably post this to comp.python.win dows. Tim Golden (authorof WMI
      interface) monitors that list religously (thanks Tim).
      >
      -Larry- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
      >
      - ÏÔʾÒýÓõÄÎÄ×Ö -
      I cannot find comp.python.win dows.What's the URL?

      Comment

      • Larry Bates

        #4
        Re: About wmi

        patrol wrote:
        On 7ÔÂ13ÈÕ, ÏÂÎç10ʱ26·Ö, Larry Bates <larry.ba...@we bsafe.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.ex e" and
        >>"FNPLicensing Service.exe",wh ich are system processes.
        >>Problem2:Afte r a while, this program will abort by error
        >> File "C:\Python25\li b\wmi.py", line 397, in __call__
        >>handle_com_er ror (error_info)
        >> File "C:\Python25\li b\wmi.py", line 190, in handle_com_erro r raise
        >>x_wmi, "\n".join (exception_stri ng)
        >>UnicodeDecode Error: '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.Thre ad):
        >> def __init__ (self):
        >> threading.Threa d.__init__ (self)
        >> def run (self):
        >> print 'In Another Thread...'
        >> pythoncom.CoIni tialize ()
        >> dom1 = parse('processT erminateList.xm l')
        >> config_element = dom1.getElement sByTagName("pro cessTerminateLi st")
        >>[0]
        >> servers = config_element. getElementsByTa gName("processN ame")
        >> try:
        >> c = wmi.WMI ()
        >> for process in c.Win32_Process ():
        >> for server in servers:
        >> if process.name == getText(server. childNodes):
        >> process.Termina te()
        >> print process.name
        >> process_watcher = c.Win32_Process .watch_for("cre ation")
        >> while True:
        >> new_process = process_watcher ()
        >> name = new_process.Cap tion
        >> print name
        >> for server in servers:
        >> if name == getText(server. childNodes):
        >> new_process.Ter minate()
        >> finally:
        >> pythoncom.CoUni nitialize ()
        >>def getText(nodelis t):
        >> rc = ""
        >> for node in nodelist:
        >> if node.nodeType == node.TEXT_NODE:
        >> rc = rc + node.data
        >> return rc
        >>if __name__ == '__main__':
        >> Info().start()
        >>------------------------------------------------------
        >>processTermin ateList.xml------------------------------------------------------------------------
        >><?xml version="1.0" encoding="utf-8"?>
        >><processTermi nateList>
        >> <processName>sc rcons.exe</processName>
        >> <processName>TX Platform.exe</processName>
        >> <processName>md m.exe</processName>
        >> <processName>FN PLicensingServi ce.exe</processName>
        >> <processName>no tepad.exe</processName>
        >> <processName>ue dit32.exe</processName>
        >></processTerminat eList>
        >You should probably post this to comp.python.win dows. Tim Golden (author of WMI
        >interface) monitors that list religously (thanks Tim).
        >>
        >-Larry- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
        >>
        >- ÏÔʾÒýÓõÄÎÄ×Ö -
        >
        I cannot find comp.python.win dows.What's the URL?


        -Larry

        Comment

        • patrol

          #5
          Re: About wmi

          On 7ÔÂ14ÈÕ, ÏÂÎç12ʱ29·Ö, Larry Bates <larry.ba...@we bsafe.com`wrote :
          patrol wrote:
          On 7ÔÂ13ÈÕ, ÏÂÎç10ʱ26·Ö, Larry Bates <larry.ba....@w ebsafe.com`wrot e:
          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.ex e" and
          >"FNPLicensingS ervice.exe",whi ch are system processes.
          >Problem2:Aft er a while, this program will abort by error
          > File "C:\Python25\li b\wmi.py", line 397, in __call__
          >handle_com_err or (error_info)
          > File "C:\Python25\li b\wmi.py", line 190, in handle_com_erro r raise
          >x_wmi, "\n".join (exception_stri ng)
          >UnicodeDecodeE rror: '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.Thre ad):
          > def __init__ (self):
          > threading.Threa d.__init__ (self)
          > def run (self):
          > print 'In Another Thread...'
          > pythoncom.CoIni tialize ()
          > dom1 = parse('processT erminateList.xm l')
          > config_element = dom1.getElement sByTagName("pro cessTerminateLi st")
          >[0]
          > servers = config_element. getElementsByTa gName("processN ame")
          > try:
          > c = wmi.WMI ()
          > for process in c.Win32_Process ():
          > for server in servers:
          > if process.name == getText(server. childNodes):
          > process.Termina te()
          > print process.name
          > process_watcher = c.Win32_Process .watch_for("cre ation")
          > while True:
          > new_process = process_watcher ()
          > name = new_process.Cap tion
          > print name
          > for server in servers:
          > if name == getText(server. childNodes):
          > new_process.Ter minate()
          > finally:
          > pythoncom.CoUni nitialize ()
          >def getText(nodelis t):
          > rc = ""
          > for node in nodelist:
          > if node.nodeType == node.TEXT_NODE:
          > rc = rc + node.data
          > return rc
          >if __name__ == '__main__':
          > Info().start()
          >------------------------------------------------------
          >processTermina teList.xml-------------------------------------------------------------------------
          ><?xml version="1.0" encoding="utf-8"?>
          ><processTermin ateList>
          > <processName>sc rcons.exe</processName>
          > <processName>TX Platform.exe</processName>
          > <processName>md m.exe</processName>
          > <processName>FN PLicensingServi ce.exe</processName>
          > <processName>no tepad.exe</processName>
          > <processName>ue dit32.exe</processName>
          ></processTerminat eList>
          You should probably post this to comp.python.win dows. Tim Golden (author of WMI
          interface) monitors that list religously (thanks Tim).
          >
          -Larry- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
          >
          - ÏÔʾÒýÓõÄÎÄ×Ö -
          >
          I cannot find comp.python.win dows.What's the URL?
          >

          >
          -Larry- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
          >
          - ÏÔʾÒýÓõÄÎÄ×Ö -
          Thanks

          Comment

          • Tim Golden

            #6
            Re: About wmi

            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.ex e" and
            >"FNPLicensingS ervice.exe",whi ch are system processes.
            >Problem2:Aft er a while, this program will abort by error
            > File "C:\Python25\li b\wmi.py", line 397, in __call__
            >handle_com_err or (error_info)
            > File "C:\Python25\li b\wmi.py", line 190, in handle_com_erro r raise
            >x_wmi, "\n".join (exception_stri ng)
            >UnicodeDecodeE rror: '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.win dows. 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

            Comment

            • patrol

              #7
              Re: About wmi

              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("cre ation")
              while True:
              new_process = process_watcher ()
              if new_process.Cap tion == 'notepad.exe':
              print "start killing.."
              sleep(5)
              result = new_process.ter minate()
              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.

              Comment

              • patrol

                #8
                Re: About wmi


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

                Situation (2):
                result = new_process.ter minate()
                File "C:\Python25\li b\wmi.py", line 494, in __getattr__
                handle_com_erro r (error_info)
                File "C:\Python25\li b\wmi.py", line 190, in handle_com_erro r
                raise x_wmi, "\n".join (exception_stri ng)
                UnicodeDecodeEr ror: 'ascii' codec can't decode byte 0xb7 in position
                14: ordinal
                not in range(128)

                BTW, My windows' languange is Chinese.

                Comment

                • Tim Golden

                  #9
                  Re: About wmi

                  patrol wrote:
                  Situation (2):
                  result = new_process.ter minate()
                  File "C:\Python25\li b\wmi.py", line 494, in __getattr__
                  handle_com_erro r (error_info)
                  File "C:\Python25\li b\wmi.py", line 190, in handle_com_erro r
                  raise x_wmi, "\n".join (exception_stri ng)
                  UnicodeDecodeEr ror: '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:



                  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

                  Comment

                  • Tim Golden

                    #10
                    Re: About wmi

                    patrol wrote:
                    Situation (1):
                    result = new_process.ter minate()
                    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.Ter minate ()

                    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

                    Comment

                    • patrol

                      #11
                      Re: About wmi



                      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

                      Comment

                      • patrol

                        #12
                        Re: About wmi


                        At any rate, try using:
                        >
                        result, = new_process.Ter minate ()
                        Windows is sometime case insensitive,but the python is case sensitive.
                        I also encountered this kind of problems.

                        Thanks for Tim's help.

                        Patrol

                        Comment

                        • Tim Golden

                          #13
                          Re: About wmi

                          patrol wrote:
                          >
                          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 UnicodeDecodeEr ror.
                          #
                          </code>

                          Thanks

                          TJG

                          Comment

                          • patrol

                            #14
                            Re: About wmi

                            On 7ÔÂ16ÈÕ, ÏÂÎç3ʱ29·Ö, Tim Golden <m...@timgolden ..me.ukwrote:
                            patrol wrote:>
                            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 UnicodeDecodeEr ror.
                            #
                            </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\li b\wmi.py", line 1199, in connect
                            handle_com_erro r (error_info)
                            File "C:\Python25\li b\wmi.py", line 184, in handle_com_erro r
                            exception_strin g = [u"%s - %s" % (hex (hresult_code),
                            hresult_name)]
                            UnicodeDecodeEr ror: 'ascii' codec can't decode byte 0xb7 in position
                            4: ordinal not in range(128)

                            Patrol

                            Comment

                            • Tim Golden

                              #15
                              Re: About wmi

                              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\li b\wmi.py", line 1199, in connect
                              handle_com_erro r (error_info)
                              File "C:\Python25\li b\wmi.py", line 184, in handle_com_erro r
                              exception_strin g = [u"%s - %s" % (hex (hresult_code),
                              hresult_name)]
                              UnicodeDecodeEr ror: '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_e rror, info:
                              for i in info:
                              print repr (i)

                              </code>

                              Thanks
                              TJG

                              Comment

                              Working...