VC------Windows關機函數,休眠函數使用大全(適用Windows所有操作平臺)

關閉Windows函數ExitWindowsEx(UINT     uFlag,DWORD:dwReserved)說明:

控制WINDOWS的開關:如關閉WINDOWS,重新啓動WINDOWS等,     ExitWindowsEx(UINT     uFlags,DWORD     dwReserved);是實現這一功能的API函數。如果Complile時提示EWX_XXXX未定義,那麼請手動定義這幾個常數,默認情況下是無需 我們手動定義的。
const
      EWX_FORCE=4;     //關閉所有程序並以其他用戶身份登錄?(!!應爲“強制執行否”吧!!)
      EWX_LOGOFF=0;     //重新啓動計算機並切換到MS-DOS方式
      EWX_REBOOT=2;     //重新啓動計算機
      EWX_SHUTDOWN=1;//關閉計算機
      EWX_POWEROFF=5;//好象是5,記不太清了
      EWX_FORCEIFHUNG=7;//7?不記得了,有誰好心查下MSDN
調用方法:
      ExitWindowsEx(EWX_REBOOT,0);     //重啓計算機
      ExitWindowsEx(EWX_FORCE+EWX_SHUTDOWN,0);     //強行關機

      不過博主經常聽到有人說這一API只在Windows     95/98/98SE/Me下有效,而在Windows     NT/2000/XP下無效。
      其實這是不正確的,這一API在上述平臺下均是有效的,只是我們在Windows     NT/2000/XP平臺下執行此函數之前,必須要獲取得關機特權罷了,其實就算是Windows     NT/2000/XP系統自身關機也必須要走這一流程的。
      獲取關機特權函數如下:
      procedure     Get_Shutdown_Privilege;     //獲得用戶關機特權,僅對Windows     NT/2000/XP
      var   
          rl:     Cardinal;
          hToken:     Cardinal;
          tkp:     TOKEN_PRIVILEGES;   
      begin   
          OpenProcessToken(GetCurrentProcess,     TOKEN_ADJUST_PRIVILEGES     or     TOKEN_QUERY,     hToken);
          if     LookupPrivilegeValue(nil,     "SeShutdownPrivilege ",     tkp.Privileges[0].Luid)     then
          begin
              tkp.Privileges[0].Attributes     :=     SE_PRIVILEGE_ENABLED;
              tkp.PrivilegeCount     :=     1;
              AdjustTokenPrivileges(hToken,     False,     tkp,     0,     nil,     rl);
          end;
      end;

      另一個關機API,InitiateSystemShutdown(PChar(Computer_Name),PChar(Hint_Msg), Time,Force,Reboot);在Windows     NT/2000/XP平臺下還會自動調用系統本身的關機提示窗口。
      InitiateSystemShutdown(PChar(Computer_Name),     PChar(Hint_Msg),Time,Force,Reboot);
                                                //關機計算機名,關機提示信息,停留時長,是否強行關機,是否要重啓
      當我們把Computer_Name設爲nil時,默認爲本機,如     InitiateSystemshutdown(nil,nil,0,True,False);//強行關機

      由於我們需要製作一個通用的關機程序,故要對當前的操作系統進行判斷,這個比較簡單,函數如下:
      function     GetOperatingSystem:     string;//獲取操作系統信息
      var       osVerInfo:     TOSVersionInfo;
      begin
          Result     := " ";
          osVerInfo.dwOSVersionInfoSize     :=     SizeOf(TOSVersionInfo);
          if     GetVersionEx(osVerInfo)     then
              case     osVerInfo.dwPlatformId     of
              VER_PLATFORM_WIN32_NT:
              begin
                  Result     :=     "Windows     NT/2000/XP "
              end;
              VER_PLATFORM_WIN32_WINDOWS:
              begin
                  Result     :=     "Windows     95/98/98SE/Me ";
              end;
          end;
      end;

      執行關機的主函數:
      procedure     ShutDownComputer;
      begin
          if     GetOperatingSystem= "Windows     NT/2000/XP "     then   
          begin   
              Get_Shutdown_Privilege;
              //調用此函數會出現系統關機提示窗口,並允許用戶取消關機動作
              //InitiateSystemShutDown(nil, "關機提示:討厭你所以關了你! ",0,True,False);
              ExitWindowsEx(EWX_SHUTDOWN+EWX_FORCE+EWX_POWEROFF+EWX_FORCEIFHUNG,0);
          end     else
          begin
              ExitWindowsEx(EWX_SHUTDOWN+EWX_FORCE+EWX_POWEROFF+EWX_FORCEIFHUNG,0);
          end;
      end;   
使用:

procedure     TShutDownForm.btn_PowerOffClick(Sender:Object);
begin
      ShutDownComputer;
end;

=========================================================================================================================}
實現系統休眠
HANDLE     hToken;                //     handle     to     process     token   
TOKEN_PRIVILEGES     tkp;      //     pointer     to     token     structure   

BOOL     fResult;                //     system     shutdown     flag   

//     Get     the     current     process     token     handle     so     we     can     get     shutdown   
//     privilege.   

if     (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEG     ES ¦TOKEN_QUERY,     &hToken))   
{   
  ShowMessage("OpenProcessToken     failed.");   
  return;   
}   
//     Get     the     LUID     for     shutdown     privilege.   

LookupPrivilegeValue(NULL,     SE_SHUTDOWN_NAME,   
          &tkp.Privileges[0].Luid);   

tkp.PrivilegeCount     =     1;     //     one     privilege     to     set   
tkp.Privileges[0].Attributes     =     SE_PRIVILEGE_ENABLED;   

//     Get     shutdown     privilege     for     this     process.   

AdjustTokenPrivileges(hToken,     FALSE,     &tkp,     0,   
  (PTOKEN_PRIVILEGES)     NULL,     0);   

//     Cannot     test     the     return     value     of     AdjustTokenPrivileges.   

if     (GetLastError()     !=     ERROR_SUCCESS)   
{   
  ShowMessage("AdjustTokenPrivileges     enable     failed.");   
  return;   
}   
SetSystemPowerState(false,TRUE); 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章