views:

1145

answers:

4

I have several applications that are part of a suite of tools that various developers at our studio use. these applications are mainly command line apps that open a DOS cmd shell. These apps in turn start up a GUI application that tracks output and status (via sockets) of these command line apps.

The command line apps can be started with the user is logged in, when their workstation is locked (they fire off a batch file and then immediately lock their workstaion), and when they are logged out (via a scheduled task). The problems that I have are with the last two cases.

If any of these apps fire off when the user is locked or logged out, these command will spawn the GUI windows which tracks the output/status. That's fine, but say the user has their workstation locked -- when they unlock their workstation, the GUI isn't visible. It's running the task list, but it's not visible. The next time these users run some of our command line apps, the GUI doesn't get launched (because it's already running), but because it's not visible on the desktop, users don't see any output.

What I'm looking for is a way to tell from my command line apps if they are running behind a locked workstation or when a user is logged out (via scheduled task) -- basically are they running without a user's desktop visible. If I can tell that, then I can simply not start up our GUI and can prevent a lot of problem.

These apps that I need to test are C/C++ Windows applications.

I hope that this make sense.

+1  A: 

You might be able to use SENS (System Event Notification Services). I've never used it myself, but I'm almost positive it will do what you want: give you notification for events like logon, logoff, screen saver, etc.

I know that's pretty vague, but hopefully it will get you started. A quick google search turned up this, among others: http://discoveringdotnet.alexeyev.org/2008/02/sens-events.html

Adam Neal
+2  A: 

I found the programmatic answer that I was looking for. It has to do with stations. Apparently anything running on the desktop will run on a station with a particular name. Anything that isn't on the desktop (i.e. a process started by the task manager when logged off or on a locked workstation) will get started with a different station name. Example code:

HWINSTA dHandle = GetProcessWindowStation();
if ( GetUserObjectInformation(dHandle, UOI_NAME, nameBuffer, bufferLen, &lenNeeded) ) {
    if ( stricmp(nameBuffer, "winsta0") ) {
        // when we get here, we are not running on the real desktop
        return false;
   }
}

If you get inside the 'if' statement, then your process is not on the desktop, but running "somewhere else". I looked at the namebuffer value when not running from the desktop and the names don't mean much, but they are not WinSta0.

Link to the docs here.

Mark
A: 

I have successfully used this approach to detect whether the desktop is locked on Windows:

bool isDesktopLocked = false;
HDESK inputDesktop = OpenInputDesktop(0, FALSE,
             DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW |
             DESKTOP_ENUMERATE |  DESKTOP_SWITCHDESKTOP |
             DESKTOP_WRITEOBJECTS | DESKTOP_READOBJECTS |
             DESKTOP_WRITE);

if (NULL == inputDesktop)
{
    isDesktopLocked = true;
}
else
{
    CloseDesktop(inputDesktop);
}
Andy Stevenson