James
James

Reputation: 489

C# Logoff out a user via Windows Service

How do I logoff a user via a Windows Service?

I found this example here but it will not work in a service. http://www.c-sharpcorner.com/UploadFile/thiagu304/desktopfunctions02112007140806PM/desktopfunctions.aspx

Upvotes: 1

Views: 2933

Answers (2)

C Sharp Conner
C Sharp Conner

Reputation: 378

HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server --->>>> Set DWORD32 AllowRemoteRPC == 1

The Remote Procedure Call (RPC) service [RpcSs] should be running on a computer by default.

We need this enabled to use the following commands:

  • To get sessions == [qwinsta /SERVER:"XXXXXX"]
  • To end active session, use session name of "console" == [logoff "console" /SERVER:"XXXXXX"]
  • To end particular session where N is session ID == [logoff "N" /SERVER:"XXXXXX"]

I believe those commands use the functions you are trying to use behind the scenes, therefore if those commands succeed then that means the functions will succeed.

Upvotes: 0

Stephen Martin
Stephen Martin

Reputation: 9645

Check out WTSLogoffSession. It does precisely what you want and is pretty easy to call via PInvoke. To get the session ID use WTSGetActiveConsoleSessionId or WTSEnumerateSessions and WTSQuerySessionInformation.

Upvotes: 3

Related Questions