2

My application is running as service under Windows Server 2008. I'm looking for a way to detect an active console session. This can either be the console or a RDP session started as administrative session ("mstsc /admin").

The console session is called "Console" but I'm lost with RDP sessions. Under Windows 2003 it was easy because a console session was always running with id 0. This changed since Vista so I'm looking for another way to find out.

I've already checked the WTSxxx Win32 API but have not found what I'm looking for.

Can anybody help?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Tinus
  • 31
  • 4
  • Are you trying to determine when someone has logged into the box? – John M Feb 08 '11 at 16:52
  • This is not going to work. Google "session 0 isolation" to find out more about the reason a service can no longer interact with other sessions since Vista/Server2008. – Hans Passant Feb 08 '11 at 16:56
  • @John: Yes, I want to know when someone has logged into the box, but only when it's an administrative (console) session. I do not want to detected a standard RDP session. – Tinus Feb 09 '11 at 10:28
  • @Hans: Thanks, I know about that, I don't want to interact with the user. I only want to detect the presence of the console session. – Tinus Feb 09 '11 at 10:29

2 Answers2

1

If you're looking for the id of the session that is currently attached to the physical console, the API is WTSGetActiveConsoleSessionId

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • Thanks, that was **exactly** what I was looking for (seems I overlooked that). – Tinus Feb 09 '11 at 10:34
  • Was promising but does not work. I have a 2008R2 box with a physical monitor/keyboard but use RDP via "mstsc /admin" to start a console session. My active session id is now 2, whereas WTSGetActiveConsoleSessionId returns 1 (which is the disconnect physical session). – Tinus Feb 09 '11 at 11:00
0

One option is have your service CanHandleSessionChangeEvent set to true then implement OnSessionChange(SessionChangeDescription changeDescription) Then if ChangeDescription.Reason == SessionChangeReason.ConsoleConnect you have had someone connect to the console.

protected override void OnSessionChange(SessionChangeDescription changeDescription)
{
    if(changeDescription.Reason == SessionChangeReason.ConsoleConnect)
    {
        //use changeDescription.SessionId to find if the logged in user 
        //  to that session is an administrator.
    }
}
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431