Relying on window title irks me... So this happened.
Simba Code:
function GetProccessID(ImageName : string) : TIntegerArray;
const Validation := [
'[\\/:\*\?"<>\|]', { Forbidden characters }
'^(nul|prn|con|lpt[0-9]|com[0-9])(\.|$)', { Forbidden names }
'^\.' { Cannot start with dot }
];
var
Index,
Results : Integer;
Output : TStringArray;
Process : TProcess;
begin
for Index to High(Validation) do
if ExecRegExpr(Validation[Index], ImageName) then
Exit();
Process.Init(nil);
try
Process.SetCommandLine('TASKLIST /FI "IMAGENAME EQ ' + ImageName + '.EXE" /FO LIST');
Process.SetOptions([poUsePipes, poNoConsole]);
Process.Execute();
Output := Explode(#13#10, Trim(Process.GetOutput().ReadAnsiString()));
finally
Process.Free();
end;
SetLength(Result, Length(Output));
for Index := 0 to High(Output) do
if ExecRegExpr('PID:\s*(\d+)', Output[Index]) then
begin
Result[Results] := StrToIntDef(ReplaceRegExpr('PID:\s*(\d+)', Output[Index], '$1', True), -1);
Results += 1;
end;
SetLength(Result, Results);
end;
function TInputPipeStream.ReadAnsiString() : string; override;
var
Buffer : string;
Count : Integer;
Index : Integer;
begin
repeat
SetLength(Buffer, 1024);
Count := Self.Read(Buffer[1], 1024);
if Count <> 0 then
begin
SetLength(Buffer, Count);
Result := Result + Buffer;
end;
until Count = 0;
end;
(*
Credit to Andrew D. for validation
http://stackoverflow.com/questions/11100821#answer-11101624
*)
Example use...
Simba Code:
function GetRuneScapeClient(): Integer;
var Processes: TIntegerArray;
begin
Processes := GetProcessID('JagexLauncher');
if Length(Processes) then
Exit(Processes[0]);
end;
Or, since we're assuming they have ogLib...
Simba Code:
GLXMapHooks(GetProcessID('JagexLauncher').First());