zoukankan      html  css  js  c++  java
  • 程序的开机关机重启,开机启动,休眠功能delphi实现(使用AdjustTokenPrivileges提升权限)

     TShutDownStatus = (sdShutDown,sdReboot,sdLogOff,sdPowerOff);

     procedure ShutDown(sdStatus : TShutDownStatus);

      var
         NewState:       TTokenPrivileges;
         lpLuid:         Int64;
         ReturnLength:   DWord;
         ToKenHandle:    Cardinal;
      begin
         OpenProcessToken(GetCurrentProcess,
                       TOKEN_ADJUST_PRIVILEGES
                       OR TOKEN_ALL_ACCESS
                       OR STANDARD_RIGHTS_REQUIRED
                       OR TOKEN_QUERY,ToKenHandle);
         LookupPrivilegeValue(nil,'SeShutdownPrivilege',lpLuid);
         NewState.PrivilegeCount:=1;
         NewState.Privileges[0].Luid:=lpLuid;
         NewState.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;
         ReturnLength:=0;
         AdjustTokenPrivileges(ToKenHandle,False,NewState,0,nil,ReturnLength);
        case sdStatus of
          sdShutDown: ExitWindowsEx(EWX_SHUTDOWN OR EWX_POWEROFF or EWX_FORCE,0);
          sdReboot: ExitWindowsEx(EWX_REBOOT OR EWX_POWEROFF or EWX_FORCE,0);
          sdLogOff:ExitWindowsEx(EWX_LOGOFF,0);
          sdPowerOff:SetSystemPowerState(False,True);
          end;

      end;

      {
        函数功能:实现系统睡眠休眠功能
        bHibernate : True 睡眠  False 休眠
      } 

    procedure TEpComOper.SystemSleep(bHibernate:Boolean);
      var
        h_Module : THandle;
        pSetSuspendState : function(hibernate,ForceCritical,DisableWakeEvent:Boolean):Boolean;
      begin
        h_Module := LoadLibrary('PowrProf.dll');
        if h_Module <>0 then
        begin
          pSetSuspendState := GetProcAddress(h_Module,'SetSuspendState');
          if @pSetSuspendState<>nil then
          begin
            pSetSuspendState(bHibernate,False,False);
          end;
          FreeLibrary(h_Module);
        end;
      end;

    {设置取消程序的开机启动功能}
      procedure TEpComOper.SetAppAutoRun(bRun : Boolean);
      var
        Reg:TRegistry;
      begin
        Reg := TRegistry.Create;
        try
          Reg.RootKey := HKEY_LOCAL_MACHINE;
          Reg.OpenKey('SoftwareMicrosoftWindowsCurrentVersionRun',True);
          if bRun then
            Reg.WriteString(‘test’,Application.ExeName)
          else
            Reg.DeleteValue('test');
          Reg.CloseKey;
        finally
          Reg.Free;
        end;
      end;

    http://blog.csdn.net/zang141588761/article/details/51859129

  • 相关阅读:
    设计模式-工厂模式-场景以及优缺点-目的就是应对变化 (国江面试回答的)
    开启otl的64位长整数支持
    otl翻译(11) -- OTL的迭代器
    OTL翻译(10) -- OTL的流缓冲池
    OTL翻译(9) --常量的SQL语句
    OTL翻译(8) -- otl_long_string/otl_long_unicode_string类
    OTL翻译(7) -- otl_exception类
    OTL翻译(6) -- otl_connect类
    OTL翻译(5) -- otl_stream流相关绑定变量
    OTL翻译(4) -- otl_stream类
  • 原文地址:https://www.cnblogs.com/findumars/p/6711268.html
Copyright © 2011-2022 走看看