type
TForm1 = class(TForm)
EdtRomateName: TEdit;
BtnShutDown: TButton;
procedure BtnShutDownClick(Sender: TObject);
private
{ Private declarations }
function EnablePrivilege(lpSystemName:PChar; lpName:PChar):Bool;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
function TForm1.EnablePrivilege(lpSystemName, lpName: PChar): Bool;
var
hToken : THANDLE;
fOk : Bool;
fTest : Bool;
tp, tpNew : TOKEN_PRIVILEGES;
ReturnLength : Cardinal;
begin
fOk := FALSE;
if OpenProcessToken(GetCurrentProcess,(TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY),hToken) then
begin
tp.PrivilegeCount := 1;
if not (LookupPrivilegeValue(lpSystemName,lpName,tp.Privileges[0].Luid)) then
ShowMessage('Can not lookup privilege value!');
tp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
if not (AdjustTokenPrivileges(hToken,FALSE,tp,sizeof(tp),tpNew,ReturnLength)) then
ShowMessage('Can not adjust privilege value.');
if GetLastError = ERROR_SUCCESS then
fOk := TRUE
else
fOK := FALSE;
CloseHandle(hToken);
end;
Result := fOk;
end;
procedure TForm1.BtnShutDownClick(Sender: TObject);
var
RemoteShutDown : Bool;
ComputerName : PChar;
begin
if EdtRomateName.Text <> '' then
begin
ComputerName := PChar(EdtRomateName.Text;
EnablePrivilege(ComputerName,'SeRemoteShutdownPrivilege');
{此句EnablePrivilege(nil,'SeShutdownPrivilege');
下一句改为RemoteShutDown :=InitiateSystemShutdown(nil,nil,0,TRUE,FALSE);将实现本地关机}
RemoteShutDown :=InitiateSystemShutdown(ComputerName ,nil,0,TRUE,FALSE);
{将InitiateSystemShutdown函数的最后一个参数改为TRUE将实现restart}
if RemoteShutDown then
ShowMessage('(远程)关机成功!')
else
ShowMessage('(远程)关机失败!');
end;
end;