[python-win32] How to get command line parameters of running processes via pywin32

Tim Golden mail at timgolden.me.uk
Thu Dec 6 10:59:59 CET 2007


Tim Roberts wrote:
> Patrick Li wrote:
>> I am interested in scanning the user's process table periodically and
>> getting the list of running process names as well as their command
>> line parameters. 
>>
>> For the process names, I am able to get the list of pids using
>> win32process.EnumProcesses and then use win32api.OpenProcess and
>> win32process.GetModuleFileNameEx to get the names.  However, I
>> couldn't find anywhere in the doc that tells me how to get the command
>> line parameters for them as well.
>>
>> Then I found the WMI module on
>> http://tgolden.sc.sabren.com/python/wmi_cookbook.html#running_processes
>> that seems to do the trick:
>> import wmi
>> c = wmi.WMI ()
>> for process in c.Win32_Process ():
>>   print process.CommandLine
>>   
>> Is that the most efficient way to get the command line parameters?  My
>> program will need to scan the process table quite often, so I am
>> trying to make this operation as lightweight as noticeable. 
> 
> That's the only way.  The command line is stored in the process' private
> memory; there is no API to go fetch it.
> 
> Why do you need to get this information "quite often"?  What problem are
> you solving?  What you describe is going to be a resource-intensive
> operation.

The wmi module is a bit sluggish and can be a CPU hog (especially
when running intrinsic events). There are a couple of tips here
on getting a bit of speed out of things, and if you want to come
forward with a particular bit of code I might be able to give you
some pointers:

   http://timgolden.me.uk/python/wmi-tutorial.html#speeding-things-up

TJG


More information about the python-win32 mailing list