That's one of the gotcha's about Windows that I ran into. I don't know all of the technical stuff, but here's some things that I learned a few years ago from trying to automate a GUI program (with and without Perl).
- If no user is logged in at the local console or remotely via remote desktop, there is no GUI environment. That means no program windows are available to be automated.
- If the user is logged in via remote desktop and the connection is disconnected, the GUI environment goes away.
- If the user locks the session (or screensaver kicks in and requires login), the GUI environment goes away.
- If the user is logged in via remote desktop and does not have the remote desktop window as the active window, the GUI environment goes away.
The above may not be 100% accurate, but that's what I remember encountering in my attempts at automating GUI programs from a few years ago. Also, in the scenarios above where I said the "GUI goes away", any programs that were running continue to run. Windows just isn't using a GUI environment until the user is logged back in and actively using the desktop.