Logoff interactive users in Windows from a service

You could use following P/Invoke calls to achieve this. Below sample works only with Admin Rights [DllImport(“wtsapi32.dll”, SetLastError = true)] static extern bool WTSLogoffSession(IntPtr hServer, int SessionId, bool bWait); [DllImport(“Wtsapi32.dll”)] static extern bool WTSQuerySessionInformation( System.IntPtr hServer, int sessionId, WTS_INFO_CLASS wtsInfoClass, out System.IntPtr ppBuffer, out uint pBytesReturned); [DllImport(“wtsapi32.dll”, SetLastError = true)] static extern IntPtr WTSOpenServer([MarshalAs(UnmanagedType.LPStr)] String … Read more

Log off user from Win XP programmatically in C#

You could P/Invoke ExitWindowsEx: http://www.pinvoke.net/default.aspx/user32/ExitWindowsEx.html Pulling it all together: using System.Runtime.InteropServices; class Class1 { [DllImport(“user32.dll”)] static extern bool ExitWindowsEx(uint uFlags, uint dwReason); [STAThread] static void Main(string[] args) { ExitWindowsEx(ExitWindows.LogOff, ShutdownReason.MajorOther | ShutdownReason.MinorOther); } } [Flags] public enum ExitWindows : uint { // ONE of the following five: LogOff = 0x00, ShutDown = 0x01, Reboot = … Read more