function GetPidByProcessName(const ProcName: string): Integer;
function EnumWndProc(Handle: HWND; ST: TStrings): BOOL; stdcall;
var
Caption: array[0..128] of Char;
begin
Result := True;
SendMessage(Handle, WM_GETTEXT, Sizeof(Caption), integer(@Caption));
ST.AddObject(Caption, TObject(Handle));
end;
var
TS: TStrings;
begin
TS := TStringList.Create;
try
EnumWindows(@EnumWndProc, Integer(TS));
Result := TS.IndexOf(ProcName);
if Result <> -1 then Result := Integer(TS.Objects[Result]);
finally
TS.Free;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ShowMessageFmt('Pid: %8.8x',[GetPidByProcessName('Sin título: Bloc de notas')]);
end;