Windows with Delphi Windows API (94) Windows Filesystem (41) Windows Forms (69) Windows Graphics (38)
Exchange Links About this site Links to us 
|
Programmatically logoff a user, shutdown or reboot
2 comments. Current rating: (1 votes). Leave comments and/ or rate it.
If your program needs to programmatically logoff a user, shutdown or reboot the computer, then you may use the following function with the appropriate parameter:
WinExit(EWX_LOGOFF); // one of these 3 only :-)
WinExit(EWX_REBOOT);
WinExit(EWX_SHUTDOWN);
See the source for the flags EWX_POWEROFF and EWX_FORCE.
The function SetPrivilege() is a necessary helper function.
The other document referenced at the top in the See-Also section is a slight modification of this code snippet, but it works basically the same way.  | |  | | function SetPrivilege (sPrivilegeName: string; bEnabled: Boolean) : Boolean;
var
TPPrev,
TP : TTokenPrivileges;
Token : THandle;
dwRetLen : DWORD;
begin
result := False;
OpenProcessToken (GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, @Token);
TP.PrivilegeCount := 1;
if LookupPrivilegeValue (nil, PChar (sPrivilegeName), TP.Privileges[0].LUID) then
begin
if bEnabled then
TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
else
TP.Privileges[0].Attributes := 0;
dwRetLen := 0;
result := AdjustTokenPrivileges (Token, False, TP, SizeOf (TPPrev), TPPrev,
dwRetLen)
end;
CloseHandle (Token)
end;
function WinExit (iFlags: integer) : Boolean;
begin
result := true;
if SetPrivilege ('SeShutdownPrivilege', true) then
begin
if (not ExitWindowsEx (iFlags, 0)) then
begin
result := False
end;
SetPrivilege ('SeShutdownPrivilege', False)
end
else
begin
result := False
end
end;
| |  | |  | You don't like the formatting? Check out SourceCoder then!
Comments:
|
|
|
|
How can you log off specific user
|
|
|
|
|
its a good code
|
|