Mega Code Archive

 
Categories / Delphi / System
 

Shutdown Computer by 3 Solution

Title: Shutdown Computer by 3 Solution Question: how i can shutdown computer in windows NT family? Answer: // ALI EBRAHIMI DORCHEH (ebr_ali@yahoo.com) //------------- Solution 1----------------- procedure TForm1.Button1Click(Sender: TObject); begin ExitWindowsEx(EWX_FORCE and EWX_SHUTDOWN,0); //EWX_SHUTDOWN for shutdown //EWX_REBOOT for reboot //EWX_LOGOFF for logoff end; //------------- Solution 2----------------- run %Windir%\system32\shutdown.exe Example : procedure TForm1.Button1Click(Sender: TObject); begin WinExec('shutdown.exe -s -f -t 0' , SW_HIDE); end; //------------- Solution 3----------------- unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} function SetPrivilege1 (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 WinExit1 (iFlags: integer) : Boolean; begin result := true; if SetPrivilege1 ('SeShutdownPrivilege', true) then begin if (not ExitWindowsEx (iFlags, 0)) then begin result := False end; SetPrivilege1 ('SeShutdownPrivilege', False) end else begin result := False end; end; procedure TForm1.Button1Click(Sender: TObject); begin // 0= Logoff // 1= Shutdown WinExit1(1); end; end.