1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls, Vcl.ComCtrls, Psapi;
type
TForm1 = class(TForm)
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
lastwindows: String;
VAL_pid: DWORD;
VAL_title: String;
VAL_handle_windows: HWND;
implementation
{$R *.dfm}
// PUBLIC FUNCTION : GET PATH FROM PID ( Psapi )
function GetPathFromPID(const PID: cardinal): string;
var
hProcess: THandle;
path: array[0..MAX_PATH - 1] of char;
begin
try
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, false, PID);
if hProcess <> 0 then
try
if GetModuleFileNameEx(hProcess, 0, path, MAX_PATH) = 0 then // Dépendance: Psapi
RaiseLastOSError;
result := ExtractFileName(path);
finally
CloseHandle(hProcess)
end
else
RaiseLastOSError;
Except
result := '@'; // Retourne un @ quand le path n'est pas trouvé Ex: System
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
lastwindows := '###NA###';
Timer1.Enabled := true;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
VAL_handle_windows := GetForegroundWindow;
GetWindowThreadProcessId(VAL_handle_windows,VAL_pid);
SetLength(VAL_title, 255);
SetLength(VAL_title, GetWindowText(VAL_handle_windows, PChar(VAL_title), Length(VAL_title)));
if(VAL_title <> lastwindows) = true then begin
if(GetPathFromPID(VAL_pid) = 'MonExe.exe') = true then begin // A modifier !
// L'exe a été trouvé !
end else begin
// L'exe n'as pas été trouvé !
end;
lastwindows := VAL_title;
end;
end;
end. |
Partager