quarta-feira, 23 de julho de 2008

Faz um REBOOT - Windows9x e NT


Pessoal para quem está precisando reiniciar a máquina apartir do próprio aplicativo seque código de exemplo:

function Reboot: Boolean;
var
pid, hToken: THandle;
tmpLUID : TLUIDAndAttributes;
Tkp, NewTkn: TTokenPrivileges;
BufLen : DWORD;

begin
Result := True;
// Para 9x basta isto:
if Win32Platform <> VER_PLATFORM_WIN32_NT then
begin
ExitWindowsEx(EWX_REBOOT, 0);
Exit;
end;


// Obtem o handle do processo atual pid := GetCurrentProcess;
// Abre o token associado ao processo
OpenProcessToken(pid, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, hToken);


// Obter o identificador LUID que representa o privilegio
LookupPrivilegeValue(nil, 'SeShutdownPrivilege', tmpLUID.LUID);

with Tkp do
begin
PrivilegeCount := 1;
Privileges[0].LUID := tmpLUID.LUID;
Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
end;


// Agora ajusta o privilegio para conseguir o reboot
AdjustTokenPrivileges(hToken, False, Tkp, SizeOf(NewTkn), NewTkn, BufLen);


// Aguarda 1 segundo para refresh
Sleep(1000);
if ExitWindowsEx(EWX_FORCE + EWX_REBOOT, 0) then
Exit;


Result := False;
end;

  © Blogger template 'Perfection' by Ourblogtemplates.com 2008

Back to TOP