skymag
asked on
WIndows NT restart
I would really appreciate some code to Restart my PC using Delphi 5 on Windows NT 4 SP6.
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.G etCurrentP rocess,TOK EN_ADJUST_ PRIVILEGES or TOKEN_QUERY,c1) then
try
if LookupPrivilegeValue(nil,p char(privi legeName), i64) then begin
tp1.PrivilegeCount:=1;
tp1.Privileges[0].Luid:=i6 4;
tp1.Privileges[0].Attribut es:=0;
if AdjustTokenPrivileges(c1,f alse,tp1,s izeOf(TTok enPrivileg es),tp2,c2 ) then begin
if (c2=sizeOf(TTokenPrivilege s)) and (tp2.PrivilegeCount=1) and (tp2.Privileges[0].Luid=i6 4) then
tp1.Privileges[0].Attribut es:=tp2.Pr ivileges[0 ].Attribut es;
tp1.Privileges[0].Attribut es:=tp1.Pr ivileges[0 ].Attribut es or SE_PRIVILEGE_ENABLED;
result:=AdjustTokenPrivile ges(c1,fal se,tp1,siz eOf(TToken Privileges ),PTokenPr ivileges(n il)^,TPCar dinal(nil) ^);
end;
end;
finally CloseHandle(c1) end;
end;
AddPrivilege('SeShutdownPr ivilege');
ExitWindowsEx(EWX_REBOOT, 0);
Regards, Madshi.
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
type TPCardinal = ^cardinal;
var c1,c2 : cardinal;
i64 : int64;
tp1,tp2 : TTokenPrivileges;
begin
result:=false;
if OpenProcessToken(windows.G
try
if LookupPrivilegeValue(nil,p
tp1.PrivilegeCount:=1;
tp1.Privileges[0].Luid:=i6
tp1.Privileges[0].Attribut
if AdjustTokenPrivileges(c1,f
if (c2=sizeOf(TTokenPrivilege
tp1.Privileges[0].Attribut
tp1.Privileges[0].Attribut
result:=AdjustTokenPrivile
end;
end;
finally CloseHandle(c1) end;
end;
AddPrivilege('SeShutdownPr
ExitWindowsEx(EWX_REBOOT, 0);
Regards, Madshi.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanx for the help guys!
ASKER
Thanx for the help guys!
procedure CloseWindows(ShutDownType:
var
hToken : THandle;
tkp, Newt : TTokenPrivileges;
retlength : DWORD;
begin
if OpenProcessToken(GetCurren
if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid) = False then begin
tkp.PrivilegeCount:=1;
tkp.Privileges[0].Attribut
AdjustTokenPrivileges(hTok
end;
ExitWindowsEx(ShutDownType
end;
If you don't have any luck, shout the magic word......madshi.
Ta,
John.