[Solved] Killing external process lazarus / 34-Windows ---------------------------------------------------------------------- ffejrobins30 · 2011-10-16 03:42 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? ---------------------------------------------------------------------- Bart · 2011-10-16 10:39 Maybe take a look at this MS link: http://support.microsoft.com/kb/178893/en-us Also this may be of use? Quote from: WikiPediaMicrosoft 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 ---------------------------------------------------------------------- Bart · 2011-10-16 12:22 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 · 2011-10-16 14:56 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 · 2011-10-17 00:11 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 · 2011-11-08 15:31 Quote from: Bart on October 16, 2011, 02:22:44 pmI 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... ---------------------------------------------------------------------- TurboRascal · 2011-11-08 22:59 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...