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)
yingkitAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.