Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi. I would like to create an application that would monitor processes that are opened. I want to use it to monitor how often do I open my mp3 files. The program would run in background and count the number of times I run each mp3 file and later I would sort my mp3 files based on this number.

I used Process Monitor to check if it can be done. When i filter the output so that it shows only my mp3 player processes and i set to see only "Process Start" operation then I can read which file was opened, for example this is the command line detail that the Process Monitor showed me files that are opend in real-time. After each file run there is a new input in Process Monitor.
See screenshot to look what I am talking about:


As you can see I could easily count the Process Monitor outputs to get the number of times each file was started. However, I don't know how it is done because Process Monitor is an .exe and i can't see inside the code.

What is the easiest way to solve my problem? In fact programming language I would like to use doesn't matter, can be C#, C++, Python.

Thank you in advance.
Posted
Updated 17-May-15 10:55am
v2

There is no "easy" way to do this. You'd have to poll the handle table of your MP3 player process every once in a while to see which files it has open.

This[^] is a little hint as to how Process Monitor does it.
 
Share this answer
 
I have come up with something on my own. It should be enough to use this library psutil in Python 2.7. My process name will be constant, because I always use the same program to run my mp3 / video files.

1. Find PID of my process
2. When I have the PID i can check what files are currently opened by my process.
3. Always remember last opened file. If the current file is different from last one, then increment current's file open count by 1.
4. Use while loop to check this in background.

Here's some small amount of code but in general the soultion is quite simple :)

Python
import psutil
process_list = psutil.pids()

for i in process_list:
    temp_process = psutil.Process(i)
    
    #if process doesn't have a name then the .name() method will cause a crash, we need to try-catch it
    try:
        if (temp_process.name()=="mpc-hc64.exe"):
            print temp_process.cmdline() #in my case opened file will be listed in the cmdline, may not always be true
            print temp_process.open_files() #this however should ALWAYS return files that are opened by current process!!
    except:
        print "no process name"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900