[PSAPI #1] EnumProcesses

Felix Nawothnig felix.nawothnig at t-online.de
Tue Jun 14 14:14:17 CDT 2005


I removed the NULL handling as it's partly wrong and undocumented 
anyway. I'll add a new testsuite which checks the third parameter and 
lots of other stuff as soon as I fixed the other functions too.

ChangeLog:
- Replaced server requests in EnumProcesses by native APIs
- Fixed third parameter (should return the amount of used and not
   needed memory)
-------------- next part --------------
Index: psapi_main.c
===================================================================
RCS file: /home/wine/wine/dlls/psapi/psapi_main.c,v
retrieving revision 1.23
diff -u -r1.23 psapi_main.c
--- psapi_main.c	29 Mar 2005 19:49:22 -0000	1.23
+++ psapi_main.c	14 Jun 2005 15:27:37 -0000
@@ -75,61 +75,49 @@
 /***********************************************************************
  *           EnumProcesses (PSAPI.@)
  */
-BOOL WINAPI EnumProcesses(DWORD *lpidProcess, DWORD cb, DWORD *lpcbNeeded)
+BOOL WINAPI EnumProcesses(DWORD *lpdwProcessIDs, DWORD cb, DWORD *lpcbUsed)
 {
-    HANDLE	hSnapshot;
-    DWORD	count;
-    DWORD	countMax;
-    DWORD       pid;
-    int         ret;
-
-    TRACE("(%p, %ld, %p)\n", lpidProcess,cb, lpcbNeeded);
+    SYSTEM_PROCESS_INFORMATION *spi;
+    NTSTATUS status;
+    PVOID pBuf = NULL;
+    ULONG nAlloc = 0x8000;
 
-    if ( lpidProcess == NULL )
-        cb = 0;
-    if ( lpcbNeeded != NULL )
-        *lpcbNeeded = 0;
+    do {
+        if (pBuf != NULL) 
+        {
+            HeapFree(GetProcessHeap(), 0, pBuf);
+            nAlloc *= 2;
+        }
 
-    SERVER_START_REQ( create_snapshot )
-    {
-        req->flags   = SNAP_PROCESS;
-        req->inherit = FALSE;
-        req->pid     = 0;
-        wine_server_call_err( req );
-        hSnapshot = reply->handle;
-    }
-    SERVER_END_REQ;
+        pBuf = HeapAlloc(GetProcessHeap(), 0, nAlloc);
+        if (pBuf == NULL)
+            return FALSE;
+
+        status = NtQuerySystemInformation(SystemProcessInformation, pBuf,
+                                          nAlloc, NULL);
+    } while (status == STATUS_INFO_LENGTH_MISMATCH);
 
-    if ( hSnapshot == 0 )
+    if (status != STATUS_SUCCESS)
     {
-        FIXME("cannot create snapshot\n");
+        HeapFree(GetProcessHeap(), 0, pBuf);
+        SetLastError(RtlNtStatusToDosError(status));
         return FALSE;
     }
-    count = 0;
-    countMax = cb / sizeof(DWORD);
-    for (;;)
+
+    spi = pBuf;
+
+    for(*lpcbUsed = 0; cb >= sizeof(DWORD); cb -= sizeof(DWORD))
     {
-        SERVER_START_REQ( next_process )
-        {
-            req->handle = hSnapshot;
-            req->reset = (count == 0);
-            if ((ret = !wine_server_call_err( req )))
-                pid = reply->pid;
-        }
-        SERVER_END_REQ;
-        if (!ret) break;
-        TRACE("process 0x%08lx\n", pid);
-        if ( count < countMax )
-            lpidProcess[count] = pid;
-        count++;
-    }
-    CloseHandle( hSnapshot );
+        *lpdwProcessIDs++ = spi->dwProcessID;
+        *lpcbUsed += sizeof(DWORD);
 
-    if ( lpcbNeeded != NULL )
-        *lpcbNeeded = sizeof(DWORD) * count;
+        if(spi->dwOffset == 0)
+            break;
 
-    TRACE("return %lu processes\n", count);
+        spi = (SYSTEM_PROCESS_INFORMATION *)(((PCHAR)spi) + spi->dwOffset);
+    }
 
+    HeapFree(GetProcessHeap(), 0, pBuf);
     return TRUE;
 }
 


More information about the wine-patches mailing list