?
Solved

How to use delphi to shutdown My computer?

Posted on 1998-06-29
20
Medium Priority
?
1,052 Views
Last Modified: 2010-08-05
I want to make a delphi program.When I click a button,
it will shut down my computer automatically.
0
Comment
Question by:nhomc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 2
  • +4
20 Comments
 
LVL 1

Expert Comment

by:Greedy
ID: 1356419
0
 
LVL 1

Expert Comment

by:Greedy
ID: 1356420
ExitWindows(how, 0);

where how is:
EW_RESTARTWINDOWS - to restart Windows
EW_REBOOTSYSTEM - to reboot the entire system

also look at ExitWindowsEx for more options....all help is in
C:\Program Files\Borland\Delphi 3\HELP\win32.hlp
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1356421
http://bes.trendline.co.il/torry/vcl/system/wreboot.zip

"By Barry Brannan. Non-visual component that lets you reboot/ shutdown/ logoff your system. "

-Torry's Delphi page
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Expert Comment

by:BoRiS
ID: 1356422
nhomc

Greedy is totally correct there...I personally would use ExitwindowsEx(...,0)

Like so...

ExitWindowsEx(EWX_SHUTDOWN, 0); to shutdown...
ExitWindowsEx(EWX_LOGOFF, 0); to Logoff user
ExitWindowsEx(EWX_POWEROFF, 0); to shutdown and power down
ExitWindowsEx(EWX_REBOOT, 0); to reboot the system
ExitWindowsEx(EWX_FORCE, 0); to force shutdown no matter what

anyways

Later
BoRiS
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1356423
Well, I guess the components you see work the same way...
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1356424
unit WinReboot;

(* TWinReboot component (freeware)
   by Barry Brannan, September 1997
   (barrylb@poboxes.com)


   Usage:

   1. Drop TWinReboot component on a form.
   2. Call WinReboot1.WinExit(flags)

   Where flags must be one of the following:

   EWX_LOGOFF     - Shuts down processes and logs user off
   EWX_REBOOT     - Shuts down the restarts the system
   EWX_SHUTDOWN   - Shuts down system

   The following attributes may be combined (OR'd) with above flags

   EWX_POWEROFF  - shuts down system and turns off the power.
   EWX_FORCE     - forces processes to terminate.


   Example:
           WinReboot1.WinExit(EWX_REBOOT or EWX_FORCE);
*)

interface

uses
  Windows, Classes;

type
  TWinReboot = class(TComponent)
  private
    function SetPrivilege(privilegeName: string; enable: boolean): boolean;
  public
    function WinExit(flags: integer): boolean;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TWinReboot]);
end;

function TWinReboot.SetPrivilege(privilegeName: string; enable: 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(privilegeName), tp.Privileges[0].LUID) then
  begin
    if enable 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 TWinReboot.WinExit(flags: integer): boolean;
begin
  Result := True;
  SetPrivilege('SeShutdownPrivilege', true);
  if not ExitWindowsEx(flags, 0) then
      Result := False;
  SetPrivilege('SeShutdownPrivilege', False);
end;

end.
0
 

Expert Comment

by:spokraev
ID: 1356425
Search in Win32.hlp for:
1. AdjustTokenPrivileges
2. ExitWindowsEx
0
 

Author Comment

by:nhomc
ID: 1356426
Thank you for your help!
All the answer is good ,but I am a tenderfoot in delphi,
I don't know how to use AdjustTokenPrivileges,Can you tell me
detailedly?

0
 

Author Comment

by:nhomc
ID: 1356427
Thank you for your help!
All the answer is good ,but I am a tenderfoot in delphi,
I don't know how to use AdjustTokenPrivileges,Can you tell me
detailedly?

0
 

Author Comment

by:nhomc
ID: 1356428
Thank you for your help!
All the answer is good ,but I am a tenderfoot in delphi,
I don't know how to use AdjustTokenPrivileges,
Can you tell me detailedly?
0
 
LVL 4

Expert Comment

by:BoRiS
ID: 1356429
nhomc

To shutdown windows 95 etc. use the following...

ExitWindowsEx(EWX_SHUTDOWN, 0); to shutdown...

to shutdown a windows NT machine use this not AdjustTokenPrivileges...

InitiateSystemShutdown( nil, 'System will now shutdown', 2000, True, True);

were nil = local machine
'system ...' = is the message to display
2000 = is the time to show the message
True = force applications with unsaved changes flag
True = reboot after shutdown

Later
BoRiS

0
 
LVL 3

Expert Comment

by:Matvey
ID: 1356430
Whow Boris you score again... :)

As a metter of fact you I think don't have to know how to use AdjustTokenPrivileges. I can't explain you, but I think the code I've posted - the component can do.
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1356431
The code I posted is the code of a component found in Torry's Delphi page.

Here are some more like it:

http://bes.trendline.co.il/torry/vcl/system/exitwin.zip

"By Alexander Meeder. Encapsulates the Windows API ExitWindowEx, now it has to be safe for WindowsNT"

http://bes.trendline.co.il/torry/vcl/system/wcommand.zip

http://bes.trendline.co.il/torry/vcl/system/wreboot.zip

Hope that helps...
0
 

Author Comment

by:nhomc
ID: 1356432
Thank you for your help!
All the answer is good ,but I am a tenderfoot in delphi,
I don't know how to use AdjustTokenPrivileges,Can you tell me
detailedly?
 

0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1356433
Here is an example to shutdown and restart the computer(Tested with Delphi 3 on NT4):

procedure TForm1.Button1Click(Sender: TObject);
var
 TokenPrivileges, PreviosPrivileges: ^TTokenPrivileges;
 ProcessHandle: THandle;
 tmp: Integer;
begin
   if not OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES+TOKEN_QUERY,ProcessHandle)  then Exit;
   LookupPrivilegeValue('', 'SeShutdownPrivilege', TokenPrivileges^.Privileges[0].Luid);
   TokenPrivileges^.PrivilegeCount:=1;
   TokenPrivileges^.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;
   tmp:=0;
   PreviosPrivileges:=nil;
   AdjustTokenPrivileges(ProcessHandle, False, TokenPrivileges^, 0,PreviosPrivileges^ ,tmp);
   if not GetLastError()=ERROR_SUCCESS then Exit;
   if not InitiateSystemShutdown('', '', 0, True, True) then Exit;
   TokenPrivileges.Privileges[0].Attributes:=0;
   AdjustTokenPrivileges(ProcessHandle, False, TokenPrivileges^, 0,PreviosPrivileges^ ,tmp);
end;
0
 

Author Comment

by:nhomc
ID: 1356434
ronit:
  Thanks for your winnt program!
  But I want a shutdown program in win95.
  Can you give me another full win95 program just like your first one?
  Thanks a lot!
                                     nhomc
                                         

 
0
 

Author Comment

by:nhomc
ID: 1356435
ronit:
  Thanks for your winnt program!
  But I want a shutdown program in win95.
  Can you give me another full win95 program just like your first one?
  Thanks a lot!
                                     nhomc
                                         

 
0
 

Author Comment

by:nhomc
ID: 1356436
ronit:
I use your program in win95,
but it exit in the following line:

if not OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES+TOKEN_QUERY,ProcessHandle)  then Exit;

so I can conclude that OpenProcessToken returns false.

0
 
LVL 8

Accepted Solution

by:
ZifNab earned 800 total points
ID: 1356437
Hi nhomc,

here is the explanation of ExitWindowsEx(Author written below):

The ExitWindowsEx function either logs off, shuts down, or shuts down and
restarts the system. BOOL ExitWindowsEx(    UINT uFlags,      // shutdown operation
    DWORD dwReserved       // reserved   );       

ParametersuFlags
Specifies the type of shutdown. This parameter must be some combination of the following values:
Value           Meaning

EWX_FORCE      Forces processes to terminate. When this flag is set, Windows does not send the messages WM_QUERYENDSESSION and WM_ENDSESSION to the applications currently running in the system. This can cause the applications to lose data. Therefore, you should only use this flag in an emergency.

EWX_LOGOFF      Shuts down all processes running in the security context of the process that called the ExitWindowsEx function. Then it logs the user off.

EWX_POWEROFF      Shuts down the system and turns off the power. The system must support the power-off feature.Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks
section. Windows 95: Security privileges are not supported or required.
EWX_REBOOT      Shuts down the system and then restarts the system. Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section. Windows 95: Security
privileges are not supported or required.
EWX_SHUTDOWN      Shuts down the system to a point at which it is safe to turn off the power. All file buffers have been flushed to disk, and all running processes have stopped. Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks
section. Windows 95: Security privileges are not supported or required.

dwReservedReserved; this parameter is ignored.  

Return Values

If the function succeeds, the return value is TRUE.
If the function fails, the return value is FALSE. To get extended error information, call GetLastError.

Remarks
The ExitWindowsEx function returns as soon as it has initiated the shutdown. The shutdown or logoff then proceeds asynchronously.
During a shutdown or log-off operation, applications that are shut down are allowed a specific amount of time to respond to the shutdown request. If the time expires, Windows displays a dialog box that allows the user to forcibly shut down the application, to retry the shutdown, or to cancel the shutdown request. If the EWX_FORCE value is specified, Windows always
forces applications to close and does not display the dialog box.
The ExitWindowsEx function sends a separate notification message,
CTRL_SHUTDOWN_EVENT or CTRL_LOGOFF_EVENT as the situation warrants, to console processes. A console process routes these messages to its HandlerRoutine functions, which are added and removed by calls to the SetConsoleCtrlHandler function. ExitWindowsEx sends these notification messages asynchronously; thus, an application cannot assume that the console notification messages have been handled when a call to ExitWindowsEx returns.
Windows NT: To shut down or restart the system, the calling process must use the AdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME privilege. For more information about security privileges, see Privileges.
Windows 95: Security privileges are not supported or required.

See Also AdjustTokenPrivileges, ExitWindows, HandlerRoutine, SetConsoleCtrlHandler

Author: neilw@zentor.demon.co.uk  (Neil Washbrook)


So you see security privilages are not supported nor required in windows95.

ZiF.
0
 

Author Comment

by:nhomc
ID: 1356438
Thank you for all your help!
All the answer is excellent!
I made a mistake at first!
I thought it must use AdjustTokenPrivileges at win95.
So I wasted a lot of time.
I am sorry for my mistake!
Thanks a lot!

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month10 days, 4 hours left to enroll

762 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