In an earlier post, “Locking A Windows PC In C# & .NET”, we looked at how to invoke the Windows API to lock the screen.

In this post, we look at something similar - how to log off a currently logged-in Windows user.

This is also achieved by invoking the Windows API function ExitWindowsEx.

This is achieved as follows using the DllImport attribute.

[DllImport("user32")]
public static extern bool ExitWindowsEx(uint uFlags, uint dwReason)

The function itself is invoked as follows:

ExitWindowsEx(0, 0);

The two parameters passed are uFlags and dwReason.

The value for uFlag, 0 is defined as follows:

Value Meaning
EWX_LOGOFF 0 Shuts down all processes running in the logon session of the process that called the ExitWindowsEx function. Then it logs the user off. This flag can be used only by processes running in an interactive user’s logon session.

The value for dwReason, 0, is defined as follows:

Value Meaning
SHTDN_REASON_MAJOR_OTHER0x00000000 Other issue

Upon invocation, this will log you off.

Be careful with this, as you need to consider what happens to any applications with open files when you log off the logged-in user.

TLDR

You can log off the current logged-in user by calling the Windows API function ExitWindowsEx

Happy hacking!