Log out of your local Windows user using C #

I am currently working on parental control software. This software needs to log out and then lock the account so they can't log in unless the parent / admin has indicated that they can.

I've tried several things so far, like checking the boxes in the user account indicating that it's disabled. This removes it completely from the login screen. From what I figured out is that if a user account is logged in, it doesn't apply the ADS_Disable flag. I also tried to find resources to log out of another account, but I can only find information about logging out of the account that the logout command is running on. For example Pinvoke or call LOGOUT.EXE directly .

I found the LSAUser resource and found that there might be something in there. I am doing this project for a school and I need a little guidance. Since there is such a rare amount of information on this, is there a better way to do what I want to do? Or is there a reason why I shouldn't be doing this? Any alternatives?

+3


source to share


3 answers


Use WTSDisconnectSession()

the Windows API. See the article here .

using System;
using System.Runtime.InteropServices;
using System.ComponentModel;

class Program
{
  [DllImport("wtsapi32.dll", SetLastError = true)]
  static extern bool WTSDisconnectSession(IntPtr hServer, int sessionId, bool bWait);

  const int WTS_CURRENT_SESSION = -1;
  static readonly IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero;

  static void Main(string[] args)
  {
    if (!WTSDisconnectSession(WTS_CURRENT_SERVER_HANDLE,
         WTS_CURRENT_SESSION, false))
      throw new Win32Exception();
  }
}

      



Even without Remote Desktop, it will disconnect the current user and go to the login screen. Processes will run in the background. After re-entering the system, the running programs will be displayed as they were before the disconnection.

+8


source


  [DllImport("wtsapi32.dll", SetLastError = true)]
  static extern bool WTSDisconnectSession(IntPtr hServer, int sessionId, bool bWait);

      


When using WTSDisconnectSession on a remote desktop, it is equivalent to "Close" the remote desktop window. This disconnects the Windows session, but keep the connection.

The advantage is that you can reconnect the session later by remotely logging in again.
The downside is that Windows may not be logged in by another user when the remote desktop connection is full.


To simulate Windows "Logout" use ExitWindowsEx under user32.dll

[DllImport("user32.dll", SetLastError = true)]
static extern bool ExitWindowsEx(uint uFlags, uint dwReason);

public static bool WindowsLogOff() {
  return ExitWindowsEx(0, 0);
}

      



if you want to force logout you need to add a flag EWX_FORCE

like this:

ExitWindowsEx(0 | 0x00000004, 0);

      

More on the function here: https://msdn.microsoft.com/en-us/library/windows/desktop/aa376868(v=vs.85).aspx

0


source


disabling Leng Weh Seng's answer (as I cannot comment), if you want to force logout you need to add the EWX_FORCE flag like this:

ExitWindowsEx(0 | 0x00000004, 0);

      

More on the function here: https://msdn.microsoft.com/en-us/library/windows/desktop/aa376868(v=vs.85).aspx

0


source







All Articles