nhomc
asked on
How to use delphi to shutdown My computer?
I want to make a delphi program.When I click a button,
it will shut down my computer automatically.
it will shut down my computer automatically.
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
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
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
"By Barry Brannan. Non-visual component that lets you reboot/ shutdown/ logoff your system. "
-Torry's Delphi page
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
Greedy is totally correct there...I personally would use ExitwindowsEx(...,0)
Like so...
ExitWindowsEx(EWX_SHUTDOWN
ExitWindowsEx(EWX_LOGOFF, 0); to Logoff user
ExitWindowsEx(EWX_POWEROFF
ExitWindowsEx(EWX_REBOOT, 0); to reboot the system
ExitWindowsEx(EWX_FORCE, 0); to force shutdown no matter what
anyways
Later
BoRiS
Well, I guess the components you see work the same way...
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_REB OOT 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('Sample s', [TWinReboot]);
end;
function TWinReboot.SetPrivilege(pr ivilegeNam e: string; enable: boolean): boolean;
var
tpPrev,
tp : TTokenPrivileges;
token : THandle;
dwRetLen : DWord;
begin
result := False;
OpenProcessToken(GetCurren tProcess, 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].Attribute s := SE_PRIVILEGE_ENABLED
else
tp.Privileges[0].Attribute s := 0;
dwRetLen := 0;
result := AdjustTokenPrivileges(toke n, False, tp, SizeOf(tpPrev), tpPrev, dwRetLen);
end;
CloseHandle(token);
end;
function TWinReboot.WinExit(flags: integer): boolean;
begin
Result := True;
SetPrivilege('SeShutdownPr ivilege', true);
if not ExitWindowsEx(flags, 0) then
Result := False;
SetPrivilege('SeShutdownPr ivilege', False);
end;
end.
(* 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_REB
*)
interface
uses
Windows, Classes;
type
TWinReboot = class(TComponent)
private
function SetPrivilege(privilegeName
public
function WinExit(flags: integer): boolean;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Sample
end;
function TWinReboot.SetPrivilege(pr
var
tpPrev,
tp : TTokenPrivileges;
token : THandle;
dwRetLen : DWord;
begin
result := False;
OpenProcessToken(GetCurren
tp.PrivilegeCount := 1;
if LookupPrivilegeValue(nil, pchar(privilegeName), tp.Privileges[0].LUID) then
begin
if enable then
tp.Privileges[0].Attribute
else
tp.Privileges[0].Attribute
dwRetLen := 0;
result := AdjustTokenPrivileges(toke
end;
CloseHandle(token);
end;
function TWinReboot.WinExit(flags: integer): boolean;
begin
Result := True;
SetPrivilege('SeShutdownPr
if not ExitWindowsEx(flags, 0) then
Result := False;
SetPrivilege('SeShutdownPr
end;
end.
Search in Win32.hlp for:
1. AdjustTokenPrivileges
2. ExitWindowsEx
1. AdjustTokenPrivileges
2. ExitWindowsEx
ASKER
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?
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?
ASKER
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?
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?
ASKER
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?
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?
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
To shutdown windows 95 etc. use the following...
ExitWindowsEx(EWX_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
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.
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.
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...
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...
ASKER
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?
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?
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(GetCurren tProcess() ,TOKEN_ADJ UST_PRIVIL EGES+TOKEN _QUERY,Pro cessHandle ) then Exit;
LookupPrivilegeValue('', 'SeShutdownPrivilege', TokenPrivileges^.Privilege s[0].Luid) ;
TokenPrivileges^.Privilege Count:=1;
TokenPrivileges^.Privilege s[0].Attri butes:=SE_ PRIVILEGE_ ENABLED;
tmp:=0;
PreviosPrivileges:=nil;
AdjustTokenPrivileges(Proc essHandle, False, TokenPrivileges^, 0,PreviosPrivileges^ ,tmp);
if not GetLastError()=ERROR_SUCCE SS then Exit;
if not InitiateSystemShutdown('', '', 0, True, True) then Exit;
TokenPrivileges.Privileges [0].Attrib utes:=0;
AdjustTokenPrivileges(Proc essHandle, False, TokenPrivileges^, 0,PreviosPrivileges^ ,tmp);
end;
procedure TForm1.Button1Click(Sender
var
TokenPrivileges, PreviosPrivileges: ^TTokenPrivileges;
ProcessHandle: THandle;
tmp: Integer;
begin
if not OpenProcessToken(GetCurren
LookupPrivilegeValue('', 'SeShutdownPrivilege', TokenPrivileges^.Privilege
TokenPrivileges^.Privilege
TokenPrivileges^.Privilege
tmp:=0;
PreviosPrivileges:=nil;
AdjustTokenPrivileges(Proc
if not GetLastError()=ERROR_SUCCE
if not InitiateSystemShutdown('',
TokenPrivileges.Privileges
AdjustTokenPrivileges(Proc
end;
ASKER
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
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
ASKER
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
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
ASKER
ronit:
I use your program in win95,
but it exit in the following line:
if not OpenProcessToken(GetCurren tProcess() ,TOKEN_ADJ UST_PRIVIL EGES+TOKEN _QUERY,Pro cessHandle ) then Exit;
so I can conclude that OpenProcessToken returns false.
I use your program in win95,
but it exit in the following line:
if not OpenProcessToken(GetCurren
so I can conclude that OpenProcessToken returns false.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
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!
http://www.inprise.com/devsupport/delphi/ti_list/TI2856.html