Recent

Author Topic: [Windows] How kill process knowing path ?  (Read 12865 times)

seba22

  • Full Member
  • ***
  • Posts: 136
[Windows] How kill process knowing path ?
« on: May 12, 2010, 08:17:32 am »
Welcome,

I'm working on "update" application. I have to kill all running instances of my application before writing new files.

I know path and name.

Any idea how can i do this on Windows ?

Regards

Bart

  • Hero Member
  • *****
  • Posts: 5706
    • Bart en Mariska's Webstek
Re: [Windows] How kill process knowing path ?
« Reply #1 on: May 12, 2010, 09:21:48 am »
This is from an old Delphi program (win32), it will list all running processes, with their filenames.
Maybe you can use this as a startingpoint.

It uses TlHlp32 unit.

Code: [Select]
procedure TForm1.SnapBtnClick(Sender: TObject);
var H: THandle;
    PE: TProcessEntry32;
    GotModule: Boolean;
    ME: TModuleEntry32;
begin
  Memo1.Clear;
  with Memo1.SelAttributes do
  begin
    Color := clRed;
    Style := Style + [fsBold];
  end;
  Memo1.Lines.Add(Format('%-10s %-10s %-10s %-8s %s',
                  ['ProcessID','ModuleID','ParentID','Threads','Naam']));
  Memo1.SelAttributes := Memo1.Defattributes;
  try
    H := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
    if H = 0 then Exit;
    PE.dwSize := SizeOf(TProcessEntry32);
    if Process32First(H,PE) then
    repeat
      Memo1.Lines.Add(Format('%-10x %-10x %-10x %-8x %s',
                      [PE.th32ProcessID,PE.th32ModuleID,PE.th32ParentProcessID,
                      PE.cntThreads,PE.szExeFile]));

    until not Process32Next(H,PE);
  finally
    CloseHandle(H);
  end;
end;

Once you've got the processID you can terminate (kill, really, the process will not be able to save data) the process like this:
Code: [Select]
Try
   //trying to get access to process
   AHandle := OpenProcess(PROCESS_ALL_ACCESS,False,ID); //uses windows
   if AHandle = 0 then
   begin
     //cannot open process
     exit;
   end;
   //try to kill th eprocess (in a nasty way)
   if not TerminateProcess(AHandle,255) then
   begin
     //failure to kill
     exit;
   end;
 Finally
   CloseHandle(AHandle);
 end;

Or (probably nicer, send a WM_QUIT message to the process(?), never done that though.

Be aware, this code might not function in accectly the way it does in NT based Windows (w2K, XP and above) as it does in win9x (you might not be able to get access to the process, because you don't have the proper access-rights).

Bart

seba22

  • Full Member
  • ***
  • Posts: 136
Re: [Windows] How kill process knowing path ?
« Reply #2 on: May 12, 2010, 09:59:38 am »
Welcome,

I will test it leater (on my workstation).


I need working code on XP and above including vista and 7.

I hope that code won't be detected by AV as Virus.

I would like ask You a question.

http://www.tech-recipes.com/rx/446/xp_kill_windows_process_command_line_taskkill/

What about that code.
It says it works under XP, what about Vista and 7 ?

Maybye it's possible to "exec" this function from lazarus ?

Maybye it will be better solution ?

What's Your opinion ?

Bart

  • Hero Member
  • *****
  • Posts: 5706
    • Bart en Mariska's Webstek
Re: [Windows] How kill process knowing path ?
« Reply #3 on: May 13, 2010, 12:06:41 pm »
I need working code on XP and above including vista and 7.

Can't help you there. My computer has outlived XP an Vista. Now I'm aiming to also outlive W7  :)

Maybye it's possible to "exec" this function from lazarus ?

Take a look at TProcess.

Bart

seba22

  • Full Member
  • ***
  • Posts: 136
Re: [Windows] How kill process knowing path ?
« Reply #4 on: May 13, 2010, 06:09:57 pm »

Hello my friend.

I found override, maybye it's not gentle but give me 100% compatibility and no false alert on any AV.

So when user run UPDATE application (stand alone exe), that update WRITE file in configuration directory with name "kill".

Every few seconds my application check if file "kill" exist.
If yes, application.terminate ;)

And of course on start-up it's check is that file exist.

After update, updater remove file and everything work perfect.

I know, it's not recommended... but give me 100% compatibility...

 

TinyPortal © 2005-2018