Einzelnen Beitrag anzeigen

Aike

Registriert seit: 2. Okt 2004
319 Beiträge
 
#12

Re: TShutDown komponente

  Alt 8. Feb 2006, 12:11
Einfacher geht's so (Zusammenfassung):

Delphi-Quellcode:
type
  TExit=(Reboot,Shutdown,Logoff,Hibernate,Standby);
Delphi-Quellcode:
procedure GetShutdownPrivileges;
 //Urheber: Daniel B.
var vi : TOSVersionInfo;
    hToken : THandle;
    tp : TTokenPrivileges;
    h : DWord;
begin
  vi.dwOSVersionInfoSize:=SizeOf(vi);
  GetVersionEx(vi);
  if vi.dwPlatformId = VER_PLATFORM_WIN32_NT then
  begin
    OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,hToken);
    LookupPrivilegeValue(nil,'SeShutdownPrivilege',tp.Privileges[0].Luid);
    tp.PrivilegeCount := 1;
    tp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
    h := 0;
    AdjustTokenPrivileges(hToken,False,tp,0,PTokenPrivileges(nil)^,h);
    CloseHandle(hToken);
  end;
end;
Delphi-Quellcode:
procedure ExitWin(Exit:TExit;Force:boolean);
var Flags:integer;
begin
  GetShutdownPrivileges;

  if Exit=Shutdown then
    ExitWindowsEx(EWX_ShutDown,0);

  if Exit=Hibernate then
    SetSystemPowerState(false,Force);

  if Exit=Standby then
    SetSystemPowerState(true,Force);

  if Exit=Logoff then
  begin
    Flags:=EWX_LOGoff;
    if Force then
      flags:=flags+EWX_FORCE;
    ExitWindowsEx(flags,0);
  end;

  if Exit=Reboot then
  begin
    flags:=EWX_REBOOT;
    if Force then
      flags:=flags+EWX_FORCE;
    ExitWindowsEx(flags,0);
  end;
end;
greetz ...
Achim
  Mit Zitat antworten Zitat