in reply to find active window
I think that Win32::GUI is the right way, and GetForegroundWindow() gives you the window handle of the window currently having the focus :
perl -MWin32::GUI -wle "print Win32::GUI::GetForegroundWindow()" [download]