Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

shutdown a win 2000 system

hi, is there any way to (by code) shutdown a windows system BUT with a delay? pretty much like shutdown does in Linux

something like i'd like to shutdown the machine in 10 min, so i send 10 min (or the sec.) as a parameter.

it's going to be a quick console application with no timer in it.

thanks!
0
urif
Asked:
urif
1 Solution
 
AvonWyssCommented:
There is, but I don't have the correct API calls at hand. However, you migh want to look at the freeware console application PSSHUTDOWN from www.sysinternals.com which does EXACTLY that - even allowing you to shutdown remote machines.
0
 
alanwhincupCommented:
0
 
alanwhincupCommented:
If you don't want to use a timer you could just use the Sleep() function in the SysUtils unit to delay your program for a certain ammount of time. This won't allow your program to process any messages during the execution of the Sleep() function. I sometimes use the below function if i want to process messages during a delayed time period:

procedure Delay(Ms : Longint);
var
  TickCount : LongInt;
begin
  TickCount := GetTickCount;
  while GetTickCount - TickCount < Ms do
    Application.ProcessMessages;
end;
0
 
urifAuthor Commented:
what i'm looking for is actually how to implement the call to InitiateSystemShutdown().

i tried the code in swissdelphicenter but it just crashes (on a win2k machine running delphi 5)

any ideas on how to implement this API correctly?

thanks
0
 
CrazyOneCommented:
Well I took some of the code on the swissdelphicenter and reworked it a little bit and works for me on Win2000. This is just raw code so you will need to implement your timing sequence.

procedure TForm1.Button1Click(Sender: TObject);
var
  rl: Cardinal;
  hToken: Cardinal;
  tkp: TOKEN_PRIVILEGES;

begin
  if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,
  hToken) then
    ShowMessage('Cannot open process token.')
  else begin
    if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid) then begin
      tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
      tkp.PrivilegeCount           := 1;
      AdjustTokenPrivileges(hToken, False, tkp, 0, nil, rl);
      if GetLastError <> ERROR_SUCCESS then
        ShowMessage('Error adjusting process privileges.');
    end
    else
      ShowMessage('Cannot find privilege value.');
  end;

  InitiateSystemShutdown(nil, nil, 0, True, True);

end;


The Crazy One
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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