• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 180
  • Last Modified:

Shut down AND power off

Hi,
How can I shut down AND power off a computer in Delphi?  The answer should work in Win9x and WinNT4
(D2)
0
yingkit
Asked:
yingkit
  • 3
1 Solution
 
rwilson032697Commented:
Listening
0
 
intheCommented:
you can use'

ExitWindowsEx(EWX_SHUTDOWN, 0);


procedure ExitWindowsNT(uFlags : integer);
var
  hToken : THANDLE;
  tkp, tkDumb : TTokenPrivileges;
  DumbInt : integer;
begin
  FillChar(tkp, sizeof(tkp), 0);
  // Get a token for this process
  if not (OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)) then
     raise Exception.create('OpenProcessToken failed with code '+inttostr(GetLastError));

  // Get the LUID for the Shutdown privilege
  LookupPrivilegeValue(nil, pchar('SeShutdownPrivilege'), tkp.Privileges[0].Luid);

  tkp.PrivilegeCount := 1; // one privilege to set
  tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;

  // Get the shutdown provolege for this process
  AdjustTokenPrivileges(hToken, false, tkp, sizeof(tkDumb), tkDumb, DumbInt);

  // Cannot test the return value of AdjustTokenPrivileges
  if GetLastError <> ERROR_SUCCESS then
     Raise Exception.create('AdjustTokenPrivileges failed with code '+inttostr(GetLastError));

  // shut down the system and for all applications to close
  if not ExitWindowsEx(uFlags, 0) then
     Raise Exception.create('ExitWindowsEx failed with code '+inttostr(GetLastError));
end;


Regards Barry
0
 
intheCommented:
oh missed with poweroff as well:


unit XSNT;
//  compiled with Borland Delphi 4.0  & tested on NT4.0
 
interface
 
uses Windows;
 
procedure XSForceShutdown;
procedure XSForceReboot;
 
implementation
 
type
  XSPriv = packed record
    Count :LongInt;
    Luid :Int64;
    Attr :LongInt;
  end;
 
procedure TurnOnShutDownPrivilege; var Token :Cardinal;  NewPrivilege
:_TOKEN_PRIVILEGES;  OldPrivilege :_TOKEN_PRIVILEGES;  OldLen :DWord;  Priv
:XSPriv absolute NewPrivilege; begin  OpenProcessToken(GetCurrentProcess,
TOKEN_ALL_ACCESS or TOKEN_WRITE, Token);  Priv.Count := 1;
LookupPrivilegeValue(nil, 'SeShutdownPrivilege', Priv.Luid);  Priv.Attr := SE_PRIVILEGE_ENABLED;  AdjustTokenPrivileges(Token, false, NewPrivilege, SizeOf(OldPrivilege), OldPrivilege, OldLen); end;
 
procedure XSForceShutdown;
begin
  TurnOnShutDownPrivilege;
  ExitWindowsEx(EWX_SHUTDOWN or EWX_POWEROFF or EWX_FORCE, 0);
end;
 
procedure XSForceReboot;
begin
  TurnOnShutDownPrivilege;
  ExitWindowsEx(EWX_REBOOT or EWX_FORCE, 0);
end;
 
end.
0
 
intheCommented:
Sorry to post anoter but it shows how to do both os's in same call by checking if nt is the os first:


unit ExitWin;
 
interface
 
uses
  Windows;
 
function PlatformNT : boolean;
{ Betriebssystemplattform ermitteln
  Funktionsergebnis false: Windows 95
                    true : Windows NT }
 
function ExitWindowsF(uFlags : word) : boolean;
{ Windows (95/NT) herunterfahren
  uFlags: siehe Windows-SDK ExitWindowsEx }
 
implementation
 
const
  { in Delphi nicht deklariertes }
  ANYSIZE_ARRAY    = 1;
  SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
 
var
  IsNT : boolean;
 
function PlatformNT : boolean;
begin
  PlatformNT:=IsNT;
end;
 
function ExitWindowsNT(uFlags : word) : boolean;
// Routine für Windows NT
var
  hToken : THandle;
  ptkp, ptkpold : PTokenPrivileges;
  r : dword;
begin
  // Token Handle des aktuellen Prozesses ermitteln
  if OpenProcessToken(GetCurrentProcess,
{$IFDEF DELPHI2}
    TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, @hToken) then
{$ELSE}
    TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
{$ENDIF}
  begin
    // LUID für shut down ermitteln und Privileg setzen
    GetMem(ptkp,sizeof(TTOKENPRIVILEGES) +
      (1-ANYSIZE_ARRAY) * sizeof(TLUIDANDATTRIBUTES));
    LookupPrivilegeValue(nil, SE_SHUTDOWN_NAME,
      ptkp^.Privileges[0].Luid);
    ptkp^.PrivilegeCount:=1;  // Anzahl zu setzender Privilegien     ptkp^.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;
    // Privileg für diesen Prozess setzen
    r:=0;
    ptkpold:=nil;
    if AdjustTokenPrivileges(hToken, false, ptkp^, 0, ptkpold^, r) then       ExitWindowsNT:=ExitWindowsEx(uFlags,0);
  end;
  ExitWindowsNT:=GetLastError=ERROR_SUCCESS;
end;
 
function ExitWindowsF(uFlags : word) : boolean;
// Windows (95/NT) beenden
begin
  if IsNT then ExitWindowsF:=ExitWindowsNT(uFlags)
  else ExitWindowsF:=ExitWindowsEx(uFlags,0);
end;
 
procedure CheckOS;
// Betriebssystemplattform ermitteln
var
  VerInfo : TOSVersionInfo;
begin
  IsNT:=false;
  VerInfo.dwOSVersionInfoSize:=sizeof(VerInfo);
  if (GetVersionEx(VerInfo)) then
    IsNT:=VerInfo.dwPlatformId=VER_PLATFORM_WIN32_NT;
end;
 
begin
  CheckOS;
end.
 
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now