function Tform1.WinExitInNT(iFlags: Integer ): Boolean; begin Result := True; if (SetPrivilege('SeShutdownPrivilege',True)) then begin if(not ExitWindowsEx(iFlags,0))then begin Result := False; end; SetPrivilege('SeShutdownPrivilege',False) end else begin Result := False; end; end; function Tform1.SetPrivilege(sPrivilegeName: string; bEnabled: Boolean): Boolean; var TPPrev,TP: TTokenPrivileges; Token: THandle; dwRetLen: DWord; begin Result := False; OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,Token); TP.PrivilegeCount := 1; if( LookupPrivilegeValue(Nil,PChar(sPrivilegeName),TP.Privileges[ 0 ].LUID )) then begin if(bEnabled) then begin TP.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED; end else begin TP.Privileges[ 0 ].Attributes := 0; end; dwRetLen := 0; Result := AdjustTokenPrivileges(Token,False,TP,SizeOf( TPPrev ),TPPrev,dwRetLen); end; CloseHandle(Token ); end;procedure TForm1.FormCreate(Sender: TObject); begin WinExitInNT(EWX_POWEROFF); end;end.
首先聲明type ...... procedure GetAdmin; ...... implementation{$R *.DFM} procedure GetAdmin; var st : SYSTEMTIME; hToken : THANDLE; tkp : TOKEN_PRIVILEGES; rr : Dword; begin OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,hToken); LookupPrivilegeValue(nil,'SeShutdownPrivilege',tkp.Privileges[0].Luid); // 設定權限為1 tkp.PrivilegeCount := 1; tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED; // 得到系統權限 AdjustTokenPrivileges(hToken, FALSE, tkp, 0,nil,rr); end;procedure TForm1.Button1Click(Sender: TObject); begin GetAdmin;//調用獲取權限過程; ExitWindowsEx(EWX_SHUTDOWN or EWX_POWEROFF or EWX_FORCE, 0);//強行結束當前全部進程後關閉系統; //ExitWindowsEx(EWX_SHUTDOWN or EWX_POWEROFF, 0);//關閉系統前詢問用戶; //ExitWindowsEx(EWX_REBOOT, 0);//重起系統; //ExitWindowsEx(EWX_REBOOT OR EWX_force, 0)//強行結束全部進程後重起; //ExitWindowsEx(EWX_logoff, 0);//登出; //ExitWindowsEx(EWX_logoff OR EWX_force, 0);//強行結束全部進程後登出; end; {取得系統權限過程不可缺少,否則在NT內核的系統中不能正常通過驗証的.}
begin
Result := True;
if (SetPrivilege('SeShutdownPrivilege',True)) then
begin
if(not ExitWindowsEx(iFlags,0))then
begin
Result := False;
end;
SetPrivilege('SeShutdownPrivilege',False)
end else begin
Result := False;
end;
end; function Tform1.SetPrivilege(sPrivilegeName: string; bEnabled: Boolean): Boolean;
var
TPPrev,TP: TTokenPrivileges;
Token: THandle;
dwRetLen: DWord;
begin
Result := False;
OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,Token);
TP.PrivilegeCount := 1;
if( LookupPrivilegeValue(Nil,PChar(sPrivilegeName),TP.Privileges[ 0 ].LUID )) then
begin
if(bEnabled) then
begin
TP.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;
end else begin
TP.Privileges[ 0 ].Attributes := 0;
end;
dwRetLen := 0;
Result := AdjustTokenPrivileges(Token,False,TP,SizeOf( TPPrev ),TPPrev,dwRetLen);
end;
CloseHandle(Token );
end;procedure TForm1.FormCreate(Sender: TObject);
begin
WinExitInNT(EWX_POWEROFF);
end;end.
......
procedure GetAdmin;
......
implementation{$R *.DFM}
procedure GetAdmin;
var
st : SYSTEMTIME;
hToken : THANDLE;
tkp : TOKEN_PRIVILEGES;
rr : Dword;
begin
OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,hToken);
LookupPrivilegeValue(nil,'SeShutdownPrivilege',tkp.Privileges[0].Luid);
// 設定權限為1
tkp.PrivilegeCount := 1;
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
// 得到系統權限
AdjustTokenPrivileges(hToken, FALSE, tkp, 0,nil,rr);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
GetAdmin;//調用獲取權限過程;
ExitWindowsEx(EWX_SHUTDOWN or EWX_POWEROFF or EWX_FORCE, 0);//強行結束當前全部進程後關閉系統;
//ExitWindowsEx(EWX_SHUTDOWN or EWX_POWEROFF, 0);//關閉系統前詢問用戶;
//ExitWindowsEx(EWX_REBOOT, 0);//重起系統;
//ExitWindowsEx(EWX_REBOOT OR EWX_force, 0)//強行結束全部進程後重起;
//ExitWindowsEx(EWX_logoff, 0);//登出;
//ExitWindowsEx(EWX_logoff OR EWX_force, 0);//強行結束全部進程後登出;
end;
{取得系統權限過程不可缺少,否則在NT內核的系統中不能正常通過驗証的.}