unit
unit Unit2;
interface
uses
Windows, Messages, Classes, Graphics, Controls, Forms, Dialogs,
System.SysUtils, Psapi, contnrs, shellapi,
StdCtrls, TlHelp32, Vcl.ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
// <a href='https://codeoncode.blogspot.com/2016/12/get-processid-by-programname-include.html' rel='noopener noreferrer' target='_blank'>https://codeoncode.blogspot.com/2016/12/get-processid-by-programname-include.html
// Get ProcessID By ProgramName (Include Path or Not Include)
function GetPIDByProgramName(const APName: string): THandle;
// Get Window Handle By ProgramName (Include Path or Not Include)
function GetHWndByProgramName(const APName: string): THandle;
// Get Window Handle By ProcessID
function GetHWndByPID(const hPID: THandle): THandle;
// Get ProcessID By Window Handle
function GetPIDByHWnd(const hWnd: THandle): THandle;
// Get Process Handle By Window Handle
function GetProcessHndByHWnd(const hWnd: THandle): THandle;
// Get Process Handle By Process ID
function GetProcessHndByPID(const hAPID: THandle): THandle;
var
Form1: TForm1;
implementation
{$R *.DFM}
// Get Window Handle By ProgramName (Include Path or Not Include)
function GetHWndByProgramName(const APName: string): THandle;
begin
Result := GetHWndByPID(GetPIDByProgramName(APName));
end;
// Get Process Handle By Window Handle
function GetProcessHndByHWnd(const hWnd: THandle): THandle;
var
PID: DWORD;
AhProcess: THandle;
begin
if hWnd <> 0 then
begin
GetWindowThreadProcessID(hWnd, @PID);
AhProcess := OpenProcess(PROCESS_ALL_ACCESS, false, PID);
Result := AhProcess;
CloseHandle(AhProcess);
end
else
Result := 0;
end;
// Get Process Handle By Process ID
function GetProcessHndByPID(const hAPID: THandle): THandle;
var
AhProcess: THandle;
begin
if hAPID <> 0 then
begin
AhProcess := OpenProcess(PROCESS_ALL_ACCESS, false, hAPID);
Result := AhProcess;
CloseHandle(AhProcess);
end
else
Result := 0;
end;
// Get Window Handle By ProcessID
function GetPIDByHWnd(const hWnd: THandle): THandle;
var
PID: DWORD;
begin
if hWnd <> 0 then
begin
GetWindowThreadProcessID(hWnd, @PID);
Result := PID;
end
else
Result := 0;
end;
// Get Window Handle By ProcessID
function GetHWndByPID(const hPID: THandle): THandle;
type
PEnumInfo = ^TEnumInfo;
TEnumInfo = record
ProcessID: DWORD;
hWnd: THandle;
end;
function EnumWindowsProc(Wnd: DWORD; var EI: TEnumInfo): Bool; stdcall;
var
PID: DWORD;
begin
GetWindowThreadProcessID(Wnd, @PID);
Result := (PID <> EI.ProcessID) or (not IsWindowVisible(Wnd)) or
(not IsWindowEnabled(Wnd));
if not Result then
EI.hWnd := Wnd; // break on return FALSE
end;
function FindMainWindow(PID: DWORD): DWORD;
var
EI: TEnumInfo;
begin
EI.ProcessID := PID;
EI.hWnd := 0;
EnumWindows(@EnumWindowsProc, Integer(@EI));
Result := EI.hWnd;
end;
begin
if hPID <> 0 then
Result := FindMainWindow(hPID)
else
Result := 0;
end;
// Get ProcessID By ProgramName (Include Path or Not Include)
function GetPIDByProgramName(const APName: string): THandle;
var
isFound: boolean;
AHandle, AhProcess: THandle;
ProcessEntry32: TProcessEntry32;
APath: array [0 .. MAX_PATH] of char;
begin
Result := 0;
AHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
try
ProcessEntry32.dwSize := Sizeof(ProcessEntry32);
isFound := Process32First(AHandle, ProcessEntry32);
while isFound do
begin
AhProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,
false, ProcessEntry32.th32ProcessID);
GetModuleFileNameEx(AhProcess, 0, @APath[0], Sizeof(APath));
if (UpperCase(StrPas(APath)) = UpperCase(APName)) or
(UpperCase(StrPas(ProcessEntry32.szExeFile)) = UpperCase(APName)) then
begin
Result := ProcessEntry32.th32ProcessID;
break;
end;
isFound := Process32Next(AHandle, ProcessEntry32);
CloseHandle(AhProcess);
end;
finally
CloseHandle(AHandle);
end;
end;
// Activate a window by its handle
function AppActivate(WindowHandle: hWnd): boolean; overload;
begin
try
SendMessage(WindowHandle, WM_SYSCOMMAND, SC_HOTKEY, WindowHandle);
SendMessage(WindowHandle, WM_SYSCOMMAND, SC_RESTORE, WindowHandle);
Result := SetForegroundWindow(WindowHandle);
except
on Exception do
Result := false;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Winhandle: THandle;
Nomfic :string ;
begin
Nomfic := 'notepad';
Winhandle := GetHWndByProgramName(Nomfic);
IF Winhandle > 0 THEN
begin
ShowWindow(Winhandle, SW_RESTORE);
SetForegroundwindow(Winhandle);
end
else
shellexecute(0,'open',pwidechar(Nomfic),nil,nil,SW_SHOWNORMAL); // 'C:\Dossier\NomFic.exe'
end;
end.
dfm
object Form1: TForm1
Left = 379
Top = 180
Caption = 'Form1'
ClientHeight = 235
ClientWidth = 349
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 144
Top = 128
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
end
voilà si tu peut en faire quelque chose
bonne prog
6 nov. 2021 à 17:27
Je ne sais pas laquelle est la plus performante (vitesse, occupation mémoire...) Mais ça ne doit pas faire une grosse différence dans le cas présent.
En tou cas, merci pour le lien sur ces fonctions qui peuvent m'être utiles un jour.