这个问题在论坛上已经有多人讨论,建议下次先搜索下问题再提问。
const
SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
SE_REMOTE_SHUTDOWN_NAME = 'SeRemoteShutdownPrivilege';
var
hToken: THandle;
tkp, tkp_prev: TTokenPrivileges;
PrivilName: PChar;
ReturnLength: Cardinal;
procedure TForm1.EnablePrivileges;
begin
if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
raise Exception.Create('OpenProcessToken failed.');
PrivilName := SE_SHUTDOWN_NAME
if not LookupPrivilegeValue(PChar(edtComputerName.Text), PrivilName, tkp.Privileges[0].Luid) then
raise Exception.Create('You do not have Remote Shutdown Privilege on this'#13#10 +
'computer, or computer is not a Windows NT computer,'#13#10 +
'or computer cannot be found on the network.');
tkp.PrivilegeCount := 1;
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, False, tkp, SizeOf(TTokenPrivileges), tkp_prev, ReturnLength);
if GetLastError <> ERROR_SUCCESS then
raise Exception.Create('AdjustTokenPrivileges enable failed.');
end;