Recent

Author Topic: [SOLVED] Simple Way to Close/Kill External App -Using Window Title  (Read 470 times)

pixelink

  • Hero Member
  • *****
  • Posts: 945
    • Pixelink Media
I have a working form that displays all open windows with their Title caption.
I just want to be able to close/kill chosen program in list just based on the window title.

I have been looking up TProcess, but I am not really finding code examples that do what i need, most docs I find are for Opening files/programs.


What I am doing is trying to replace is the "TerminateTask" from my old  VB6 app.
« Last Edit: May 26, 2019, 10:45:28 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

ASerge

  • Hero Member
  • *****
  • Posts: 1305
Re: Simple Way to Close/Kill External App -Using Window Title
« Reply #1 on: May 25, 2019, 07:37:35 am »
I have a working form that displays all open windows with their Title caption.
I just want to be able to close/kill chosen program in list just based on the window title.
First, you need a window handle. If you are displaying a list of external windows, you should already have them, if not, use the FindWindow function.
Next. Make a call to PostMessage(Wnd, WM_QUIT, 0, 0). For the main application window, this is similar to the application exit command. Or get the process ID of the window owner, through a call to the function GetWindowThreadProcessId, and then roughly terminate the process through a call to the functions OpenProcess and TerminateProcess.

440bx

  • Hero Member
  • *****
  • Posts: 958
Re: Simple Way to Close/Kill External App -Using Window Title
« Reply #2 on: May 25, 2019, 09:27:48 am »
I just want to be able to close/kill chosen program in list just based on the window title.
There are a number of potential problems in killing a process based on a window handle.

Serge is right in that you could send the program a WM_QUIT but, that may not close/kill the program.  For instance, if a process has two threads with their inputs attached to the same message queue, sending a WM_QUIT will only cause one of the threads to stop pumping messages and won't end the program.  What happens after one of the threads stops pumping messages depends on the application but, the result is unlikely to be anything desirable.

Another situation where the result is unlikely to be a desirable one is when a process has more than one thread pumping messages from independent message queues.  Forcefully ending one thread's message pump by using WM_QUIT won't end the process and, what's happens after that is unpredictable.

Actually, I don't know exactly what happens in those cases because I've never used the method you're suggesting.  It's not "clean", not reliable and, it opens the door to quite a few problems.

You're probably better off using TerminateProcess and relying on the O/S to clean up whatever is left behind by the terminated process.  For that, the handle has to have the right to end the process.  There will likely be some processeses your program won't be able to end/terminate.

A more "polite" way would be to send the target window a WM_CLOSE. That doesn't guarantee the process will end but, at least the user may have a chance to close the program gracefully.

HTH.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

pixelink

  • Hero Member
  • *****
  • Posts: 945
    • Pixelink Media
Re: Simple Way to Close/Kill External App -Using Window Title
« Reply #3 on: May 26, 2019, 12:28:41 pm »
Okay.. got it to work...

Code: Pascal  [Select]
  1.  
  2. var
  3.   Wnd: HWND;
  4. begin
  5.   Wnd := FindWindow(Nil, 'Test');
  6.   begin
  7.     PostMessage(Wnd, WM_CLOSE, 0, 0);
  8.     Wnd := FindWindowEx(0, Wnd, Nil, 'Test');
  9.   end;
  10. end;
  11.  
  12.  


Thanks for the help
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

440bx

  • Hero Member
  • *****
  • Posts: 958
Re: Simple Way to Close/Kill External App -Using Window Title
« Reply #4 on: May 26, 2019, 01:35:32 pm »
Okay.. got it to work...

Code: Pascal  [Select]
  1.  
  2. var
  3.   Wnd: HWND;
  4. begin
  5.   Wnd := FindWindow(Nil, 'Test');
  6.   begin
  7.     PostMessage(Wnd, WM_CLOSE, 0, 0);
  8.     Wnd := FindWindowEx(0, Wnd, Nil, 'Test');
  9.   end;
  10. end;
  11.  
  12.  


Thanks for the help

You're welcome but, it's not a good method to choose a process to close/terminate.  There are too many potential problems with it. 

For instance, it's nice that now you're posting a WM_CLOSE to the window but, the call to FindWindowEx is likely to execute before the target window has had a chance to process the WM_CLOSE.  SendMessageTimeout instead of PostMessage could be used to overcome some parts of that one problem but, it creates a few problems of its own too (particularly if you're making the call from the user interface thread.)

HTH.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

pixelink

  • Hero Member
  • *****
  • Posts: 945
    • Pixelink Media
Re: Simple Way to Close/Kill External App -Using Window Title
« Reply #5 on: May 26, 2019, 06:42:33 pm »
You're welcome but, it's not a good method to choose a process to close/terminate.  There are too many potential problems with it. 

For instance, it's nice that now you're posting a WM_CLOSE to the window but, the call to FindWindowEx is likely to execute before the target window has had a chance to process the WM_CLOSE.  SendMessageTimeout instead of PostMessage could be used to overcome some parts of that one problem but, it creates a few problems of its own too (particularly if you're making the call from the user interface thread.)

HTH.

Your're right.

I had already removed the line and cleaned up the code.

So this is the  updated code

Code: Pascal  [Select]
  1.  
  2. [code=pascal]
  3.  
  4. var
  5.   Wnd: HWND;
  6.   PostMessage(Wnd, WM_CLOSE, 0, 0);
  7.  
  8. end;
  9.  
  10.  
[/code]
« Last Edit: May 26, 2019, 06:44:40 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 945
    • Pixelink Media
Re: Simple Way to Close/Kill External App -Using Window Title
« Reply #6 on: May 26, 2019, 07:11:53 pm »
I decided to put in some way to know whether the window closed or not.
But, it IS NOT executing my If statement... no showmessage appears either way.

No error.... just closes the window and moves on.

Why doesn't this work?

Code: Pascal  [Select]
  1.  
  2. var
  3. Wnd: HWND;
  4.  
  5. begin
  6.     Wnd:= FindWindow(Nil, PChar(CWP));
  7.     PostMessage(Wnd, WM_CLOSE, 0, 0);
  8.     Delay(3000);
  9.  
  10.     while Wnd <> 0 do
  11.         begin
  12.              Wnd:= FindWindow(Nil, PChar(CWP));
  13.              if Wnd = 1 Then
  14.                  begin
  15.                      showmessage('gone');
  16.                      Break;
  17.                  end
  18.              else
  19.                  begin
  20.                      showmessage('it is still open');
  21.                      Break;
  22.                  end;
  23.              end;
  24.           end;
  25.       end
  26.  
  27.  

« Last Edit: May 26, 2019, 07:15:02 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

Bart

  • Hero Member
  • *****
  • Posts: 3452
    • Bart en Mariska's Webstek
Re: Simple Way to Close/Kill External App -Using Window Title
« Reply #7 on: May 26, 2019, 10:26:14 pm »
Wnd equals 0?

Bart

pixelink

  • Hero Member
  • *****
  • Posts: 945
    • Pixelink Media
Re: Simple Way to Close/Kill External App -Using Window Title
« Reply #8 on: May 26, 2019, 10:45:15 pm »
Wnd equals 0?

Bart

duh... how did I miss such an easy issue.

Sorry to bother everyone.

:)
« Last Edit: May 26, 2019, 11:08:07 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060