We help IT Professionals succeed at work.

Forcing a shutdown

andrewsmith
andrewsmith asked
on
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
Comment
Watch Question

Commented:
ewx_Shutdown + ewx_Force

Commented:
Motaz is right. EWX_FORCE alone is not sufficient. You need to combine it with EWX_SHUTDOWN, EWX_LOGOFF or EWX_REBOOT.

Regards, Madshi.

Commented:
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.GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,c1) then
   try
     if LookupPrivilegeValue(nil,pchar(privilegeName),i64) then begin
       tp1.PrivilegeCount:=1;
       tp1.Privileges[0].Luid:=i64;
       tp1.Privileges[0].Attributes:=0;
       if AdjustTokenPrivileges(c1,false,tp1,sizeOf(TTokenPrivileges),tp2,c2) then begin
         if (c2=sizeOf(TTokenPrivileges)) and (tp2.PrivilegeCount=1) and (tp2.Privileges[0].Luid=i64)
then
           tp1.Privileges[0].Attributes:=tp2.Privileges[0].Attributes;
         tp1.Privileges[0].Attributes:=tp1.Privileges[0].Attributes or SE_PRIVILEGE_ENABLED;
         result:=AdjustTokenPrivileges(c1,false,tp1,sizeOf(TTokenPrivileges),PTokenPrivileges(nil)^,TPCardinal(nil)^);
       end;
     end;
   finally CloseHandle(c1) end;
end;

AddPrivilege('SeShutdownPrivilege');

Madshi, did you remember this:)

http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=delphi&qid=11971598

Motaz

Commented:
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.GetCurrentProcess, 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^.PrivilegeCount) - 1 do
            ptp^.Privileges[i1].Attributes := ptp^.Privileges[i1].Attributes 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.

Commented:
use this 4 reboot:
ExitWindowsEx(EWX_REBOOT+EWX_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+EWX_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.




Author

Commented:
Thankyou, that was all I wanted to know

Andrew

Explore More ContentExplore courses, solutions, and other research materials related to this topic.