Issue while tracking application in windows

I am trying to create an mfc application(hidden application) that monitors the various applications running on the system I am using GetForgroundWindow() to find the active window but sometimes I get my own application window running in the background how I can get the running next active window in z order or previously active window

is there any other way to properly track the applications
 
Back