const
IDLE = $40;
BELOW_NORMAL = $4000;
NORMAL = $20;
ABOVE_NORMAL = $8000;
HIGH = $80;
REALTIME = $100;
// this function changes process priority.
// It may be used for local or remote host. When used for local host
// TargetHost, UserName, Password parameters must be empty.
// For remote computer the parameters must be:
// TargetHost: name or IP address of the remote host, like '10.8.36.54'
// UserName: name of the user, may include domain, like 'MOON\Administrator';
// Password: the user's password.
// Handle: a handle of a running process.
// Priority: a new priority for the process. It must be one of the constants above.
procedure ProcessSetPriority(TargetHost, UserName, Password: string;
Handle: cardinal; Priority: integer);
var
Connection: TWmiConnection;
Query: TWmiQuery;
Method: TWmiMethod;
s: string;
begin
Query := TWmiQuery.Create(nil);
Connection := TWmiConnection.Create(nil);
Method := TWmiMethod.Create(nil);
try
Query.Connection := Connection;
Method.WmiObjectSource := Query;
Query.WQL.Text := 'select * from Win32_Process';
Connection.MachineName := TargetHost;
Connection.Credentials.UserName := UserName;
Connection.Credentials.Password := Password;
// exception may happen on Win9x, WinNT if WMI core is not installed;
// The provided credentials may also be invalid.
Connection.Connected := true;
Query.Open;
if not Query.Locate('Handle', Handle, []) then
raise Exception.Create('Process not found: '+IntToStr(Handle));
Method.WmiMethodName := 'SetPriority';
Method.InParams.ParamByName('Priority').AsInteger := Priority;
if Method.Execute <> 0 then
begin
s := Method.LastWmiErrorDescription;
if s = '' then s := 'Error '+ IntToStr(Method.LastWmiError);
raise Exception.Create(s);
end;
finally
Method.Free;
Query.Free;
Connection.Free;
end;
end;
|