在win2000下如何調用API函數關機?或者如何調用關機那窗口關機?

解决方案 »

  1.   

    unit main;interfaceuses
      Windows, SysUtils, Classes, Forms, StdCtrls, Mask,
      Buttons, TrayIcon, Menus, Controls, ExtCtrls,Registry;type
      TForm1 = class(TForm)
        Panel1: TPanel;
        MaskEdit1: TMaskEdit;
        BitBtn1: TBitBtn;
        Timer1: TTimer;
        Label2: TLabel;
        BitBtn3: TBitBtn;
        TrayNotifyIcon1: TTrayNotifyIcon;
        PopupMenu1: TPopupMenu;
        N1: TMenuItem;
        N2: TMenuItem;
        N3: TMenuItem;
        procedure BitBtn1Click(Sender: TObject);
        procedure Timer1Timer(Sender: TObject);
        procedure BitBtn3Click(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure TrayNotifyIcon1DblClick(Sender: TObject);
        procedure N2Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure N3Click(Sender: TObject);
        procedure FormShow(Sender: TObject);
      private
        { Private declarations }
        procedure AdjustToken();
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      oldx,oldy: integer;
      ShutDownTime:TTime;
      SFlag:Boolean;
    implementationuses About;{$R *.dfm}
    procedure TForm1.AdjustToken;
    var
      hdlProcessHandle : Cardinal;
      hdlTokenHandle : Cardinal;
      tmpLuid : Int64;
      tkp : TOKEN_PRIVILEGES;
      tkpNewButIgnored : TOKEN_PRIVILEGES;
      lBufferNeeded : Cardinal;
      Privilege : array[0..0] of _LUID_AND_ATTRIBUTES;
    begin
             hdlProcessHandle := GetCurrentProcess;
             OpenProcessToken(hdlProcessHandle,
                             (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY),
                              hdlTokenHandle);
             LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);
             Privilege[0].Luid := tmpLuid;
             Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;
             tkp.PrivilegeCount := 1;
             tkp.Privileges[0] := Privilege[0];
             AdjustTokenPrivileges(hdlTokenHandle,
                                   False,
                                   tkp,
                                   Sizeof(tkpNewButIgnored),
                                   tkpNewButIgnored,
                                   lBufferNeeded);
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
      ZAppName: array[0..127] of char;
      ATitle,TmpStr: String;
      Found: HWND;
      Reg:TRegistry;
    begin
      ATitle := Application.Title;
      Application.Title := 'OnlyOne'
         + IntToStr(HInstance);
      StrPCopy(ZAppName, ATitle);
      Found := FindWindow(nil, ZAppName);
      Application.Title := ATitle;
      if Found<>0 then begin
        Application.Terminate;
      end;
    SFlag:=False;
    Oldx:=Form1.Left;
    Oldy:=Form1.Top;
    Reg:=TRegistry.Create;
    Reg.RootKey:=HKEY_LOCAL_MACHINE;
    if Reg.OpenKey('Software\ShutDownOnTime', False) then
       TmpStr:=Reg.ReadString('TheTime');
    Reg.CloseKey;
    Reg.Free;
    Maskedit1.Text :=TmpStr;
    end;procedure TForm1.BitBtn1Click(Sender: TObject);
    var Reg: TRegistry;
    begin
    if SFlag=False then
    try
           ShutDownTime:=StrtoTime(MaskEdit1.Text+':00');
           Timer1.Enabled :=True;
           BitBtn1.Caption:='取消定时';
           MaskEdit1.Enabled :=False;
           SFlag:=True;
           Reg:=TRegistry.Create;
           Reg.RootKey:=HKEY_LOCAL_MACHINE;
           if Reg.OpenKey('Software\ShutDownOnTime', True) then
              Reg.WriteString('TheTime', MaskEdit1.Text);
           Reg.CloseKey;
           Reg.Free;
    except Application.MessageBox('时间格式不对','错误',MB_OK+MB_ICONERROR);
    end
    Else Begin
           Timer1.Enabled :=False;
           BitBtn1.Caption:='定时关机';
           MaskEdit1.Enabled :=True;
           SFlag:=False;
    end;
    end;procedure TForm1.Timer1Timer(Sender: TObject);
    begin
    if Timetostr(time())=TimetoStr(ShutDownTime) then
    Begin
      AdjustToken();
      ExitWindowsEx((EWX_FORCE or EWX_POWEROFF), 0);
    end;
    end;
    procedure TForm1.BitBtn3Click(Sender: TObject);
    begin
    Close;
    end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
    TrayNotifyIcon1.IconVisible:=True;
    if SFlag=True then
    TrayNotifyIcon1.Hint:='关机时间:'+Maskedit1.Text
    Else TrayNotifyIcon1.Hint:='没有定时';
    Action:=caNone;
    hide;
    end;procedure TForm1.TrayNotifyIcon1DblClick(Sender: TObject);
    begin
    TrayNotifyIcon1.IconVisible:=False;
    show;
    end;procedure TForm1.N2Click(Sender: TObject);
    begin
    if Application.MessageBox('确实要退出系统吗?','确认',MB_YesNo+MB_ICONQUESTION+MB_DEFBUTTON2)=MrYes then
     Application.Terminate ;
    end;
    procedure TForm1.N3Click(Sender: TObject);
    begin
    AboutBox.ShowModal;
    end;procedure TForm1.FormShow(Sender: TObject);
    begin
    Self.Left :=Oldx;
    Self.Top :=OldY;
    end;end.
      

  2.   

    以上代码是用来定时关机的,用以下代码即可:
    Var
      st : SYSTEMTIME;
      hToken : THANDLE;
      tkp : TOKEN_PRIVILEGES;
      rr : Dword;
    Begin
           OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or  TOKEN_QUERY,hToken);
            LookupPrivilegeValue(nil,'SeShutdownPrivilege',tkp.Privileges[0].Luid);
            // 设定权限为1
            tkp.PrivilegeCount := 1;
            tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
            // 得到权限
            AdjustTokenPrivileges(hToken, FALSE, tkp, 0,nil,rr);
            // 关闭计算机
             ExitWindowsEx(EWX_FORCE + EWX_SHUTDOWN + EWX_POWEROFF, 0);
    End;
      

  3.   

    procedure TForm1.reboot_computer;
    var
      hToken:THandle;
      tkp : TOKEN_PRIVILEGES;
      ReturnLength : DWord;
    begin
      if (not OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_ALL_ACCESS or TOKEN_QUERY, hToken))then
      begin
        application.Terminate;
      end;
      LookupPrivilegeValue(nil,'SeShutdownPrivilege',tkp.Privileges[0].Luid);
      tkp.PrivilegeCount := 1;
      tkp.Privileges[0].Attributes :=SE_PRIVILEGE_ENABLED;
      ReturnLength :=0;
      AdjustTokenPrivileges(hToken, FALSE, tkp, 0,nil,ReturnLength);
      if (GetLastError() <> ERROR_SUCCESS) then
      begin
        application.Terminate;
      end;
      if (not ExitWindowsEx(EWX_POWEROFF, 0)) then
      begin
        application.Terminate;
      end;
    end;