如何关闭计算机?
我在网上找了好几个例子,但都不成功。

解决方案 »

  1.   

    SetPrivilege('SeShutdownPrivilege', true);
            ExitWindowsEx(EWX_SHUTDOWN or EWX_FORCE or EWX_POWEROFF, 0);
      

  2.   

    function Tmain.SetPrivilege(sPrivilegeName: string; bEnabled: boolean): boolean;
    var
        TP, TPPre: TTokenPrivileges;
        Token: THandle;
        dwLength: DWORD;
    begin
        result := false;
        OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,
            Token);
        try
            TP.PrivilegeCount := 1;
            if LookupPrivilegeValue(nil, PChar(sPrivilegeName),
                TP.Privileges[0].LUID) then
            begin
                if bEnabled then
                    TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
                else
                    TP.Privileges[0].Attributes := 0;
                dwLength := 0;
                Result := AdjustTokenPrivileges(Token, false, TP, sizeof(TPPre),
                    TPPre, dwLength);
            end;
        finally
            CloseHandle(Token);
        end;
    end;
      

  3.   

    http://expert.csdn.net/Expert/ForumList_Search.asp?searchtype=1&bigclassid=53&smallclassid=5301&searchKeys=%B9%D8%BB%FA&author=&tabletype=now
      

  4.   

    我记得我都回答了N次了,不好意思
    //此方法支持win2000/win98
    procedure Tform2.rebootcomputer;
    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_SHUTDOWN, 0)) then   //EWX_SHUTDOWN/EWX_REBOOT
            begin
             application.Terminate;
            end;
    end;
      

  5.   

    //关闭win2000的过程///////////////////////////////////////////////////////////
    procedure ExitWin2000(ewx: integer);  {ewx 1: 重启;2:关闭电源}
    var
      hToken: THANDLE;
      hProc: THANDLE;
      mLUID: TLargeInteger;
      mPriv, mNewPriv: TOKEN_PRIVILEGES;
      mBufferLength: DWord;
    begin
      if not (ewx in [1, 2]) then
        exit;
      hProc := GetCurrentProcess();
      OpenProcessToken(hProc, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, hToken);
      LookupPrivilegeValue('', 'SeShutdownPrivilege', mLUID);
      mPriv.PrivilegeCount := 1;
      mPriv.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
      mPriv.Privileges[0].Luid := mLUID;
      AdjustTokenPrivileges(hToken, False, mPriv, (4 + (12 * mPriv.PrivilegeCount)), mNewPriv, mBufferLength);
      case ewx of
        1: ExitWindowsEx(EWX_FORCE + EWX_REBOOT, 0);
        2: ExitWindowsEx(EWX_FORCE + EWX_POWEROFF, 0);
      end;
    end;
    //关闭计算机的过程/////////////////////////////////////////////////////////
    procedure shutDown();
    var
      os:OSVERSIONINFO;
    begin
      os.dwOSVersionInfoSize:=sizeOf(TOSversionInfo);
      getVersionEx(os);
      if (os.dwPlatformId=VER_PLATFORM_WIN32_WINDOWS) then
        exitWindowsEx(EWX_SHUTDOWN,0);
      if (os.dwPlatformId=VER_PLATFORM_WIN32_NT) then
        exitWin2000(2);
    end;