Link to home
Start Free TrialLog in
Avatar of skymag
skymag

asked on

WIndows NT restart

I would really appreciate some code to Restart my PC using Delphi 5 on Windows NT 4 SP6.
Avatar of Jaymol
Jaymol

I'll give you this code, but the best I've ever had out of it is logging out.  Hope it helps anyway.

procedure CloseWindows(ShutDownType: DWord);
var
  hToken    : THandle;
  tkp, Newt : TTokenPrivileges;
  retlength : DWORD;
begin
  if OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
    if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid) = False then begin
      tkp.PrivilegeCount:=1;
      tkp.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;
      AdjustTokenPrivileges(hToken, False, tkp, SizeOf(TTokenPrivileges), Newt, retlength);
    end;
  ExitWindowsEx(ShutDownType, 0);
end;

If you don't have any luck, shout the magic word......madshi.

Ta,

John.
Oops... I didn't know that "madshi" is a magic word, is it really?   :-))

Well, what I have looks quite similar to John's code, but perhaps there's a small difference somewhere. At least shutting down and restarting works with my code - as long as you've the nessecary rights...

function AddPrivilege(privilegeName: string) : boolean;
type TPCardinal = ^cardinal;
var c1,c2   : cardinal;
    i64     : int64;
    tp1,tp2 : TTokenPrivileges;
begin
  result:=false;
  if OpenProcessToken(windows.GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,c1) then
    try
      if LookupPrivilegeValue(nil,pchar(privilegeName),i64) then begin
        tp1.PrivilegeCount:=1;
        tp1.Privileges[0].Luid:=i64;
        tp1.Privileges[0].Attributes:=0;
        if AdjustTokenPrivileges(c1,false,tp1,sizeOf(TTokenPrivileges),tp2,c2) then begin
          if (c2=sizeOf(TTokenPrivileges)) and (tp2.PrivilegeCount=1) and (tp2.Privileges[0].Luid=i64) then
            tp1.Privileges[0].Attributes:=tp2.Privileges[0].Attributes;
          tp1.Privileges[0].Attributes:=tp1.Privileges[0].Attributes or SE_PRIVILEGE_ENABLED;
          result:=AdjustTokenPrivileges(c1,false,tp1,sizeOf(TTokenPrivileges),PTokenPrivileges(nil)^,TPCardinal(nil)^);
        end;
      end;
    finally CloseHandle(c1) end;
end;

AddPrivilege('SeShutdownPrivilege');
ExitWindowsEx(EWX_REBOOT, 0);

Regards, Madshi.
ASKER CERTIFIED SOLUTION
Avatar of schutnik
schutnik

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of skymag

ASKER

Thanx for the help guys!
Avatar of skymag

ASKER

Thanx for the help guys!