[PATCH] enumerate processes in reverse

Marcus Meissner marcus at jet.franken.de
Thu Oct 18 01:44:16 CDT 2007


A game (Last Chaos USA) does:
	Process32First();
	while (Process32First()) {
		/* look for stuff */
	}
completely missing the first process. Unfortunately it
is looking for its own child, which gets missed.

-> reverse enumerate processes

Added testcase too.

Ciao, Marcus
---
 dlls/kernel32/tests/toolhelp.c |    7 ++++++-
 dlls/kernel32/toolhelp.c       |    8 ++++----
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/dlls/kernel32/tests/toolhelp.c b/dlls/kernel32/tests/toolhelp.c
index ca9c06c..de4142b 100644
--- a/dlls/kernel32/tests/toolhelp.c
+++ b/dlls/kernel32/tests/toolhelp.c
@@ -108,6 +108,7 @@ static void test_process(DWORD curr_pid, DWORD sub_pcs_pid)
     MODULEENTRY32       me;
     unsigned            found = 0;
     int                 num = 0;
+    int			childpos = -1;
 
     hSnapshot = pCreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
     ok(hSnapshot != NULL, "Cannot create snapshot\n");
@@ -119,7 +120,7 @@ static void test_process(DWORD curr_pid, DWORD sub_pcs_pid)
         do
         {
             if (pe.th32ProcessID == curr_pid) found++;
-            if (pe.th32ProcessID == sub_pcs_pid) found++;
+            if (pe.th32ProcessID == sub_pcs_pid) { childpos = num; found++; }
             trace("PID=%x %s\n", pe.th32ProcessID, pe.szExeFile);
             num++;
         } while (pProcess32Next( hSnapshot, &pe ));
@@ -141,6 +142,10 @@ static void test_process(DWORD curr_pid, DWORD sub_pcs_pid)
     ok(found == 2, "couldn't find self and/or sub-process in process list\n");
     ok(!num, "mismatch in counting\n");
 
+    /* one broken program does Process32First() and does not expect anything
+     * interesting to be there, especially not the just forked off child */
+    ok (childpos !=0, "child is not expected to be at position 0.\n");
+
     te.dwSize = sizeof(te);
     ok(!pThread32First( hSnapshot, &te ), "shouldn't return a thread\n");
 
diff --git a/dlls/kernel32/toolhelp.c b/dlls/kernel32/toolhelp.c
index 0464b6c..6ffa4ce 100644
--- a/dlls/kernel32/toolhelp.c
+++ b/dlls/kernel32/toolhelp.c
@@ -222,7 +222,7 @@ static void fill_process( struct snapshot* snap, ULONG* offset,
     SIZE_T                      l;
 
     snap->process_count = num;
-    snap->process_pos = 0;
+    snap->process_pos = num; /* we go backwards */
     if (!num) return;
     snap->process_offset = *offset;
 
@@ -416,8 +416,8 @@ static BOOL process_next( HANDLE hSnapShot, LPPROCESSENTRY32W lppe,
     }
     if ((snap = MapViewOfFile( hSnapShot, FILE_MAP_ALL_ACCESS, 0, 0, 0 )))
     {
-        if (first) snap->process_pos = 0;
-        if (snap->process_pos < snap->process_count)
+        if (first) snap->process_pos = snap->process_count-1;
+        if (snap->process_pos >= 0)
         {
             LPPROCESSENTRY32W pe = (PROCESSENTRY32W*)&snap->data[snap->process_offset];
             if (unicode)
@@ -437,7 +437,7 @@ static BOOL process_next( HANDLE hSnapShot, LPPROCESSENTRY32W lppe,
                                      (char*)lppe->szExeFile, sizeof(lppe->szExeFile),
                                      0, 0 );
             }
-            snap->process_pos++;
+            snap->process_pos--;
             ret = TRUE;
         }
         else SetLastError( ERROR_NO_MORE_FILES );
-- 
1.5.2.4



More information about the wine-patches mailing list