Recent

Author Topic: [Solved] Killing external process  (Read 15393 times)

ffejrobins30

  • New Member
  • *
  • Posts: 10
[Solved] Killing external process
« on: October 16, 2011, 05:42:15 am »
I'm looking for a windows-specific way to kill an external process. My application starts and requires administrator privileges already on startup, so that's not a problem. I already know how to get the process ID, I just need the easiest, most effective way to kill a process. I've come across MANY Linux/Unix solutions, but none that are windows specific.

The reason for wanting to kill the process is that I am trying to hook up to a USB device, but this other process grabs the device and locks on until I manually kill it. It is a background launcher that detects the connection and launches another app, so it doesn't save any settings or need a clean shut down.

I've probably been loking at the answer all this time, but I seem to be too close to the forest to see the trees. Any ideas?
« Last Edit: October 17, 2011, 02:12:33 am by ffejrobins30 »

Bart

  • Hero Member
  • *****
  • Posts: 3917
    • Bart en Mariska's Webstek
Re: Killing external process
« Reply #1 on: October 16, 2011, 12:39:11 pm »
Maybe take a look at this MS link: http://support.microsoft.com/kb/178893/en-us

Also this may be of use?
Quote from: WikiPedia
Microsoft Windows XP, Vista and 7 include the command taskkill[1] to terminate processes. The usual syntax for this command is taskkill /im "IMAGENAME"


I have some legacy code (Win9x) at home for killing processes by PID. I do not know if this works on NT based systems however.

Bart
« Last Edit: October 16, 2011, 12:41:43 pm by Bart »

Bart

  • Hero Member
  • *****
  • Posts: 3917
    • Bart en Mariska's Webstek
Re: Killing external process
« Reply #2 on: October 16, 2011, 02:22:44 pm »
I just tested my old kill routine, it seems to work (tested on Win7 64 bit) as long as you have proper rights (at least I can see and kill notepad with it as a regular user, I did not feel the urge to try and kill some process that runs with admin-privileges ;-)  , especially since the Win7 machin is not mine...).

My kill procedure ir rude, it is considered to be more polite to first send WM_QUIT (?) messages to the process, so that the process can terminate in an orderly fashion (if it responds).

Attached are my old Delphi projects top and kill.
Feel free to use/alter whatever.
I did a crude translation of the strings used for user interaction (see comments).

Bart

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Killing external process
« Reply #3 on: October 16, 2011, 04:56:13 pm »
I have code like this in some Delphi project:
Code: [Select]
function KillApp(const sCapt: PChar): boolean;
var AppHandle: THandle;
begin
  AppHandle:=FindWindow(Nil, sCapt);
  Result:=PostMessage(AppHandle, WM_QUIT, 0, 0);
end;

var s: string; i: integer;
begin
  if paramcount>0 then begin
    s:=paramstr(1);
    for i:=2 to paramcount do s:=s+' '+paramstr(i);
    KillApp(PChar(s));
  end;
It is simply program that kills an application given to it in parameter, by the window title. There must be other function to get handle from ID, but that's how you can send the WM_QUIT to its handle.

ffejrobins30

  • New Member
  • *
  • Posts: 10
Re: Killing external process
« Reply #4 on: October 17, 2011, 02:11:01 am »
Bart, thanks for your reply. Your solution worked perfectly. I tried to post a WM_QUIT message to the process, but that didn't work. Since the process that I'm killing doesn't save anything and is just a launcher, i don't have a problem using the "rude" version.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Killing external process
« Reply #5 on: November 08, 2011, 04:31:07 pm »
I just tested my old kill routine, it seems to work (tested on Win7 64 bit) as long as you have proper rights (at least I can see and kill notepad with it as a regular user, I did not feel the urge to try and kill some process that runs with admin-privileges ;-)  , especially since the Win7 machin is not mine...).

My kill procedure ir rude, it is considered to be more polite to first send WM_QUIT (?) messages to the process, so that the process can terminate in an orderly fashion (if it responds).
Thanks Bart,

Probably PEBKAC/PICNIC but the attached project gives "Cannot open process ID" bla. Maybe I'm feeding it the wrong processID (taken from tasklist).
Yep, PICNIC as usual; apparently you have to feed it the process id in hex...

Anyway, attached is my version of kill converted to what seemed to be the right units in FPC.

Improvements welcome.

EDIT: Yes, it does seem to work. Now need to figure out how to get from executable name to task ID...
« Last Edit: November 26, 2011, 01:03:31 pm by BigChimp »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

TurboRascal

  • Hero Member
  • *****
  • Posts: 672
  • "Good sysadmin. Bad programmer."™
Re: [Solved] Killing external process
« Reply #6 on: November 08, 2011, 11:59:20 pm »
Now it would be nice to implement a crossplatform version. Ok, for unixes, killing the process could be implemented through the native API, so it would be necessary to find the process ID.

I wonder if there is an equivalent LCL message (LM_QUIT) implemented for the above code to become crossplatform as it is.

EDIT: I've looked for it and that message is really defined! http://lazarus-ccr.sourceforge.net/docs/lcl/lmessages/lm_quit.html
I wonder if it would really work with the example above...
« Last Edit: November 09, 2011, 12:34:07 am by TurboRascal »
Regards, ArNy the Turbo Rascal
-
"The secret is to give them what they need, not what they want." - Scotty, STTNG:Relics

 

TinyPortal © 2005-2018