taskkill: Implement forcible termination by process name.
authorAndrew Nguyen <anguyen@codeweavers.com>
Tue, 21 Sep 2010 07:21:23 +0000 (02:21 -0500)
committerAlexandre Julliard <julliard@winehq.org>
Tue, 21 Sep 2010 18:16:15 +0000 (20:16 +0200)
programs/taskkill/En.rc
programs/taskkill/taskkill.c
programs/taskkill/taskkill.h

index 724f9072a1c0c9d0cb79d37f9bab23ea7b2c8513..f7fcfd6e36b9e45590aeb42303bebba787e5ba2f 100644 (file)
@@ -34,6 +34,7 @@ STRINGTABLE
     STRING_CLOSE_PID_SEARCH, "Close message sent to top-level windows of process with PID %u.\n"
     STRING_CLOSE_PROC_SRCH, "Close message sent to top-level windows of process \"%s\" with PID %u.\n"
     STRING_TERM_PID_SEARCH, "Process with PID %u was forcibly terminated.\n"
+    STRING_TERM_PROC_SEARCH, "Process \"%s\" with PID %u was forcibly terminated.\n"
     STRING_SEARCH_FAILED, "Error: Could not find process \"%s\".\n"
     STRING_ENUM_FAILED, "Error: Unable to enumerate the process list.\n"
     STRING_TERMINATE_FAILED, "Error: Unable to terminate process \"%s\".\n"
index d864e60b652e89d8719fdf3b99282b06ed9f6fe9..c3080e5746119d88da0d96df2ff0a50025ef8c56 100644 (file)
@@ -280,9 +280,17 @@ static int send_close_messages(void)
 
 static int terminate_processes(void)
 {
+    DWORD *pid_list, pid_list_size;
     unsigned int i;
     int status_code = 0;
 
+    pid_list = enumerate_processes(&pid_list_size);
+    if (!pid_list)
+    {
+        taskkill_message(STRING_ENUM_FAILED);
+        return 1;
+    }
+
     for (i = 0; i < task_count; i++)
     {
         WCHAR *p = task_list[i];
@@ -323,9 +331,50 @@ static int terminate_processes(void)
             CloseHandle(process);
         }
         else
-            WINE_FIXME("Forcible process termination by name is not implemented\n");
+        {
+            DWORD index;
+            BOOL found_process = FALSE;
+
+            for (index = 0; index < pid_list_size; index++)
+            {
+                WCHAR process_name[MAX_PATH];
+
+                if (get_process_name_from_pid(pid_list[index], process_name, MAX_PATH) &&
+                    !strcmpiW(process_name, task_list[i]))
+                {
+                    HANDLE process;
+
+                    process = OpenProcess(PROCESS_TERMINATE, FALSE, pid_list[index]);
+                    if (!process)
+                    {
+                        taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
+                        status_code = 128;
+                        continue;
+                    }
+
+                    if (!TerminateProcess(process, 0))
+                    {
+                        taskkill_message_printfW(STRING_TERMINATE_FAILED, task_list[i]);
+                        status_code = 1;
+                        CloseHandle(process);
+                        continue;
+                    }
+
+                    found_process = TRUE;
+                    taskkill_message_printfW(STRING_TERM_PROC_SEARCH, task_list[i], pid_list[index]);
+                    CloseHandle(process);
+                }
+            }
+
+            if (!found_process)
+            {
+                taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
+                status_code = 128;
+            }
+        }
     }
 
+    HeapFree(GetProcessHeap(), 0, pid_list);
     return status_code;
 }
 
index a70cab0b0e2068cc6f4d522684699352d75c7add..e535a01d2e7576bb0997902a69398882390bfa11 100644 (file)
@@ -30,6 +30,7 @@
 #define STRING_CLOSE_PID_SEARCH 107
 #define STRING_CLOSE_PROC_SRCH  108
 #define STRING_TERM_PID_SEARCH  109
-#define STRING_SEARCH_FAILED    110
-#define STRING_ENUM_FAILED      111
-#define STRING_TERMINATE_FAILED 112
+#define STRING_TERM_PROC_SEARCH 110
+#define STRING_SEARCH_FAILED    111
+#define STRING_ENUM_FAILED      112
+#define STRING_TERMINATE_FAILED 113