tags:

views:

151

answers:

5

I'm using C# Windows Forms. Is there an easy way to check if there are no users logged in?

Below is the code I've used for XP, which doesn't seem to work under windows 7 when a user is logged out.

    private bool LoggedInUser()
    {
        bool loggedIn = true;
        Process[] aProc = Process.GetProcessesByName("explorer");
        if (aProc.Length <= 0)
        {
            loggedIn = false;
        }
        return loggedIn;
    }

Thanks :)

[edit]: The program is executed by the task scheduler, not by a user.

A: 

You can p/invoke the Win32 API WTSEnumerateSessions / WTSEnumerateSessionsEx to get a listing of all of the current sessions. I think you would only have a session 0 if there are no logged on users on Vista and Win7. A better way to check via this method though is to check the username parameter pUserName from the Ex variant.

Alternatively (and probably more easily) you could use WTSGetActiveConsoleSessionId and check for a return result or 0xFFFFFFFF.

//Supported for XP and up
[DllImport("kernel32.dll")]
private static extern uint WTSGetActiveConsoleSessionId();

public static bool HasLoggedInUsers()
{
   uint result = WTSGetActiveConsoleSessionId();
   return result != 0xFFFFFFFF;
}
Brian R. Bondy
Hmm...this one didn't work. The app is executed from the task scheduler with a user account while nobody is logged in. I had it write the boolean output to a text file, and it always shows as true (somebody is logged in). Ideas?
Force Flow
@Force Flow: Please print out the active console session id. What is it?
Brian R. Bondy
It seems to be spitting out the number 2.
Force Flow
@Force then you probably have a session which has a logged on user.
Brian R. Bondy
@Force: Is your program running as a service? Does that service have allow service to interact with desktop enabled? Does it provide a username and a password?
Brian R. Bondy
Nope, not running as a service.
Force Flow
A: 

Enumerating for the explorer.exe was always flawed, not only in Windows 7. An user session does not necessarily require explorer.exe to be the default shell, so the process named explorer may well be not runnig. And enumerating processes require privileges a normal C# Windows forms application does not held.

The proper way to find user session is to enumerate them via LsaEnumerateLogonSessions. Another alternative is to use WMI and interogate the Win32_LogonSession class.

Remus Rusanu
What about fast-switching user - would that imply there's two explorer.exe processes?
tommieb75
How would I get started using LsaEnumerateLogonSessions?
Force Flow
A: 

In theory you cannot run a program like that because the program is running and activated while a user is logged in!

Think about the old Windows 2000/XP logging process which uses GINA (Graphical Identification and authentication) in which when the login credentials are verified, it spawns the process Explorer which in turn activates startup and run-once applications upon logging in.

The Login process is now updated and revamped. The only way you can find out about the current sessions is in the unmanaged code perspective, a process that runs before the login credentials dialog box....

tommieb75
A: 

You can use the Cassia library to see the active logon sessions.

SLaks
A: 

What about this

Environment.UserName

saurabh
Force Flow
Better try it once, I think it will show the user name who is logged on the system, let me know the outcome
saurabh
This will return the user which is running the program. It won't help.
SLaks