Comment fermer une application externe

Utilisateur anonyme - 10 sept. 2005 à 21:28
cs_hendrix Messages postés 65 Date d'inscription lundi 30 décembre 2002 Statut Membre Dernière intervention 18 novembre 2008 - 12 sept. 2005 à 16:24
Salut à tous. Bon on ouvre WORD avec shellexecute. Mais comment fermer cette application ?????

Merci

4 réponses

cs_ManChesTer Messages postés 374 Date d'inscription vendredi 20 octobre 2000 Statut Modérateur Dernière intervention 15 janvier 2021
11 sept. 2005 à 17:03
Voici une méthode pour quitter proprement l'application par son nom de fenetre (ou partie du nom):

Function FindActiveAppName(Name:String):Hwnd;
var WND : Hwnd;
WndName : array[0..255] of char;
begin
Name:=Uppercase(Name);
Result:=Invalid_Handle_Value;
WND:=FindWindow(nil,nil);
while wnd<>0 do
begin
GetWindowText(Wnd,WndName,Sizeof(WndName));
If WndName[0]>#0 then
begin
If pos(Name,Uppercase(WndName))>0 then
begin
result:=WND;
Exit;
end;
end;
Wnd:=GetWindow(Wnd,GW_HwNDNEXT);
end;
end;


Function TerminateApp(Name:String):Boolean;
Var ThreadId : Dword;
ProcessId : Dword;
WndHwnd : Hwnd;
begin
Result:=False;
WndHwnd:=FindActiveAppName(Name);
If WndHwnd<>Invalid_Handle_Value then
begin
GetWindowThreadProcessId(WndHwnd,@ThreadId);
ProcessId := OpenProcess(PROCESS_TERMINATE, false,ThreadId);
try
if ProcessId <> Invalid_Handle_Value then
Result:=TerminateProcess(ProcessId, 0);
finally
CloseHandle(ProcessId);
end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
TerminateApp('WORD');
end;


Bon Coding...

ManChesTer.
0
Utilisateur anonyme
11 sept. 2005 à 20:51
Manchester ca va mon pote?????
ca fait plaisir

Merci pour ta source
0
Utilisateur anonyme
11 sept. 2005 à 21:15
Petite question il y a pas une méthode plus simple une simple instruction comme shellexecute.
0
cs_hendrix Messages postés 65 Date d'inscription lundi 30 décembre 2002 Statut Membre Dernière intervention 18 novembre 2008 1
12 sept. 2005 à 16:24
avec un kill (enfin, faut que kill.exe (ou autre) soit sur le poste)

hendrix
0
Rejoignez-nous