Last active
January 6, 2017 01:41
-
-
Save gbrayut/0271db792e297cda7bc6d5dff237654b to your computer and use it in GitHub Desktop.
Python testing WMI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pip install comtypes wmi | |
import sys, os, traceback | |
sys.coinit_flags = 0 # sets pythoncom.COINIT_MULTITHREADED = 0 | |
import comtypes.client # http://starship.python.net/crew/theller/comtypes/ | |
from comtypes.automation import IDispatch, IEnumVARIANT | |
import wmi | |
#Use help(...) and dir(...) on any object/class to see more details | |
#These are already set on the main thread by sys package, but need to call if we use other threads | |
#comtypes.CoInitializeEx(comtypes.COINIT_MULTITHREADED) | |
def GetMemoryUsageMB(): | |
w = wmi.WMI('.') | |
result = w.query("SELECT WorkingSetPrivate FROM Win32_PerfRawData_PerfProc_Process WHERE IDProcess="+str(os.getpid())) | |
subset = result[0] | |
return float(subset.WorkingSetPrivate)/1024./1024. | |
print "Start %.2fMB" % GetMemoryUsageMB() | |
progid = "WbemScripting.SWbemLocator" | |
clsid = comtypes.GUID.from_progid(progid) | |
clsctx = comtypes.CLSCTX_INPROC_SERVER | comtypes.CLSCTX_LOCAL_SERVER | comtypes.CLSCTX_REMOTE_SERVER #https://github.com/go-ole/go-ole/blob/7dfdcf409020452e29b4babcbb22f984d2aa308a/constants.go#L11 | |
try: | |
i = 0 | |
while i < 100000: | |
i += 1 | |
if (i % 1000 == 0): | |
print "%7d %.1fMB" % (i,GetMemoryUsageMB()) | |
comtypes.CoInitializeEx(comtypes.COINIT_MULTITHREADED) #https://github.com/enthought/comtypes/blob/d3fbdd0c2933c9671e80d7407a0320925abc8bc5/comtypes/__init__.py#L156 | |
#com_object = comtypes.client.CreateObject(progid) #https://github.com/enthought/comtypes/blob/d3fbdd0c2933c9671e80d7407a0320925abc8bc5/comtypes/client/__init__.py#L205 | |
com_object = comtypes.CoCreateInstance(clsid, clsctx=clsctx, interface=None) # https://github.com/enthought/comtypes/blob/d3fbdd0c2933c9671e80d7407a0320925abc8bc5/comtypes/__init__.py#L1215 | |
iface = com_object.QueryInterface(comtypes.automation.IDispatch) | |
#iface.Invoke() #https://github.com/enthought/comtypes/blob/d3fbdd0c2933c9671e80d7407a0320925abc8bc5/comtypes/automation.py#L732 | |
del iface #calls release https://github.com/enthought/comtypes/blob/d3fbdd0c2933c9671e80d7407a0320925abc8bc5/comtypes/__init__.py#L905 | |
del com_object | |
comtypes.CoUninitialize() | |
except Exception, e: | |
print 'Exception: '+ str(e) | |
print traceback.format_exc() | |
''' | |
for key in dir(iface): | |
method = getattr(iface,key) | |
if str(type(method)) == "<type 'instance'>": | |
print key | |
for sub_method in dir(method): | |
if not sub_method.startswith("_") and not "clsid" in sub_method.lower(): | |
print "\t"+sub_method | |
else: | |
print "\t",key,'=',method | |
''' | |
print "End %.2fMB" % GetMemoryUsageMB() | |
#comtypes calls CoUninitialize at shutdown https://github.com/enthought/comtypes/blob/d3fbdd0c2933c9671e80d7407a0320925abc8bc5/comtypes/__init__.py#L175 | |
#comtypes.CoUninitialize() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output on a Windows 10 system: