Solved

shutdown a win 2000 system

Posted on 2002-05-26
5
172 Views
Last Modified: 2010-04-04
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
Comment
Question by:urif
5 Comments
 
LVL 14

Expert Comment

by:AvonWyss
Comment Utility
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
 
LVL 5

Expert Comment

by:alanwhincup
Comment Utility
0
 
LVL 5

Expert Comment

by:alanwhincup
Comment Utility
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
 

Author Comment

by:urif
Comment Utility
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
 
LVL 44

Accepted Solution

by:
CrazyOne earned 80 total points
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now