andrewsmith
asked on
Forcing a shutdown
Hi,
How can I force a windows shutdown, reboot or Logoff, without asking the user if they wan't to save changes?
I have tried using exitwindowsex with the ewx_force command, but this does weird things, including loging off the current user and logging back on without a user name.
Thankyou
Andrew
How can I force a windows shutdown, reboot or Logoff, without asking the user if they wan't to save changes?
I have tried using exitwindowsex with the ewx_force command, but this does weird things, including loging off the current user and logging back on without a user name.
Thankyou
Andrew
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you are using Win NT or Win2k then you need to add shutdown privilege:
function AddPrivilege(privilegeName : string) : boolean;
var c1,c2 : cardinal;
i64 : int64;
tp1,tp2 : TTokenPrivileges;
begin
result:=false;
if OpenProcessToken(windows.G etCurrentP rocess,TOK EN_ADJUST_ PRIVILEGES or TOKEN_QUERY,c1) then
try
if LookupPrivilegeValue(nil,p char(privi legeName), i64) then begin
tp1.PrivilegeCount:=1;
tp1.Privileges[0].Luid:=i6 4;
tp1.Privileges[0].Attribut es:=0;
if AdjustTokenPrivileges(c1,f alse,tp1,s izeOf(TTok enPrivileg es),tp2,c2 ) then begin
if (c2=sizeOf(TTokenPrivilege s)) and (tp2.PrivilegeCount=1) and (tp2.Privileges[0].Luid=i6 4)
then
tp1.Privileges[0].Attribut es:=tp2.Pr ivileges[0 ].Attribut es;
tp1.Privileges[0].Attribut es:=tp1.Pr ivileges[0 ].Attribut es or SE_PRIVILEGE_ENABLED;
result:=AdjustTokenPrivile ges(c1,fal se,tp1,siz eOf(TToken Privileges ),PTokenPr ivileges(n il)^,TPCar dinal(nil) ^);
end;
end;
finally CloseHandle(c1) end;
end;
AddPrivilege('SeShutdownPr ivilege');
Madshi, did you remember this:)
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=delphi&qid=11971598
Motaz
function AddPrivilege(privilegeName
var c1,c2 : cardinal;
i64 : int64;
tp1,tp2 : TTokenPrivileges;
begin
result:=false;
if OpenProcessToken(windows.G
try
if LookupPrivilegeValue(nil,p
tp1.PrivilegeCount:=1;
tp1.Privileges[0].Luid:=i6
tp1.Privileges[0].Attribut
if AdjustTokenPrivileges(c1,f
if (c2=sizeOf(TTokenPrivilege
then
tp1.Privileges[0].Attribut
tp1.Privileges[0].Attribut
result:=AdjustTokenPrivile
end;
end;
finally CloseHandle(c1) end;
end;
AddPrivilege('SeShutdownPr
Madshi, did you remember this:)
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=delphi&qid=11971598
Motaz
Of course I do remember. BTW, in the meanwhile I was getting tired of all those privileges. Here comes the function I'm currently using. It simply enables all privileges we can get... :-)
procedure EnableAllPrivileges;
var c1, c2 : dword;
ptp : PTokenPrivileges;
i1 : integer;
begin
if OpenProcessToken(windows.G etCurrentP rocess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, c1) then
try
c2 := 0;
GetTokenInformation(c1, TokenPrivileges, nil, 0, c2);
if c2 <> 0 then begin
ptp := AllocMem(c2);
if GetTokenInformation(c1, TokenPrivileges, ptp, c2, c2) then begin
for i1 := 0 to integer(ptp^.PrivilegeCoun t) - 1 do
ptp^.Privileges[i1].Attrib utes := ptp^.Privileges[i1].Attrib utes or SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(c1, false, ptp^, c2, PTokenPrivileges(nil)^, cardinal(pointer(nil)^));
end;
FreeMem(ptp);
end;
finally CloseHandle(c1) end;
end;
Regards, Madshi.
procedure EnableAllPrivileges;
var c1, c2 : dword;
ptp : PTokenPrivileges;
i1 : integer;
begin
if OpenProcessToken(windows.G
try
c2 := 0;
GetTokenInformation(c1, TokenPrivileges, nil, 0, c2);
if c2 <> 0 then begin
ptp := AllocMem(c2);
if GetTokenInformation(c1, TokenPrivileges, ptp, c2, c2) then begin
for i1 := 0 to integer(ptp^.PrivilegeCoun
ptp^.Privileges[i1].Attrib
AdjustTokenPrivileges(c1, false, ptp^, c2, PTokenPrivileges(nil)^, cardinal(pointer(nil)^));
end;
FreeMem(ptp);
end;
finally CloseHandle(c1) end;
end;
Regards, Madshi.
use this 4 reboot:
ExitWindowsEx(EWX_REBOOT+E WX_FORCE,0 );
and this 4 shutdown:
ExitWindowsEx(EWX_SHUTDOWN +EWX_FORCE ,0);
or
ExitWindowsEx(EWX_POWEROFF +EWX_FORCE ,0);
and this 4 logoff:
ExitWindowsEx(EWX_LOGOFF+E WX_FORCE,0 );
if the function succeed it return non zero value.
in windows NT family you need to get shutdown Privileges if you have not by useing AdjustTokenPrivileges function.
ExitWindowsEx(EWX_REBOOT+E
and this 4 shutdown:
ExitWindowsEx(EWX_SHUTDOWN
or
ExitWindowsEx(EWX_POWEROFF
and this 4 logoff:
ExitWindowsEx(EWX_LOGOFF+E
if the function succeed it return non zero value.
in windows NT family you need to get shutdown Privileges if you have not by useing AdjustTokenPrivileges function.
ASKER
Thankyou, that was all I wanted to know
Andrew
Andrew
Regards, Madshi.