Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
prog:lazarus:cas:processus:processus [24/04/2021 14:17] thierry [Énumérer les fenêtres actives] |
prog:lazarus:cas:processus:processus [24/04/2021 15:41] (Version actuelle) thierry [Trouver le nom complet d'un Processus avec QueryFullProcessImageName] |
||
---|---|---|---|
Ligne 17: | Ligne 17: | ||
<note tip>N'aurait t'il pas été plus facile d'utiliser ''JwaPsApi:GetProcessImageFileName'' ou ''QueryFullProcessImageNameA'' en passant le Handle du TProcess pour retrouver ce nom de fichier ???</note> | <note tip>N'aurait t'il pas été plus facile d'utiliser ''JwaPsApi:GetProcessImageFileName'' ou ''QueryFullProcessImageNameA'' en passant le Handle du TProcess pour retrouver ce nom de fichier ???</note> | ||
+ | ==== Trouver le nom complet d'un Processus avec QueryFullProcessImageName ==== | ||
+ | Ci dessous : | ||
+ | * ''GetFullProcessImageName'' retourne le nom de fichier complet en fonction du Handle du procesus | ||
+ | * ''GetFullProcessImageNameByProcID'' retourne le nom de fichier complet en fonction de l'ID du procesus.\\ (Récupere le Handle avec un ''OpenProcess'' puis appel ''GetFullProcessImageName'') | ||
+ | <code delphi> | ||
+ | uses Windows; | ||
+ | ... | ||
+ | function GetFullProcessImageName(const AProcessHandle: THandle): string; | ||
+ | var | ||
+ | vS: UnicodeString; | ||
+ | vLen: DWORD; | ||
+ | begin | ||
+ | vLen := MAX_PATH; | ||
+ | vS := ''; | ||
+ | SetLength(vS, vLen); | ||
+ | Result := ''; //par defaut | ||
+ | if QueryFullProcessImageNameW(AProcessHandle, 0, @vS[1], @vLen) then | ||
+ | begin | ||
+ | SetLength(vS, vLen); | ||
+ | Result := UTF8Encode(vS); | ||
+ | end; | ||
+ | end; | ||
+ | |||
+ | function GetFullProcessImageNameByProcID(const AProcId: DWord): string; | ||
+ | var | ||
+ | vHandle: THandle; | ||
+ | begin | ||
+ | Result := ''; | ||
+ | vHandle := OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, AProcId); | ||
+ | if vHandle > 0 then | ||
+ | begin | ||
+ | try | ||
+ | Result := GetFullProcessImageName(vHandle); | ||
+ | finally | ||
+ | CloseHandle(vHandle); | ||
+ | end; | ||
+ | end; | ||
+ | end; | ||
+ | |||
+ | |||
+ | </code> | ||
+ | |||
==== Énumérer les processus ==== | ==== Énumérer les processus ==== | ||
* Voir : [[https://wiki.freepascal.org/AppIsRunning]] | * Voir : [[https://wiki.freepascal.org/AppIsRunning]] | ||
+ | * [[https://wiki.lazarus.freepascal.org/Windows_Programming_Tips#Showing.2Ffinding_processes]] | ||
<code delphi> | <code delphi> | ||
- | function WindowsAppIsRunning(const ExeName: string): integer; | + | uses JwaTlHelp32 ; |
+ | // ATTENTION bien JwaTlHelp32, pas JwaWindows, sinon ça marche pas ! | ||
+ | ... | ||
+ | procedure EnumProc; | ||
var | var | ||
- | ContinueLoop: BOOL; | + | ContinueLoop: BOOL; |
- | FSnapshotHandle: THandle; | + | FSnapshotHandle: THandle; |
- | FProcessEntry32: TProcessEntry32; | + | FProcessEntry32: TProcessEntry32; |
begin | begin | ||
- | FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); | + | FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
- | FProcessEntry32.dwSize := SizeOf(FProcessEntry32); | + | FProcessEntry32.dwSize := SizeOf(FProcessEntry32); |
- | ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32); | + | ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32); |
- | Result := 0; | + | |
- | while integer(ContinueLoop) <> 0 do | + | while integer(ContinueLoop) <> 0 do |
- | begin | + | begin |
- | if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = | + | Form1.MMLog.Lines.Add('Process exe[%s]', [FProcessEntry32.szExeFile]); |
- | UpperCase(ExeName)) or (UpperCase(FProcessEntry32.szExeFile) = | + | ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32); |
- | UpperCase(ExeName))) then | + | end; |
- | begin | + | |
- | Inc(Result); | + | |
- | // SendMessage(Exit-Message) possible? | + | |
- | end; | + | |
- | ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32); | + | |
- | end; | + | |
- | CloseHandle(FSnapshotHandle); | + | CloseHandle(FSnapshotHandle); |
- | end; | + | end; |
</code> | </code> | ||
==== Énumérer les fenêtres actives ==== | ==== Énumérer les fenêtres actives ==== |