Check if workstation locked

I'm struggling to determine in windows XP if the workstation is locked (via service).

I was using,

1
2
3
if (user32.OpenInputDesktop(0,false,0x0001)==null) {
//then we are locked
}


which worked fine when my application was running as the logged in user but it always returns a valid handle when ran by SYSTEM user.

Any help would be much appreciated.

Thanks!
Last edited on
According to numerous MS rep posts there is no way to do this in XP. In reality you just need to determine what logon is actuated on session 0 and see if they are "active" or not. This isn't an official answer but since XP updates are being phased out and this method still seems to be valid I'd say you're golden.
Thank you so much for your reply, could you please elaborate on this just in a few simple steps how I achieve this.
I'm over thinking this, the issue you're having is that service stations don't have an input desktop so that's why "OpenInputDesktop()" is always going to return 'NULL'. Have you actually tried switching your process to 'WinSta0' yet? You may need to make it into a separate application.
Not sure what you mean sorry. My application runs as a service (system user) which it is required to for other reasons. The OS is standard Windows XP and I'm trying to avoid creating any other applications etc. to implement this capability.

Thanks again for your time.
Use the function "SetProcessWindowStation()" to change the processes windows station from 'Service-0x0-3e7$' to 'Winsta0' so that it can actually interact with the desktops in that station. http://msdn.microsoft.com/en-us/library/windows/desktop/ms686232(v=vs.85).aspx

This article might also fill in a few holes for you: http://blogs.technet.com/b/askperf/archive/2007/07/24/sessions-desktops-and-windows-stations.aspx
Ok so now I have

1
2
3
4
5
6
7
8
9
10
11
// produces a pointer
pointer = User32.OpenWindowStation("winsta0", false, MAXIMUM_ALLOWED);

// this returns true so appears to work
value = User32.SetProcessWindowStation(pointer);

// correctly returns null when ran as a user and the station is locked.
// incorrectly always returns a valid handle when run as system user.
if (User32.OpenInputDesktop(0, false, 0x0001)==null) {
    // Station is locked 
}


The open window station returns a valid handle and the setprocesswindowstation returns true but I am still getting the same behaviour. When ran as the logged in user openinputdesktop return false when the station is locked and when ran as a service (system user) I always get a valid handle.
Last edited on
Now that I think about it this is because there will always be a 'winsta0' for it to connect to.

I've got to be honest. I've never liked the method that I suggested earlier, seeing if the logged on user is idle, but this one really has me stumped. The cop out answer is to leave your remote service running and have it keep a tally of the number of times it recieves the 'WM_WTSSESSION_CHANGE' message with a wParam argument of 'WTS_SESSION_LOCK' vs the number of times it recieves that message with a wParam argument of 'WTS_SESSION_UNLOCK'. But I'm sure that is not really helpful for you. Unfortunatly I don't have a better answer for you when running on XP. Here is a link to that message: http://msdn.microsoft.com/en-us/library/windows/desktop/aa383828(v=vs.85).aspx

Good luck.
Thanks so much for your help.

I managed to find something that worked in the end....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 hwnd = OpenDesktop("Default", 0, false, DESKTOP_SWITCHDESKTOP);

  if (hwnd != 0)
  {
    rtn = SwitchDesktop(hwnd);
    if (rtn == 0)
    {
      // Locked
      CloseDesktop(hwnd);
      return true;
    }
    else
    {
      // Not locked
      CloseDesktop(hwnd);
    }
  }

return false;


Thanks again!
Topic archived. No new replies allowed.