哪位GG有自动关机的代码发给我如何? [email protected]

解决方案 »

  1.   

    自己写一个呀很简单的,调用一个API函数了
      

  2.   

    控制WINDOWS的开关:如关闭WINDOWS,重新启动WINDOWS等, ExitWindowsEx(UINT uFlags,DWORD dwReserved);是实现这一功能的API函数
    首先定义常数
    const
    EWX_FORCE=4; //关闭所有程序并以其他用户身份登录
    EWX_LOGOFF=0; //重新启动计算机并切换到MS-DOS方式
    EWX_REBOOT=2; //重新启动计算机
    EWX_SHUTDOWN=1;//关闭计算机
    运行时给How赋值,让他等于EWX_SHUTDOWN或其他,调用以下语句
    ExitWindowsEx(How,0);///////////////////////////////////////////
    Function ShutDownWindows(Flags: Byte) : Boolean;
    begin
         Result := ExitWindowsEx(Flage, 0)
         // 如果成功返回 True.
    end;Flags可用参数:EWX_LOGOFF:
      (=0)
      安全地关闭所有进程,并关闭用户登录。EWX_SHUTDOWN:
      (=1)
      关闭系统。所有缓冲区的内容都能被安全的存盘,所有进程都将被停止。
      对于Windows NT: 必须有 SE_SHUTDOWN_NAME 的安全特权方可进行此项操作。
      对于Windows 95: 不必有任何特权。EWX_REBOOT:
      (=2)
      关闭并重新启动系统。
      对于Windows NT: 必须有 SE_SHUTDOWN_NAME 的安全特权方可进行此项操作。
      对于Windows 95: 不必有任何特权。EWX_FORCE:
      (=4)
       强制切断连接,关闭所有应用程序。当使用这个参数时,Windows 将不向正在运行的应用程序发送 WM_QUERYENDSESSION 和 WM_ENDSESSION 消息,有可能造成数据丢失。所以推荐只在紧急时使用这个参数。EWX_POWEROFF:
      (=8)
      关闭系统并切断电源,需要ATX电源支持。
    /////////////////////////////////////
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);  private
        { Private declarations }
        procedure AdjustToken;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}procedure TForm1.AdjustToken();
    var
      hdlProcessHandle : Cardinal;
      hdlTokenHandle : Cardinal;
      tmpLuid : Int64;
      tkpPrivilegeCount : 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);         // Get the LUID for shutdown privilege.
             LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);
             Privilege[0].Luid := tmpLuid;
             Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;
             tkp.PrivilegeCount := 1;   // One privilege to set
             tkp.Privileges[0] := Privilege[0];
             // Enable the shutdown privilege in the access token of this
             // process.
             AdjustTokenPrivileges(hdlTokenHandle,
                                   False,
                                   tkp,
                                   Sizeof(tkpNewButIgnored),
                                   tkpNewButIgnored,
                                   lBufferNeeded); end;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
         AdjustToken;
         ExitWindowsEx((EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT), $FFFF);
    end;end.
      

  3.   

    function ShutDownSystem: Boolean; //关闭计算机
    var
      VerInfo: TOSVersionInfo;
      hToken: THANDLE;
      tkp: TOKEN_PRIVILEGES;
      Nothing: Cardinal;
    begin
      if IsWinNT then begin
        VerInfo.dwOSVersionInfoSize := SizeOf(VerInfo);
        GetVersionEx(VerInfo);
        if VerInfo.dwPlatformId = VER_PLATFORM_WIN32_NT then begin
          OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,
            hToken);
          LookupPrivilegeValue(nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid);
          tkp.PrivilegeCount := 1;
          tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
          AdjustTokenPrivileges(hToken, FALSE, tkp, 0, nil, Nothing);
        end;
        ExitWindowsEx(EWX_FORCE + EWX_SHUTDOWN + EWX_POWEROFF, 0);
      end
      else
        ExitWindowsEx(EWX_FORCE + EWX_SHUTDOWN + EWX_POWEROFF, 0);
      Result := True;
    end;
      

  4.   

    procedure Tform1.windowsexitorreboot(flag: Integer);  //关机、重起、关闭应用程序
    var
      VerInfo:TOSVersionInfo;
      hToken:THANDLE;
      tkp:TOKEN_PRIVILEGES;
      Nothing:Cardinal;
    begin
      VerInfo.dwOSVersionInfoSize:=SizeOf(VerInfo);
      GetVersionEx(VerInfo);
      if VerInfo.dwPlatformId=VER_PLATFORM_WIN32_NT then
      Begin
        OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,hToken);
        LookupPrivilegeValue(nil,'SeShutdownPrivilege',tkp.Privileges[0].Luid);
        tkp.PrivilegeCount:= 1;
        tkp.Privileges[0].Attributes:= SE_PRIVILEGE_ENABLED;
        AdjustTokenPrivileges(hToken, FALSE, tkp, 0,nil, Nothing);
      End;
         case flag of
         0 :
         begin
           ExitWindowsEx(EWX_POWEROFF,0);//关机
         end ;
         1 :
         begin
           ExitWindowsEx(EWX_REBOOT,0);//重器
         end ;
         2 :
         begin
            try
               close ;
            except
               Application.Terminate ;
            end ;
         end ;
         end ;