unit zhuxiao;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,shlobj, StdCtrls,ShellAPI;type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    //function exitwindowsex(uflags,dwreserved:cardinal):longbool;external user32 name 'ExitWindowsEx';
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
exitwindowsex(ewx_force,0);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
exitwindowsex(ewx_reboot,0);//重起
exitwindowsex(ewx_shutdown,0);//重起end;end.

解决方案 »

  1.   

    exitwindowsex(EWX_REBOOT OR EWX_POWEROFF,0); 重启exitwindowsex(EWX_SHUTDOWN OR EWX_POWEROFF,0); 关机参数不对而已。
      

  2.   

    加上一句小弟用的是windows2000server
      

  3.   

    function ShutDownSystem:boolean; //关闭2K系统;
    var
    hProcess,hAccessToken:THandle;
    LUID_AND_ATTRIBUTES:TLUIDAndAttributes;
    TOKEN_PRIVILEGES: TTokenPrivileges;
    BufferIsNull:DWORD;
    Const
    SE_SHUTDOWN_NAME='SeShutdownPrivilege';
    begin
      hProcess:=GetCurrentProcess();
      OpenProcessToken(hprocess,TOKEN_ADJUST_PRIVILEGES+TOKEN_QUERY,hAccessToken);
      LookupPrivilegeValue(Nil,SE_SHUTDOWN_NAME,LUID_AND_ATTRIBUTES.Luid);
      LUID_AND_ATTRIBUTES.Attributes:=SE_PRIVILEGE_ENABLED;
      TOKEN_PRIVILEGES.PrivilegeCount:=1;
      TOKEN_PRIVILEGES.Privileges[0]:=LUID_AND_ATTRIBUTES;
      BufferIsNull:=0;
      AdjustTokenPrivileges(hAccessToken,False,TOKEN_PRIVILEGES,sizeof(TOKEN_PRIVILEGES),Nil,BufferIsNull);
      ExitWindowsEx(EWX_POWEROFF, 0);
      ShutDownSystem:=True;
    end;
    这个可以用以2K以上,
      

  4.   

    kinsunc(kinsunc) :能否给您的程序赋些解释,因为小弟是菜鸟,想增加点经验
      

  5.   

    刚刚试验了楼上的程序,结果有的程序能关闭,但是系统却不能关毕,不知为什么?我使用的下面的这个程序却可以。大家共享吧!另外那位知道这个程序如何改成重启程序,请不吝赐教,谢谢!
    function ShutMeDown:string;
    var
      hToken : THandle;
      tkp,p  : TTokenPrivileges;
      RetLen : DWord;
      ExReply: LongBool;
      Reply  : DWord;
    begin
    case Win32Platform of
           VER_PLATFORM_WIN32_WINDOWS: //***Windows 9x/ME***//
           begin
           ExReply:= ExitWindowsEx(EWX_POWEROFF or EWX_SHUTDOWN or EWX_FORCE,0);
           if ExReply then
           Result:='Shutdown Initiated'
           else
           Result:='Shutdown failed with ' + IntToStr(GetLastError);
           end;
           VER_PLATFORM_WIN32_NT:  //***Windows NT/2000/XP***//
           begin
           if OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or
    TOKEN_QUERY,hToken) then
                   begin
                   if
    LookupPrivilegeValue(nil,'SeShutdownPrivilege',tkp.Privileges[0].Luid) then
                           begin
                           tkp.PrivilegeCount := 1;
                           tkp.Privileges[0].Attributes :=
    SE_PRIVILEGE_ENABLED;AdjustTokenPrivileges(hToken,False,tkp,SizeOf(TTokenPrivileges),p,RetLen);
                           Reply := GetLastError;
                   if Reply = ERROR_SUCCESS then
            begin
            ExReply:= ExitWindowsEx(ewx_shutdown or EWX_POWEROFF, 0);
            if ExReply then
            Result:='Shutdown Initiated'
            else
            Result:='Shutdown failed with ' + IntToStr(GetLastError);
            end;
           end;
           end;
    end; //end case
    end;
    end;
      

  6.   

    以前Adjusttokenprivileges函数不会用,刚刚在这里找到了答案,测试了一下,OK,没问题了,还害得我的机器重启了一次,测试代码如下:
    procedure TForm1.Button1Click(Sender: TObject);
    Const
      SE_SHUTDOWN_NAME='SeShutdownPrivilege';
    var
      hProcess,hAccessToken:Thandle;
      Luid_and_attributes:Tluidandattributes;
      token_privileges:TTokenPrivileges;
      BufferIsNull:Dword;
    begin
       hProcess:=GetCurrentProcess ;
       OpenProcessToken(hprocess,TOKEN_ADJUST_PRIVILEGES+TOKEN_QUERY,hAccessToken);
       LookupPrivilegeValue(nil,SE_SHUTDOWN_NAME,Luid_and_attributes.luid);
       luid_and_attributes.Attributes :=SE_PRIVILEGE_ENABLED;
       token_privileges.PrivilegeCount :=1;
       token_privileges.Privileges[0]:=Luid_and_attributes;
       BufferIsNull :=0;
       adjusttokenprivileges(haccesstoken,false,token_privileges,sizeof(token_privileges),nil,BufferIsNull);
       case RadioGroup1.ItemIndex of
       0:win32check(ExitWindows(0,0));
       1:win32check(ExitWindowsEx(EWX_LOGOFF,0));
       2:win32check(ExitWindowsEx(EWX_REBOOT,0));
       3:win32check(ExitWindowsEx(EWX_SHUTDOWN,0));
       end;
    end;
      

  7.   

    3:win32check(ExitWindowsEx(EWX_SHUTDOWN,0));我昨天最后测试了关机功能,可是不能想Win那些自动断电,这个怎么解决?
      

  8.   

    将EWX——SHUTDOWN 改成 EWX_POWEROFF 你的系统可能是2K的吧?