perfmon.py 1.0 KB

123456789101112131415161718192021222324252627
  1. """A COM Server which exposes the NT Performance monitor in a very rudimentary way
  2. Usage from VB:
  3. set ob = CreateObject("Python.PerfmonQuery")
  4. freeBytes = ob.Query("Memory", "Available Bytes")
  5. """
  6. from win32com.server import exception, register
  7. import pythoncom, win32pdhutil, winerror
  8. class PerfMonQuery:
  9. _reg_verprogid_ = "Python.PerfmonQuery.1"
  10. _reg_progid_ = "Python.PerfmonQuery"
  11. _reg_desc_ = "Python Performance Monitor query object"
  12. _reg_clsid_ = "{64cef7a0-8ece-11d1-a65a-00aa00125a98}"
  13. _reg_class_spec_ = "win32com.servers.perfmon.PerfMonQuery"
  14. _public_methods_ = [ 'Query' ]
  15. def Query(self, object, counter, instance = None, machine = None):
  16. try:
  17. return win32pdhutil.GetPerformanceAttributes(object, counter, instance, machine=machine)
  18. except win32pdhutil.error as exc:
  19. raise exception.Exception(desc=exc.strerror)
  20. except TypeError as desc:
  21. raise exception.Exception(desc=desc,scode=winerror.DISP_E_TYPEMISMATCH)
  22. if __name__=='__main__':
  23. print("Registering COM server...")
  24. register.UseCommandLine(PerfMonQuery)