0

Is there a .Net API or DLL I could call to log another user off in Windows 7?

There are a couple questions on SO already, but they seem to be specific to Windows XP. I've tried them nonetheless and could not get them to work.

Such as this one, and this one.

Is this not possible in Windows 7? Or is there another API that I should be calling?

Community
  • 1
  • 1
javamonkey79
  • 17,443
  • 36
  • 114
  • 172

3 Answers3

1

Have you tried this Function? You have to list out the session info first:

[DllImport("wtsapi32.dll", SetLastError = true)]
    static extern bool WTSLogoffSession(IntPtr hServer, int SessionId, bool bWait);
Jason
  • 121
  • 8
  • How would I list out the session info? – javamonkey79 Nov 07 '11 at 06:34
  • You have to use the EnumerateSessions function, its all pretty well documented under wtsapi32.dll on pinvoke.net. http://www.pinvoke.net/default.aspx/wtsapi32.WTSLogoffSession I tried to paste a sample but it won't give me enough room. – Jason Nov 08 '11 at 02:44
  • This isn't anything that I haven't already seen. I've tried it and the code doesn't seem to work in Windows 7. Have you been able to get it to work in Windows 7? – javamonkey79 Nov 08 '11 at 19:12
  • That's what I use on Server 2008 R2, which has basically the same kernel, and Server 2003. I've used it to log off console sessions which is essentially what the Windows 7 main session is. You probably don't even need to enumerate the sessions, just do an OpenServer which gives you the IntPtr to the server, then LogoffSession using 0 as the Session ID. Session 0 should always be the console. – Jason Nov 08 '11 at 22:05
  • I don't know, I've tried this and it doesn't seem to work. I am beginning to think that it is the version of Windows that is preventing me from doing this. – javamonkey79 Nov 09 '11 at 05:23
0

Do you have FastUserSwitching turned off? I've noticed that if it is off, you can't force someone to log off by attempting to login (even as administrator). If it is off, try turning it on and see if your program works.

John
  • 6,503
  • 3
  • 37
  • 58
  • Thank you for the suggestion, but I don't have any code yet - that is what I am asking for. I've tried the items in the links I provided but they don't seem to work. – javamonkey79 Nov 08 '11 at 19:15
  • Windows 7 (at least in my environment) is very protective of the user session if Fast User Switching is turned off. The only way to log someone off is to HOLD the power button until it powers off :O. Do you have Fast User Switching turned on? – John Nov 08 '11 at 20:32
  • Yeah, I suppose I do. I guess it takes a registry hack to change it :( see [here](http://answers.microsoft.com/en-us/windows/forum/windows_7-security/windows-7-home-premium-fast-user-switching/643ea7cd-d98a-42be-b8d4-ba37801a9ea8) – javamonkey79 Nov 09 '11 at 05:22
0

If all else fails, you could just call logoff.exe http://ss64.com/nt/logoff.html

but I agree that seems like a dirty quickfix

Ron Sijm
  • 8,490
  • 2
  • 31
  • 48
  • How is this any different from the second link I sent out? – javamonkey79 Nov 07 '11 at 06:34
  • I don't think logoff works in Windows 7. I get this: `'logoff' is not recognized as an internal or external command, operable program or batch file.` – javamonkey79 Nov 08 '11 at 19:14
  • @javamoney79 - weird; I'm using windows 7 (x64) too. When I go to "C:\Windows\System32" I do have a file logoff.exe, which says is the "Session Logoff Utility". – Ron Sijm Nov 08 '11 at 19:24
  • I do not have it with Windows 7 Home Premium. I bet you have Ultimate or something else... – javamonkey79 Nov 09 '11 at 05:16