Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I am facing issue with logoff/logout/signout user trough the window service. I have tried 4 different ways but its not working. It is working fine when I am debugging the service but when I am creating a setup of service and running it then its not working.

Its very urgent, please help me!!

What I have tried:

Process.Start("shutdown", "-l");


ProcessStartInfo info = new ProcessStartInfo();
                info.FileName = "shutdown";
                info.Arguments = "-l";
info.WindowStyle = ProcessWindowStyle.Hidden;
                Process p = Process.Start(info);
                p.WaitForExit();


if (!WTSDisconnectSession(WTS_CURRENT_SERVER_HANDLE, 
                                 WTS_CURRENT_SESSION, false))
                                throw new Win32Exception();


ExitWindowsEx(0, 0);
Posted
Updated 24-Jul-17 9:04am
v3
Comments
Richard MacCutchan 21-Jul-17 6:49am    
Doesn't look urgent to me.
Athos7 24-Jul-17 14:12pm    
The problem is likely that while you are debugging this, it is running in the user context (you in this case) when running as a service ... its probably running as system or likewise. Hard to tell without more info.

Thx
Dave Kreskowiak 24-Jul-17 14:46pm    
No, that's exactly what's going on. Since the LocalSystem cannot log itself off, ... well it's not going to work.
Kornfeld Eliyahu Peter 24-Jul-17 14:32pm    
Check your event log - you may find the answer there...

You can't do it: the service does not run under a user account - which is why you can't display anything to a user: no user, no user interface.
So shutdown -l looks at the current user that the service operates as, doesn't find one, and does nothing.

It works in debug because debugging a service is a special case, with a lot of care needed: How to: Debug Windows Service Applications[^]

Basically, you can't force a user logoff from a service directly. If your servioce has admin rights however you can - in theory, I've not tried it - do it: c# - Logoff interactive users in Windows from a service - Stack Overflow[^]
But be careful. And either backup really well before you install the service for testign, or use a VM.
And don't bring your app anywhere near my PC! :laugh:
 
Share this answer
 
A service running as local system has the permission to do this.. 
Your task involves finding a list of logged on users : CMD
<pre>query user

Then that outputs something like this:

C++
PS C:\Users\Athos7> query user
 USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
>Athos7            console             2  Active      none   7/24/2017 2:50 PM
PS C:\Users\Athos7>


Then you can just log off that session using the id:

logoff 2


You can also use findstr to only get the line with matching users ID


Or
This guy has a pretty nice PS script to turn the properties into an object. and you could either convert it or call it from your service.
Quick-Hits: Find currently logged on users | Learn Powershell | Achieve More[^]


Good Luck
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900